/** * Saves e-mail message to a file * * @param Message $message * @throws Exception\RuntimeException on not writable target directory or * on file_put_contents() failure */ public function send(Message $message) { $options = $this->options; $filename = call_user_func($options->getCallback(), $this); $file = $options->getPath() . DIRECTORY_SEPARATOR . $filename; $email = $message->toString(); if (false === file_put_contents($file, $email)) { throw new Exception\RuntimeException(sprintf('Unable to write mail to file (directory "%s")', $options->getPath())); } $this->lastFile = $file; }
public function testRestoreFromSerializedString() { $this->message->addTo('*****@*****.**', 'ZF DevTeam'); $this->message->addFrom('*****@*****.**', "Matthew Weier O'Phinney"); $this->message->addCc('*****@*****.**', 'ZF Contributors List'); $this->message->setSubject('This is a subject'); $this->message->setBody('foo'); $serialized = $this->message->toString(); $restoredMessage = Message::fromString($serialized); $this->assertEquals($serialized, $restoredMessage->toString()); }
/** * @todo use Message::fromString($raw) to convert from eml to other formats */ public function convertFile($source, $destination) { if ($this->conversion[0] === 'html' && $this->conversion[1] === 'eml') { $parts = array('html' => NULL); // Load the HTML body. $html = file_get_contents($source); $html = preg_replace_callback('@(?<a>\\s+src=")(?<src>[^"]+)(?<b>")@si', function ($matches) use(&$parts) { // Mangle the src attribute. $src = $matches['src']; if (strpos($src, '://') === FALSE && !preg_match('@^/|\\.\\.@s', $src)) { if (is_file($src)) { $cid = 'imageid' . sizeof($parts); $ext = strtolower(pathinfo($src, PATHINFO_EXTENSION)); if ($ext === 'jpg') { $ext = 'jpeg'; } $parts[$cid] = new MimePart(fopen($src, 'r')); $parts[$cid]->type = "image/{$ext}; name={$cid}.{$ext}"; $parts[$cid]->encoding = Mime::ENCODING_BASE64; $parts[$cid]->id = $cid; $src = "cid:{$cid}"; } } // Rebuild the src attribute. $ret = $matches['a'] . $src . $matches['b']; return $ret; }, $html); // Build the message. $parts['html'] = new MimePart($html); $parts['html']->type = Mime::TYPE_HTML; $parts['html']->charset = 'utf-8'; $parts['html']->encoding = Mime::ENCODING_QUOTEDPRINTABLE; $body = new MimeMessage(); $body->setParts(array_values($parts)); // Build the full output. $message = new Message(); $message->setBody($body); $output = $message->toString(); file_put_contents($destination, $output); return $this; } throw new \InvalidArgumentException("Unsupported conversion type requested"); }
public function read() { // Setup filter array $filter_arr = array(); // TO if (!empty($this->to_filter)) { $filter_arr[] = "TO " . $this->to_filter; } // FROM if (!empty($this->from_filter)) { $filter_arr[] = "FROM " . $this->from_filter; } // CC if (!empty($this->cc_filter)) { $filter_arr[] = "CC " . $this->cc_filter; } // SUBJECT if (!empty($this->subject_filter)) { $filter_arr[] = "SUBJECT " . $this->subject_filter; } // BODY if (!empty($this->body_filter)) { $filter_arr[] = "BODY " . $this->body_filter; } // UNSEEN $filter_arr[] = "UNSEEN"; // Connect and fetch emails $this->w->Log->info("Connecting to mail server"); $mail = $this->connectToMail(); if (!empty($mail)) { $this->w->Log->info("Getting messages with filter: " . json_encode($filter_arr)); $results = $mail->protocol->search($filter_arr); if (count($results) > 0) { $this->w->Log->info("Found " . count($results) . " messages, looping through"); foreach ($results as $messagenum) { $rawmessage = ""; $message = $mail->getMessage($messagenum); $zend_message = new Zend_Mail_Message(); $zend_message->setHeaders($message->getHeaders()); $zend_message->setBody($message->getContent()); $email = new EmailStructure(); $email->to = $message->to; $email->from = $message->from; if (isset($message->cc)) { $email->cc = $message->cc; } $email->subject = $message->subject; //$email->body["html"] = $message->getContent(); $rawmessage .= $zend_message->toString(); // Create messages $channel_message = new ChannelMessage($this->w); $channel_message->channel_id = $this->channel_id; $channel_message->message_type = "email"; // $channel_message->attachment_id = $attachment_id; $channel_message->is_processed = 0; $channel_message->insert(); // Save raw email $attachment_id = $this->w->File->saveFileContent($channel_message, $rawmessage, str_replace(".", "", microtime()) . ".txt", "channel_email_raw", "text/plain"); if ($message->isMultipart()) { foreach (new RecursiveIteratorIterator($message) as $part) { try { $contentType = strtok($part->contentType, ';'); switch ($contentType) { case "text/plain": $email->body["plain"] = trim($part->__toString()); break; case "text/html": $email->body["html"] = trim($part->__toString()); break; default: // Is probably an attachment so just save it $transferEncoding = $part->getHeader("Content-Transfer-Encoding")->getFieldValue("transferEncoding"); $content_type_header = $part->getHeader("Content-Type"); // Name is stored under "parameters" in an array $nameArray = $content_type_header->getParameters(); $this->w->File->saveFileContent($channel_message, $transferEncoding == "base64" ? base64_decode(trim($part->__toString())) : trim($part->__toString()), !empty($nameArray["name"]) ? $nameArray["name"] : "attachment" . time(), "channel_email_attachment", $contentType); } } catch (Zend_Mail_Exception $e) { // Ignore } } } $attachment_id = $this->w->File->saveFileContent($channel_message, serialize($email), "email.txt", "channel_email_raw", "text/plain"); } } else { $this->w->Log->info("No new messages found"); } } }
<?php require_once __DIR__ . '/../../bootstrap.php'; use Zend\Mail\Storage\Writable\Maildir; use Zend\Mail\Message; use Zend\Mail\Storage; $settings = array('dirname' => 'test_maildir'); #Maildir::initMaildir($settings['dirname']); $mail = new Maildir($settings); #\Doctrine\Common\Util\Debug::dump($mail); $message = new Message(); $message->addFrom('*****@*****.**'); $message->addTo('*****@*****.**'); $message->setSubject('my_subject ' . time()); $message->setBody('my_body'); #$mail->appendMessage($message->toString(), null, null, false); #$mail->appendMessage($message->toString(), null, null, true); #$mail->appendMessage($message->toString(), null, array(), false); #$mail->appendMessage($message->toString(), null, array(), true); $mail->appendMessage($message->toString(), null, array(Storage::FLAG_DRAFT), false); #$mail->appendMessage($message->toString(), null, array(Storage::FLAG_DRAFT), true);
public function addMail(ZendMailMessage $mail, $folder = null, $flags = null, $recent = true) { $this->eventExecute(Event::TRIGGER_MAIL_ADD_PRE); $storage = $this->getDefaultStorage(); $mailStr = $mail->toString(); $msgId = $storage->addMail($mailStr, $folder, $flags, $recent); #fwrite(STDOUT, 'addMail msgId: '.$msgId.PHP_EOL); foreach ($this->storages as $storageId => $storage) { $storage->addMail($mailStr, $folder, $flags, $recent); } $this->eventExecute(Event::TRIGGER_MAIL_ADD, array($mail)); $this->eventExecute(Event::TRIGGER_MAIL_ADD_POST, array($msgId)); return $msgId; }