/** * 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; }
/** * @param \Zend\Mail\Storage\Message $part */ private function saveAttachment(ZendMessage $part) { @mkdir('./public/files/email/' . $this->message->getCode()); preg_match('^name=\\"(.*?)\\"^', $part->contentType, $filename); if (!$filename) { $filename = uniqid(); } else { $filename = $filename[1]; } try { /** * other possibilities (not worked out): 7bit, 8bit, binary, ietf-token, x-token * http://www.faqs.org/rfcs/rfc2045.html (part 6.1) */ if (isset($part->contenttransferencoding)) { $fp = fopen('./public/files/email/' . $this->message->getCode() . '/' . $filename, 'w'); if (strtolower($part->contenttransferencoding) == 'base64') { fwrite($fp, base64_decode($part->getContent())); } elseif (strtolower($part->contenttransferencoding) == 'quoted-printable') { $fp = fopen('./public/files/email/' . $this->message->getCode() . '/' . $filename, 'w'); fwrite($fp, quoted_printable_decode($part->getContent())); } fclose($fp); } else { $fp = fopen('./public/files/email/' . $this->message->getCode() . '/' . $filename, 'w'); fwrite($fp, $part->getContent()); fclose($fp); } $link = $this->getServiceLocator()->get('config')['api']['url'] . '/files/email/' . $this->message->getCode() . '/' . $filename; $file = './public/files/email/' . $this->message->getCode() . '/' . $filename; $attachment = new Attachment(); $attachment->setFile($file)->setLink($link)->setMessage($this->message); $this->message->getAttachments()->add($attachment); } catch (\Exception $e) { \Common\Service\Logger::getInstance()->err('mail import error attachments' . $this->message->getId(), array('error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine())); } }
public function testLateFetch() { $mail = new Storage\Mbox(array('filename' => __DIR__ . '/../_files/test.mbox/INBOX')); $message = new Message(array('handler' => $mail, 'id' => 5)); $this->assertEquals($message->countParts(), 2); $this->assertEquals($message->countParts(), 2); $message = new Message(array('handler' => $mail, 'id' => 5)); $this->assertEquals($message->subject, 'multipart'); $message = new Message(array('handler' => $mail, 'id' => 5)); $this->assertTrue(strpos($message->getContent(), 'multipart message') === 0); }
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); } } }
/** * 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; }