/** * 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]); } }
/** * Send a mail using this transport * * @param Zend_Mail $mail * @access public * @return void * @throws Zend_Mail_Transport_Exception if mail is empty */ public function send(Zend_Mail $mail) { $this->_isMultipart = false; $this->_mail = $mail; $this->_parts = $mail->getParts(); $mime = $mail->getMime(); // Build body content $this->_buildBody(); // Determine number of parts and boundary $count = count($this->_parts); $boundary = null; if ($count < 1) { /** * @see Zend_Mail_Transport_Exception */ // require_once 'Zend/Mail/Transport/Exception.php'; throw new Zend_Mail_Transport_Exception('Empty mail cannot be sent'); } if ($count > 1) { // Multipart message; create new MIME object and boundary $mime = new Zend_Mime($this->_mail->getMimeBoundary()); $boundary = $mime->boundary(); } elseif ($this->_isMultipart) { // multipart/alternative -- grab boundary $boundary = $this->_parts[0]->boundary; } // Determine recipients, and prepare headers $this->recipients = implode(',', $mail->getRecipients()); $this->_prepareHeaders($this->_getHeaders($boundary)); // Create message body // This is done so that the same Zend_Mail object can be used in // multiple transports $message = new Zend_Mime_Message(); $message->setParts($this->_parts); $message->setMime($mime); $this->body = $message->generateMessage($this->EOL); // Send to transport! $this->_sendMail(); }
protected function _processSignedMessage($rawMessage) { $headers = ''; $matches = array(); preg_match('/.*boundary="(.*)"/', $headers['content-type'], $matches); list(, $boundary) = $matches; $signedMail = Zend_Mime_Message::createFromMessage($rawMessage, $boundary); return $signedMail; }
/** * check if decoding a string into a Zend_Mime_Message object works * */ public function testDecodeMimeMessage() { $text = <<<EOD This is a message in Mime Format. If you see this, your mail reader does not support this format. --=_af4357ef34b786aae1491b0a2d14399f Content-Type: application/octet-stream Content-Transfer-Encoding: 8bit This is a test --=_af4357ef34b786aae1491b0a2d14399f Content-Type: image/gif Content-Transfer-Encoding: base64 Content-ID: <12> This is another test --=_af4357ef34b786aae1491b0a2d14399f-- EOD; $res = Zend_Mime_Message::createFromMessage($text, '=_af4357ef34b786aae1491b0a2d14399f'); $parts = $res->getParts(); $this->assertEquals(2, count($parts)); $part1 = $parts[0]; $this->assertEquals('application/octet-stream', $part1->type); $this->assertEquals('8bit', $part1->encoding); $part2 = $parts[1]; $this->assertEquals('image/gif', $part2->type); $this->assertEquals('base64', $part2->encoding); $this->assertEquals('12', $part2->id); }
function renderMail($obj, $controller, $field, Am_Grid_Field_Expandable $fieldObj) { $_body = $obj->body; $_headers = unserialize($obj->headers); $atRendered = null; $val = ''; $headers = array(); foreach ($_headers as $k => $v) { $headers[$k] = $v[0]; } if (isset($headers['Content-Transfer-Encoding']) && $headers['Content-Transfer-Encoding'] == 'quoted-printable') { $body = quoted_printable_decode($_body); if (strpos($headers['Subject'], '=?') === 0) { $headers['Subject'] = mb_decode_mimeheader($headers['Subject']); } } else { $body = base64_decode($_body); } if ($body) { $body = nl2br($body); } foreach ($headers as $headerName => $headerVal) { $val .= '<b>' . $headerName . '</b> : <i>' . Am_Controller::escape($headerVal) . '</i><br />'; } if (isset($headers['Content-Type']) && strstr($headers['Content-Type'], 'multipart/mixed')) { preg_match('/boundary="(.*)"/', $headers['Content-Type'], $matches); $boundary = $matches[1]; $message = @Zend_Mime_Message::createFromMessage($body, $boundary); $parts = $message->getParts(); $part = @$parts[0]; if ($part) { $body = $part->getContent(); if ($part->encoding == 'quoted-printable') { $body = quoted_printable_decode($body); } else { $body = base64_decode($body); } } $attachments = array_slice($parts, 1); $atRendered = ''; foreach ($attachments as $at) { preg_match('/filename="(.*)"/', $at->disposition, $matches); $filename = @$matches[1]; $atRendered .= sprintf("&mdash %s (%s)", $filename, $at->type) . '<br />'; } } $val .= '<br />' . $body . ($atRendered ? '<br /><strong>Attachments:</strong><br />' . $atRendered : ''); return $val; }
protected function _multipartToFiles() { $files = array(); list($contentType, $boundary) = explode(';', $_SERVER['CONTENT_TYPE']); if ($contentType == 'multipart/mixed') { list(, $boundary) = explode('=', trim($boundary)); } else { throw new Zend_File_Transfer_Exception('This is not a multipart/mixed call'); } $data = file_get_contents('php://input'); $message = Zend_Mime_Message::createFromMessage($data, $boundary); foreach ($message->getParts() as $part) { if (!empty($part->disposition)) { $part->disposition = preg_replace('/;/', '&', $part->disposition); parse_str($part->disposition, $disposition); foreach ($disposition as $key => $value) { $disposition[$key] = preg_replace('/"/', '', $value); } } if (!empty($disposition['name'])) { $name = $disposition['name']; } if (!empty($disposition['filename'])) { $filename = $disposition['filename']; } if (!empty($part->filename)) { $filename = $part->filename; } if (!empty($name) && !empty($filename)) { $files[$name]['name'] = $filename; $files[$name]['type'] = $part->type; $files[$name]['error'] = 0; file_put_contents(sys_get_temp_dir() . '/' . $filename, $part->getContent()); $files[$name]['tmp_name'] = sys_get_temp_dir() . '/' . $filename; $files[$name]['size'] = filesize(sys_get_temp_dir() . '/' . $filename); } } return $files; }
/** * Return the MIME multipart representation of this MediaEntry. * * @return string The MIME multipart representation of this MediaEntry */ public function encode() { $xmlData = $this->saveXML(); if ($this->getMediaSource() === null) { // No attachment, just send XML for entry return $xmlData; } else { $mimeMessage = new Zend_Mime_Message(); $mimeMessage->setMime($this->_mime); $xmlPart = new Zend_Mime_Part($xmlData); $xmlPart->type = 'application/atom+xml'; $xmlPart->encoding = null; $mimeMessage->addPart($xmlPart); $binaryPart = new Zend_Mime_Part($this->getMediaSource()->encode()); $binaryPart->type = $this->getMediaSource()->getContentType(); $binaryPart->encoding = null; $mimeMessage->addPart($binaryPart); return $mimeMessage->generateMessage(); } }
/** * Returns the internal Zend mail object. * * @return Zend_Mail Zend mail object */ public function getObject() { if (!empty($this->_embedded)) { $parts = array(); if ($this->_html != null) { $part = new Zend_Mime_Part($this->_html); $part->charset = $this->_object->getCharset(); $part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE; $part->disposition = Zend_Mime::DISPOSITION_INLINE; $part->type = Zend_Mime::TYPE_HTML; $parts = array($part); } $msg = new Zend_Mime_Message(); $msg->setParts(array_merge($parts, $this->_embedded)); // create html body (text and maybe embedded), modified afterwards to set it to multipart/related $this->_object->setBodyHtml($msg->generateMessage()); $related = $this->_object->getBodyHtml(); $related->type = Zend_Mime::MULTIPART_RELATED; $related->encoding = Zend_Mime::ENCODING_8BIT; $related->boundary = $msg->getMime()->boundary(); $related->disposition = null; $related->charset = null; } else { if ($this->_html != null) { $this->_object->setBodyHtml($this->_html); } } return $this->_object; }
/** * Decodes a MIME encoded string and returns a Zend_Mime_Message object with * all the MIME parts set according to the given string * * @param string $message * @param string $boundary * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND} * @return Zend_Mime_Message */ public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND) { require_once 'Zend/Mime/Decode.php'; $parts = Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL); $res = new Zend_Mime_Message(); foreach ($parts as $part) { // now we build a new MimePart for the current Message Part: $newPart = new Zend_Mime_Part($part); foreach ($part['header'] as $key => $value) { /** * @todo check for characterset and filename */ // list($key, $value) = $header; switch ($key) { case 'content-type': $newPart->type = $value; break; case 'content-transfer-encoding': $newPart->encoding = $value; break; case 'content-id': $newPart->id = trim($value, '<>'); break; case 'Content-Disposition': $newPart->disposition = $value; break; case 'content-description': $newPart->description = $value; break; default: throw new Zend_Exception('Unknown header ignored for MimePart:' . $key); } } $res->addPart($newPart); } return $res; }
/** * Decodes a MIME encoded string and returns a Zend_Mime_Message object with * all the MIME parts set according to the given string * * @param string $message * @param string $boundary * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND} * @return Zend_Mime_Message */ public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND) { $partsStr = self::_disassembleMime($message, $boundary); if (count($partsStr) <= 0) { return null; } $res = new Zend_Mime_Message(); foreach ($partsStr as $part) { // separate header and body $header = true; // expecting header lines first $headersfound = array(); $body = ''; $lastheader = ''; $lines = explode("\n", $part); // read line by line foreach ($lines as $line) { $line = trim($line); if ($header) { if ($line == '') { $header = false; } elseif (strpos($line, ':')) { list($key, $value) = explode(':', $line, 2); $headersfound[trim($key)] = trim($value); $lastheader = trim($key); } else { if ($lastheader != '') { $headersfound[$lastheader] .= ' ' . trim($line); } else { // headers do not start with an ordinary header line? // then assume no headers at all $header = false; } } } else { $body .= $line . $EOL; } } // now we build a new MimePart for the current Message Part: $newPart = new Zend_Mime_Part($body); foreach ($headersfound as $key => $value) { /** * @todo check for characterset and filename */ switch ($key) { case 'Content-Type': $newPart->type = $value; break; case 'Content-Transfer-Encoding': $newPart->encoding = $value; break; case 'Content-ID': $newPart->id = trim($value, '<>'); break; case 'Content-Disposition': $newPart->disposition = $value; break; case 'Content-Description': $newPart->description = $value; break; default: throw new Zend_Exception('Unknown header ignored for MimePart:' . $key); } } $res->addPart($newPart); } return $res; }
/** * Send a mail using this transport * * @param OpenPGP_Zend_Mail $mail * @access public * @return void * @throws Zend_Mail_Transport_Exception if mail is empty */ public function send(OpenPGP_Zend_Mail $mail) { $this->_isMultipart = false; $this->_mail = $mail; $this->_parts = $mail->getParts(); $mime = $mail->getMime(); // Build body content $this->_buildBody(); // Determine number of parts and boundary $count = count($this->_parts); $boundary = null; if ($count < 1) { throw new Zend_Mail_Transport_Exception('Empty mail cannot be sent'); } if ($count > 1) { // Multipart message; create new MIME object and boundary $mime = new Zend_Mime($this->_mail->getMimeBoundary()); $boundary = $mime->boundary(); } elseif ($this->_isMultipart) { // multipart/alternative -- grab boundary $boundary = $this->_parts[0]->boundary; } // Determine recipients, and prepare headers $this->recipients = implode(',', $mail->getRecipients()); $this->_prepareHeaders($this->_getHeaders($boundary)); // Create message body // This is done so that the same OpenPGP_Zend_Mail object can be used in // multiple transports $message = new Zend_Mime_Message(); $message->setParts($this->_parts); $message->setMime($mime); $this->body = $message->generateMessage($this->EOL); //////////////////////////////////////////////////////// // // // ALPHAFIELDS 2012-11-03: ADDED PGP/MIME ENCRYPTION // // USING lib/openpgp/opepgplib.php // // // //////////////////////////////////////////////////////// // get from globals (set in tiki-setup.php) global $openpgplib; $pgpmime_msg = $openpgplib->prepareEncryptWithZendMail($this->header, $this->body, $mail->getRecipients()); $this->header = $pgpmime_msg[0]; // set pgp/mime headers from result array $this->body = $pgpmime_msg[1]; // set pgp/mime encrypted message body from result array //////////////////////////////////////////////////////// // // // ALPHAFIELDS 2012-11-03: ..END PGP/MIME ENCRYPTION // // // //////////////////////////////////////////////////////// // Send to transport! $this->_sendMail(); }
public function testCSVFileUpload() { $this->markTestIncomplete('The implementation is not yet finished and needs adaptation to new unit tests system.'); try { $this->_httpClient->setUri($this->_baseUri . '/files'); $this->_httpClient->setMethod(Zend_Http_Client::POST); $msg = new Zend_Mime_Message(); $jsonPart = new Zend_Mime_Part('{}'); $jsonPart->type = 'application/json'; $jsonPart->filename = microtime(true) . '.json'; $jsonPart->disposition = 'attachment'; $msg->addPart($jsonPart); $xmlPart = new Zend_Mime_Part($this->_icc . ',GEM17223,2,SIM_Model,APN1,10.1.2.1,1,TM SPAIN'); $xmlPart->type = 'text/plain'; $xmlPart->filename = microtime(true) . '.csv'; $xmlPart->disposition = 'attachment'; $msg->addPart($xmlPart); $this->_httpClient->setHeaders('Content-Type', 'multipart/mixed; boundary="' . $msg->getMime()->boundary() . '"'); $this->_httpClient->setRawData($msg->generateMessage()); $res = $this->_httpClient->request(); $this->assertEquals(200, $res->getStatus()); $obj = json_decode($res->getBody()); $this->assertTrue(is_object($obj)); $this->assertNotEmpty($obj->customerData); } catch (Exception $e) { $this->fail($e->getMessage()); } }
/** * get raw message as string * * @param Zend_Mail $mail * @param array $_additionalHeaders * @return string */ public function getRawMessage(Zend_Mail $mail = NULL, $_additionalHeaders = array()) { if ($mail !== NULL) { // this part is from Zend_Mail_Transport_Abstract::send() $this->_isMultipart = false; $this->_mail = $mail; $this->_parts = $mail->getParts(); $mime = $mail->getMime(); // Build body content $this->_buildBody(); // Determine number of parts and boundary $count = count($this->_parts); $boundary = null; if ($count < 1) { /** * @see Zend_Mail_Transport_Exception */ require_once 'Zend/Mail/Transport/Exception.php'; throw new Zend_Mail_Transport_Exception('Mail is empty'); } if ($count > 1) { // Multipart message; create new MIME object and boundary $mime = new Zend_Mime($this->_mail->getMimeBoundary()); $boundary = $mime->boundary(); } elseif ($this->_isMultipart) { // multipart/alternative -- grab boundary $boundary = $this->_parts[0]->boundary; } // Determine recipients, and prepare headers $this->recipients = implode(',', $mail->getRecipients()); $this->_prepareHeaders($this->_getHeaders($boundary)); // Create message body // This is done so that the same Zend_Mail object can be used in // multiple transports $message = new Zend_Mime_Message(); $message->setParts($this->_parts); $message->setMime($mime); $this->body = $message->generateMessage($this->EOL); } $mailAsString = $this->getHeaders($_additionalHeaders) . $this->EOL . $this->getBody(); // convert \n to \r\n $mailAsString = preg_replace("/(?<!\\r)\\n(?!\\r)/", "\r\n", $mailAsString); return $mailAsString; }