public function testContentEncoding() { // Test with base64 encoding $content = $this->part->getContent(); $this->assertEquals($this->testText, base64_decode($content)); // Test with quotedPrintable Encoding: $this->part->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE; $content = $this->part->getContent(); $this->assertEquals($this->testText, quoted_printable_decode($content)); // Test with 8Bit encoding $this->part->encoding = Mime\Mime::ENCODING_8BIT; $content = $this->part->getContent(); $this->assertEquals($this->testText, $content); }
/** * Generate MIME compliant message from the current configuration * * If both a text and HTML body are present, generates a * multipart/alternative Zend_Mime_Part containing the headers and contents * of each. Otherwise, uses whichever of the text or HTML parts present. * * The content part is then prepended to the list of Zend_Mime_Parts for * this message. * * @return void */ protected function _buildBody() { $text = $this->_mail->getBodyText(); $html = $this->_mail->getBodyHtml(); $htmlAttachments = $this->_mail->getHtmlRelatedAttachments(); $htmlAttachmentParts = $htmlAttachments->getParts(); $hasHtmlRelatedParts = count($htmlAttachmentParts); if ($text && $html || $html && $hasHtmlRelatedParts && count($this->_parts)) { // Generate unique boundary for multipart/alternative $mime = new Zend_Mime(null); $boundaryLine = $mime->boundaryLine($this->EOL); $boundaryEnd = $mime->mimeEnd($this->EOL); $html->disposition = false; if ($hasHtmlRelatedParts) { $message = new Zend_Mime_Message(); array_unshift($htmlAttachmentParts, $html); $message->setParts($htmlAttachmentParts); $htmlMime = $htmlAttachments->getMime(); $message->setMime($htmlMime); $html = new Zend_Mime_Part($message->generateMessage($this->EOL, false)); $html->boundary = $htmlMime->boundary(); $html->type = Zend_Mime::MULTIPART_RELATED; $html->encoding = null; } $body = $boundaryLine; if ($text) { $text->disposition = false; $body .= $text->getHeaders($this->EOL) . $this->EOL . $text->getContent($this->EOL) . $this->EOL . $boundaryLine; } $body .= $html->getHeaders($this->EOL) . $this->EOL . $html->getContent($this->EOL) . $this->EOL . $boundaryEnd; $mp = new Zend_Mime_Part($body); $mp->type = Zend_Mime::MULTIPART_ALTERNATIVE; $mp->boundary = $mime->boundary(); $this->_isMultipart = true; // Ensure first part contains text alternatives array_unshift($this->_parts, $mp); // Get headers $this->_headers = $this->_mail->getHeaders(); return; } // If not multipart, then get the body if (false !== ($body = $this->_mail->getBodyHtml())) { array_unshift($this->_parts, $body); if ($hasHtmlRelatedParts) { $this->_mail->setType(Zend_Mime::MULTIPART_RELATED); foreach ($htmlAttachmentParts as $part) { $this->_parts[] = $part; } } } elseif (false !== ($body = $this->_mail->getBodyText())) { array_unshift($this->_parts, $body); } if (!$body) { /** * @see Zend_Mail_Transport_Exception */ require_once 'Zend/Mail/Transport/Exception.php'; throw new Zend_Mail_Transport_Exception('No body specified'); } // Get headers $this->_headers = $this->_mail->getHeaders(); $headers = $body->getHeadersArray($this->EOL); foreach ($headers as $header) { // Headers in Zend_Mime_Part are kept as arrays with two elements, a // key and a value $this->_headers[$header[0]] = array($header[1]); } }
public function testShouldSendEmail() { if (!$this->_testsEnabled) { return; } $pwless = new Garp_Auth_Adapter_Passwordless(); $pwless->requestToken(array('email' => self::TEST_EMAIL)); $userModel = new Model_User(); $theUser = $userModel->fetchRow(); $authModel = new Model_AuthPasswordless(); $authRecord = $authModel->fetchRow(); $tokenUrl = new Garp_Util_FullUrl(array(array('method' => 'passwordless'), 'auth_submit')) . '?uid=' . $theUser->id . '&token=' . $authRecord->token; $storedMessage = file_get_contents(GARP_APPLICATION_PATH . '/../tests/tmp/' . self::TEST_EMAIL . '.tmp'); $expectedMessage = Garp_Util_String::interpolate($this->_getMockEmailMessage(), array('LOGIN_URL' => $tokenUrl)); // Pass thru actual Mime part, otherwise the two wil never be the same $mp = new Zend_Mime_Part($expectedMessage); $mp->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE; $mp->type = Zend_Mime::TYPE_TEXT; $mp->disposition = Zend_Mime::DISPOSITION_INLINE; $mp->charset = 'iso-8859-1'; // Just check for the token url. Message is encoded so checking for entire message to be // correct is overly complex (and not the responsibility of this unit test). $this->assertTrue(strpos($storedMessage, $mp->getContent("\r\n")) !== false); }
/** * Get the Content of the current Mime Part. This class assumes the * content is already encoded when $this->alreadyEncoded is set to true. * * @return String */ public function getContent($EOL = Zend_Mime::LINEEND) { if ($this->_isStream) { return parent::getContent($EOL); } else { if ($this->_alreadyEncoded) { return $this->_content; } else { return parent::getContent($EOL); } } }