public function __construct(array $params) { parent::__construct($params); if (isset($params['allowedtags'])) { $this->allowedTags = $params['allowedtags']; } $this->processParts($this); }
/** * Public constructor * * @param array $params */ public function __construct(array $params) { parent::__construct($params); // do not do this for "child" messages (attachments) if (!empty($params['root'])) { $helper = new SanitizeHeaders(); $helper($this); } }
/** * Saves the current resource to the given path. Returns true if move action was successful; false otherwise. * * @param string $outputFolder the output folder path for this resource * * @throws \Msl\ResourceProxy\Exception\ResourceMoveContentException * * @return bool */ public function moveToOutputFolder($outputFolder) { try { if ($this->message->isMultipart()) { // Getting the resource content (subject + body of the email) $contentPart = $this->message->getPart(1); $content = $contentPart->getContent(); // Check for attachment // Getting second part of message object $part = $this->message->getPart(2); // Get the attachment file name if ($part->getHeaders()->has('Content-Disposition')) { $fileName = $part->getHeaderField('Content-Disposition', 'filename'); // Get the attachment and decode $attachment = base64_decode($part->getContent()); // Save the attachment $attachmentFileName = $this->getAttachmentFileName($fileName, $outputFolder); $finalAttOutputDirectory = $this->getAttachmentFileFolder($outputFolder); if (!file_exists($finalAttOutputDirectory)) { mkdir($finalAttOutputDirectory, 0775, true); } file_put_contents($attachmentFileName, $attachment); } } else { // Getting the resource content (subject + body of the email) $content = $this->message->getContent(); } // Writing content to file // Setting the file name (output folder + message sub folder + current object string representation + timestamp) $outputFileName = $this->getContentFileName($outputFolder); // Writing the content to the output file $finalOutputDirectory = $this->getContentFileFolder($outputFolder); if (!file_exists($finalOutputDirectory)) { mkdir($finalOutputDirectory, 0775, true); } file_put_contents($outputFileName, $content); } catch (\Exception $e) { throw new Exception\ResourceMoveContentException(sprintf('Error while moving the content of resource \'%s\' to the output folder \'%s\'. Error message is: \'%s\'.', $this->toString(), $outputFolder, $e->getMessage())); } return true; }
public function __construct(array $params) { parent::__construct($params); if (!$this->isMultipart()) { return; } for ($counter = 1; $counter <= $this->countParts(); ++$counter) { $part = $this->getPart($counter); $partHeaders = $part->getHeaders(); $partHeaders->getPluginClassLoader()->registerPlugin('contentdisposition', ContentDisposition::class); if (!$partHeaders->has('content-disposition')) { continue; } /** @var ContentDisposition $header */ $header = $part->getHeader('content-disposition'); $type = $header->getDisposition(); if (empty($type) || $type != 'attachment') { continue; } $this->attachments[$header->getParameter('filename')] = $part->getContent(); } }
/** * @param \Common\Entity\Email $email * @param \Zend\Mail\Storage\Message $part */ protected function getState(ZendMessage $part) { $state = null; // state auto reply if (is_null($state) && !empty($part->getHeaders()) && isset($part->autosubmitted)) { $state = State::STATE_AUTO_REPLY; } // state delivery notice if (!empty($part->getHeaders()) && isset($part->contenttype)) { $dummy = explode(';', $part->contentType); if (stripos($dummy[0], 'delivery-status') !== FALSE) { $state = State::STATE_DELIVERY_NOTICE; } } // state spam if (is_null($state) && !empty($part->getHeaders()) && isset($part->subject)) { if (stripos($part->subject, 'spam-suspect') !== FALSE) { $state = State::STATE_SPAM; } } // state paypal $from = $this->message->getAddresses()->filter(function ($entry) { return $entry->getType() == Address::TYPE_FROM; })->first(); if (strcasecmp($from->getAddress(), '*****@*****.**') === 0) { $state = State::STATE_PAYPAL; } // state ups $from = $this->message->getAddresses()->filter(function ($entry) { return $entry->getType() == Address::TYPE_FROM; })->first(); if (strcasecmp($from->getAddress(), '*****@*****.**') === 0) { $state = State::STATE_UPS; } // state new if (is_null($state)) { $state = State::STATE_NEW; } if ($this->message->getStates()->count() > 0) { $currentState = $this->message->getStates()[0]->getState(); if ($currentState !== State::STATE_NEW) { return; } $stateObj = $this->message->getStates()[0]; } else { $stateObj = new State(); } $stateObj->setMessage($this->message)->setState($state); $this->message->getStates()->add($stateObj); }
/** * Retrieves Message Attachments * * @param \Zend\Mail\Storage\Message $zendMailMessage * @return array */ private function processAttachments($zendMailMessage) { $attachments = array(); if ($zendMailMessage->getBody()) { $parts = $zendMailMessage->getBody()->getParts(); foreach ($parts as $part) { if ($part->disposition) { $disposition = \Zend\Mime\Decode::splitHeaderField($part->disposition); if ($disposition[0] == \Zend\Mime\Mime::DISPOSITION_ATTACHMENT && isset($disposition['filename'])) { $fileName = $disposition['filename']; $fileContent = $part->getContent(); $attachments[] = new Attachment($fileName, base64_decode($fileContent)); } } } } return $attachments; }
/** * @param Message $message * * @return bool */ protected function isSeen(Message $message) { return $message->hasFlag(Storage::FLAG_SEEN); }
public function testCaseInsensitiveMultipart() { $message = new Message(array('raw' => "coNTent-TYpe: muLTIpaRT/x-empty\r\n\r\n")); $this->assertTrue($message->isMultipart()); }
/** * @param Message $message * * @return array|null */ public function getAttachments(Message $message) { $attachments = null; // Only multi-part will have attachments if ($message->isMultipart()) { if ($message->countParts() > 1) { $partCount = $message->countParts(); for ($i = 0; $i < $partCount; $i++) { $part = $message->getPart($i + 1); // Check for headers, to avoid exceptions if ($part->getHeaders() !== null) { // Getting disposition $disposition = null; try { $disposition = $part->getHeaderField('Content-Disposition'); } catch (\Exception $e) { // Zend throws an Exception, if headerField does not exist } if ($disposition == 'attachment') { $content = quoted_printable_decode($part->getContent()); // Encoding? try { $transferEncoding = $part->getHeaderField('Content-Transfer-Encoding'); if ($transferEncoding == 'base64') { $content = base64_decode($content); } } catch (\Exception $e) { // Zend throws an Exception, if headerField does not exist } $attachments[] = array('mime' => $part->getHeaderField('Content-Type'), 'filename' => $part->getHeaderField('Content-Disposition', 'filename'), 'content' => $content); } } } } } return $attachments; }
/** * {@inheritdoc} */ public function __construct(array $params) { parent::__construct($params); }
protected function processBouncedMessage(\Zend\Mail\Storage\Message $message) { $content = $message->getContent(); $isHard = false; if (preg_match('/permanent[ ]*[error|failure]/', $content)) { $isHard = true; } if (preg_match('/X-QueueItemId: [a-z0-9\\-]*/', $content, $m)) { $arr = preg_split('/X-QueueItemId: /', $m[0], -1, \PREG_SPLIT_NO_EMPTY); $queueItemId = $arr[0]; if (!$queueItemId) { return; } $queueItem = $this->getEntityManager()->getEntity('EmailQueueItem', $queueItemId); if (!$queueItem) { return; } $massEmailId = $queueItem->get('massEmailId'); $massEmail = $this->getEntityManager()->getEntity('MassEmail', $massEmailId); $campaignId = null; if ($massEmail) { $campaignId = $massEmail->get('campaignId'); } $targetType = $queueItem->get('targetType'); $targetId = $queueItem->get('targetId'); $target = $this->getEntityManager()->getEntity($targetType, $targetId); $emailAddress = $queueItem->get('emailAddress'); if ($isHard && $emailAddress) { $emailAddressEntity = $this->getEntityManager()->getRepository('EmailAddress')->getByAddress($emailAddress); $emailAddressEntity->set('invalid', true); $this->getEntityManager()->saveEntity($emailAddressEntity); } if ($campaignId && $target && $target->id) { $this->getCampaignService()->logBounced($campaignId, $queueItemId, $target, $emailAddress, $isHard); } } }
/** * Retrieves Message Attachments * * @param \Zend\Mail\Storage\Message $imapMessage * @return array */ private function processAttachments($imapMessage) { $attachments = array(); if ($imapMessage->isMultipart()) { foreach (new \RecursiveIteratorIterator($imapMessage) as $part) { $headers = $part->getHeaders(); if ($headers->get('contentdisposition')) { $contentDisposition = $headers->get('contentdisposition'); $disposition = \Zend\Mime\Decode::splitHeaderField($contentDisposition->getFieldValue()); if ($disposition[0] == (\Zend\Mime\Mime::DISPOSITION_ATTACHMENT || \Zend\Mime\Mime::DISPOSITION_INLINE) && isset($disposition['filename'])) { $fileName = $disposition['filename']; $fileContent = $part->getContent(); $attachments[] = new Attachment($fileName, base64_decode($fileContent)); } } } } return $attachments; }
/** * convert raw mail to common array format. * * @param ReceiveMessage $rawMail * * common mail array format * * @return null|MailPart */ private function parseMailParts(ReceiveMessage $rawMail) { $rawMailHeaders = $rawMail->getHeaders(); $contentType = isset($rawMail->content_type) ? $rawMail->getHeaderField('Content-Type') : 'no_content_type'; $part = $this->configurePart($contentType); if (is_null($part)) { // prn('first'); return $part; } if ($rawMail->isMultipart()) { $partResult = []; try { foreach ($rawMail as $rawPart) { // prn($rawPart); $tres = $this->parseMailParts($rawPart); // prn($tres); if (!isset($tres)) { continue; } $partResult[] = $tres; } } catch (\Zend\Mail\Header\Exception\InvalidArgumentException $exc) { // prn($exc->getMessage()); } // prn($partResult); $part->setContent($partResult); $part->setHeaders($rawMailHeaders); } else { $isAttachment = in_array(explode('/', $contentType)[0], $this::$attachmentTypes); $partResult = $rawMail->getContent(); if (in_array('Content-Transfer-Encoding', array_keys($rawMailHeaders->toArray())) && isset($partResult)) { $partResult = $this->decode($partResult, $rawMail->contentTransferEncoding); $rawMailHeaders->removeHeader('Content-Transfer-Encoding'); } if ($isAttachment) { switch ($this->attachmentProcessingType) { case self::AttachmentNone: // prn('second'); return null; break; case self::AttachmentInfo: $partResult = null; break; case self::AttachmentFiles: break; } } else { try { $mailCharset = $rawMail->getHeaderField('content-type', 'charset'); } catch (\Exception $ex) { $mailCharset = null; } $partResult = $this->setMailEncoding($partResult, $mailCharset); } } $part->setContent($partResult); $part->setHeaders($rawMailHeaders); // prn('third'); return $part; }