/** * create Tinebase_Mail from Zend_Mail_Message * * @param Zend_Mail_Message $_zmm * @param string $_replyBody * @return Tinebase_Mail */ public static function createFromZMM(Zend_Mail_Message $_zmm, $_replyBody = null) { $contentStream = fopen("php://temp", 'r+'); fputs($contentStream, $_zmm->getContent()); rewind($contentStream); $mp = new Zend_Mime_Part($contentStream); self::_getMetaDataFromZMM($_zmm, $mp); // append old body when no multipart/mixed if ($_replyBody !== null && $_zmm->headerExists('content-transfer-encoding')) { $mp = self::_appendReplyBody($mp, $_replyBody); } else { $mp->decodeContent(); if ($_zmm->headerExists('content-transfer-encoding')) { switch ($_zmm->getHeader('content-transfer-encoding')) { case Zend_Mime::ENCODING_BASE64: // BASE64 encode has a bug that swallows the last char(s) $bodyEncoding = Zend_Mime::ENCODING_7BIT; break; default: $bodyEncoding = $_zmm->getHeader('content-transfer-encoding'); } } else { $bodyEncoding = Zend_Mime::ENCODING_7BIT; } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Using encoding: ' . $bodyEncoding); } $mp->encoding = $bodyEncoding; } $result = new Tinebase_Mail('utf-8'); $result->setBodyText($mp); $result->setHeadersFromZMM($_zmm); return $result; }
/** * send a notification as email * * @param Tinebase_Model_FullUser $_updater * @param Addressbook_Model_Contact $_recipient * @param string $_subject the subject * @param string $_messagePlain the message as plain text * @param string $_messageHtml the message as html * @param string|array $_attachments */ public function send($_updater, Addressbook_Model_Contact $_recipient, $_subject, $_messagePlain, $_messageHtml = NULL, $_attachments = NULL) { // create mail object $mail = new Tinebase_Mail('UTF-8'); // this seems to break some subjects, removing it for the moment // -> see 0004070: sometimes we can't decode message subjects (calendar notifications?) //$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64); $mail->setSubject($_subject); $mail->setBodyText($_messagePlain); if ($_messageHtml !== NULL) { $mail->setBodyHtml($_messageHtml); } // set mail generator $mail->addHeader('X-MailGenerator', 'Tine 2.0'); // add header to identify mails sent by notification service $mail->addHeader('X-Tine20-Type', 'Notification'); // don't reply to this mail, dear autoresponder ... :) $mail->addHeader('Precedence', 'bulk'); if (empty($this->_fromAddress)) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' No notification service address set. Could not send notification.'); return; } if ($_updater !== NULL && !empty($_updater->accountEmailAddress)) { $mail->setFrom($_updater->accountEmailAddress, $_updater->accountFullName); $mail->setSender($this->_fromAddress, $this->_fromName); } else { $mail->setFrom($this->_fromAddress, $this->_fromName); } // attachments if (is_array($_attachments)) { $attachments =& $_attachments; } elseif (is_string($_attachments)) { $attachments = array(&$_attachments); } else { $attachments = array(); } foreach ($attachments as $attachment) { if ($attachment instanceof Zend_Mime_Part) { $mail->addAttachment($attachment); } else { if (isset($attachment['filename'])) { $mail->createAttachment($attachment['rawdata'], Zend_Mime::TYPE_OCTETSTREAM, Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $attachment['filename']); } else { $mail->createAttachment($attachment); } } } // send if (!empty($_recipient->email)) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Send notification email to ' . $_recipient->email); } $mail->addTo($_recipient->email, $_recipient->n_fn); Tinebase_Smtp::getInstance()->sendMessage($mail); } else { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Not sending notification email to ' . $_recipient->n_fn . '. No email address available.'); } }
/** * test line end encoding of Zend_Mime_Part / Smtp Protocol */ public function testSendWithWrongLineEnd() { $config = TestServer::getInstance()->getConfig(); $mailDomain = $config->maildomain ? $config->maildomain : 'tine20.org'; // build message with wrong line end rfc822 part $mail = new Tinebase_Mail('utf-8'); $mail->setBodyText('testmail' . "\r\n" . "\r\n"); $mail->setFrom('unittest@' . $mailDomain, 'unittest'); $mail->setSubject('line end test'); $mail->addTo('unittest@' . $mailDomain); $mail->addHeader('X-Tine20TestMessage', 'lineend'); // replace EOLs $content = file_get_contents(dirname(dirname(__FILE__)) . '/files/text_plain.eml'); $content = preg_replace("/\\x0a/", "\r\n", $content); $stream = fopen("php://temp", 'r+'); fputs($stream, $content); rewind($stream); $attachment = new Zend_Mime_Part($stream); $attachment->type = Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822; $attachment->encoding = null; $attachment->charset = 'ISO-8859-1'; $attachment->filename = 'attach.eml'; $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; $mail->addAttachment($attachment); $smtpConfig = $this->_account->getSmtpConfig(); $transport = new Felamimail_Transport($smtpConfig['hostname'], $smtpConfig); $mail->send($transport); $smtpLog = $transport->getConnection()->getLog(); $badLineEndCount = preg_match_all("/\\x0d\\x0d\\x0a/", $smtpLog, $matches); $this->assertEquals(0, $badLineEndCount); $badLineEndCount = preg_match_all("/\\x0d/", $smtpLog, $matches); $this->assertTrue(preg_match_all("/\\x0d/", $smtpLog, $matches) > 70, 'unix line ends are missing'); }
/** * set mail body * * @param Tinebase_Mail $_mail * @param Felamimail_Model_Message $_message */ protected function _setMailBody(Tinebase_Mail $_mail, Felamimail_Model_Message $_message) { if ($_message->content_type == Felamimail_Model_Message::CONTENT_TYPE_HTML) { $_mail->setBodyHtml(Felamimail_Message::addHtmlMarkup($_message->body)); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $_mail->getBodyHtml(TRUE)); } } $plainBodyText = $_message->getPlainTextBody(); $_mail->setBodyText($plainBodyText); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $_mail->getBodyText(TRUE)); } }
/** * send lost password mail * * @param string $_username * @return bool * * @todo add more texts to mail views & translate mails */ public function sendLostPasswordMail($_username) { // get full user $fullAccount = Tinebase_User::getInstance()->getFullUserByLoginName($_username); // generate new password $newPassword = $this->generatePassword(); // save new password in user Tinebase_Auth::getInstance()->setPassword($_username, $newPassword, $newPassword); // send lost password mail $mail = new Tinebase_Mail('UTF-8'); $mail->setSubject("New password for Tine 2.0"); // get name from user //$recipientName = $fullAccount->accountFirstName." ".$fullAccount->accountLastName; $recipientName = $fullAccount->accountFullName; // get email from user $recipientEmail = $fullAccount->accountEmailAddress; // get plain and html message from views //-- translate text and insert correct link $view = new Zend_View(); $view->setScriptPath(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'views'); $view->mailTextWelcome = "We generated a new password for you ..."; $view->newPassword = $newPassword; $messagePlain = $view->render('lostpwMailPlain.php'); $mail->setBodyText($messagePlain); $messageHtml = $view->render('lostpwMailHtml.php'); if ($messageHtml !== NULL) { $mail->setBodyHtml($messageHtml); } $mail->addHeader('X-MailGenerator', 'Tine 2.0'); $mail->setFrom('*****@*****.**', 'Tine 2.0 Webmaster'); if (!empty($recipientEmail)) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' send lost password email to ' . $recipientEmail); } $mail->addTo($recipientEmail, $recipientName); $mail->send(); return true; } return false; }
/** * create Tinebase_Mail from Zend_Mail_Message * * @param Zend_Mail_Message $_zmm * @param string $_replyBody * @return Tinebase_Mail */ public static function createFromZMM(Zend_Mail_Message $_zmm, $_replyBody = null) { $contentStream = fopen("php://temp", 'r+'); fputs($contentStream, $_zmm->getContent()); rewind($contentStream); $mp = new Zend_Mime_Part($contentStream); if ($_zmm->headerExists('content-transfer-encoding')) { $mp->encoding = $_zmm->getHeader('content-transfer-encoding'); $mp->decodeContent(); } else { $mp->encoding = Zend_Mime::ENCODING_7BIT; } // append old body when no multipart/mixed if ($_replyBody !== null && $_zmm->headerExists('content-transfer-encoding')) { $contentStream = fopen("php://temp", 'r+'); stream_copy_to_stream($mp->getRawStream(), $contentStream); fputs($contentStream, $_replyBody); rewind($contentStream); // create decoded stream $mp = new Zend_Mime_Part($contentStream); $mp->encoding = $_zmm->getHeader('content-transfer-encoding'); } if ($_zmm->headerExists('content-type')) { $contentTypeHeader = Zend_Mime_Decode::splitHeaderField($_zmm->getHeader('content-type')); $mp->type = $contentTypeHeader[0]; if (isset($contentTypeHeader['boundary'])) { $mp->boundary = $contentTypeHeader['boundary']; } if (isset($contentTypeHeader['charset'])) { $mp->charset = $contentTypeHeader['charset']; } } else { $mp->type = Zend_Mime::TYPE_TEXT; } $result = new Tinebase_Mail('utf-8'); $result->setBodyText($mp); foreach ($_zmm->getHeaders() as $header => $values) { foreach ((array) $values as $value) { switch ($header) { case 'content-transfer-encoding': // these are implicitly set by Zend_Mail_Transport_Abstract::_getHeaders() // these are implicitly set by Zend_Mail_Transport_Abstract::_getHeaders() case 'content-type': case 'mime-version': // do nothing break; case 'bcc': $addresses = Felamimail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->addBcc($address['address'], $address['name']); } break; case 'cc': $addresses = Felamimail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->addCc($address['address'], $address['name']); } break; case 'date': try { $result->setDate($value); } catch (Zend_Mail_Exception $zme) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . " Could not set date: " . $value); } if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . " " . $zme); } $result->setDate(); } break; case 'from': $addresses = Felamimail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->setFrom($address['address'], $address['name']); } break; case 'message-id': $result->setMessageId($value); break; case 'return-path': $result->setReturnPath($value); break; case 'subject': $result->setSubject($value); break; case 'to': $addresses = Felamimail_Message::parseAdresslist($value); foreach ($addresses as $address) { $result->addTo($address['address'], $address['name']); } break; default: $result->addHeader($header, $value); break; } } } return $result; }
/** * set mail body * * @param Tinebase_Mail $_mail * @param Felamimail_Model_Message $_message */ protected function _setMailBody(Tinebase_Mail $_mail, Felamimail_Model_Message $_message) { if ($_message->content_type == Felamimail_Model_Message::CONTENT_TYPE_HTML) { // checking embedded images $embeddedImages = $_mail->processEmbeddedImagesInHtmlBody($_message->body); // now checking embedded signature base64 image $base64Images = $_mail->processEmbeddedImageSignatureInHtmlBody($_message->body); if (count($embeddedImages) > 0) { $_message->body = str_ireplace('src="index.php?method=Felamimail.showTempImage&tempImageId=', 'src="cid:', $_message->body); } if (count($base64Images) > 0) { // there should be only one image in the signature $signature_cid = $_mail->createCid($base64Images[0][1]); $_message->body = preg_replace('/<img id="user-signature-image" alt="[^\\"]+" src="data:image\\/jpeg;base64,[^"]+">/', '<img id="user-signature-image" src="cid:' . $signature_cid . '"/>', $_message->body); } $_mail->setBodyHtml(Felamimail_Message::addHtmlMarkup($_message->body)); if (count($embeddedImages) > 0) { foreach ($embeddedImages as $embeddedImage) { $file = Tinebase_Core::getTempDir() . '/' . $embeddedImage[1]; $image = file_get_contents($file); $_mail->createHtmlRelatedAttachment($image, $embeddedImage[1], 'image/jpg', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, $embeddedImage[0]); } } if (count($base64Images) > 0) { // again, there should be only one image in the signature $image = base64_decode($base64Images[0][1]); $_mail->createHtmlRelatedAttachment($image, $signature_cid, 'image/jpg', Zend_Mime::DISPOSITION_INLINE, Zend_Mime::ENCODING_BASE64, $base64Images[0][0]); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $_mail->getBodyHtml(TRUE)); } } $plainBodyText = $_message->getPlainTextBody(); $_mail->setBodyText($plainBodyText); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $_mail->getBodyText(TRUE)); } }
/** * test line end encoding of Zend_Mime_Part / Smtp Protocol */ public function testSendWithWrongLineEnd() { $this->markTestSkipped('FIXME: 0011688: fix line end encoding in attachments'); // build message with wrong line end rfc822 part $mail = new Tinebase_Mail('utf-8'); $mail->setBodyText('testmail' . "\r\n" . "\r\n"); $mail->setFrom($this->_getEmailAddress(), 'unittest'); $mail->setSubject('line end test'); $mail->addTo($this->_getEmailAddress()); $mail->addHeader('X-Tine20TestMessage', 'lineend'); // replace EOLs $content = file_get_contents(dirname(dirname(__FILE__)) . '/files/text_plain.eml'); $content = preg_replace("/\\x0a/", "\r\n", $content); $stream = fopen("php://temp", 'r+'); fputs($stream, $content); rewind($stream); $attachment = new Zend_Mime_Part($stream); $attachment->type = Felamimail_Model_Message::CONTENT_TYPE_MESSAGE_RFC822; $attachment->encoding = null; $attachment->charset = 'ISO-8859-1'; $attachment->filename = 'attach.eml'; $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; $mail->addAttachment($attachment); $smtpConfig = $this->_account->getSmtpConfig(); $transport = new Felamimail_Transport($smtpConfig['hostname'], $smtpConfig); Zend_Mail_Protocol_Abstract::$loggingEnabled = true; $mail->send($transport); Zend_Mail_Protocol_Abstract::$loggingEnabled = false; $smtpLog = $transport->getConnection()->getLog(); $badLineEndCount = preg_match_all("/\\x0d\\x0d\\x0a/", $smtpLog, $matches); $this->assertEquals(0, $badLineEndCount); $unixLineEndCount = preg_match_all("/\\x0d/", $smtpLog, $matches); $this->assertTrue($unixLineEndCount > 70, 'unix line ends are missing (got ' . $unixLineEndCount . ' unix line ends)'); }