public function testSetGetParts() { $msg = new Mime\Message(); // No Parts $p = $msg->getParts(); $this->assertTrue(is_array($p)); $this->assertTrue(count($p) == 0); $p2 = array(); $p2[] = new Mime\Part('This is a test'); $p2[] = new Mime\Part('This is another test'); $msg->setParts($p2); $p = $msg->getParts(); $this->assertTrue(is_array($p)); $this->assertTrue(count($p) == 2); }
/** * Assert mime messages * * @param array $expected * @param \Zend\Mime\Message $actual */ public static function assertMimeMessages($expected, Mime\Message $actual) { $found = array(); foreach ($actual->getParts() as $part) { /* @var $part \Zend\Mime\Part */ static::assertInstanceOf('Zend\\Mime\\Part', $part); $contentType = strtolower($part->type); if (empty($contentType)) { foreach ($part->getHeadersArray() as $header) { list($field, $value) = $header; if (strtolower($field) === 'content-type') { $contentType = strtolower(preg_replace('/^\\s*([^;]+).*$/', '$1', $value)); break; } } } foreach ($expected as $mimeType => $rawContent) { if (strtolower($mimeType) == $contentType) { static::assertEquals($rawContent, $part->getRawContent(), sprintf('Content do not match in mime-type "%s"', $mimeType)); $found[$mimeType] = true; } } } foreach ($expected as $mimeType => $rawContent) { static::assertFalse(empty($found[$mimeType]), sprintf('Content not found in mime-type "%s"', $mimeType)); } }
/** * Sends the email. * * @param bool $resetData Whether or not to reset the service to its default values. * * @throws \Exception * * @return bool */ public function sendMail($resetData = true) { $_from[] = $this->from; $fromStr = $this->generateEmailStrings($_from); $toStr = $this->generateEmailStrings($this->to); $replyStr = $this->generateEmailStrings($this->replyto); $zendMailData = self::getMailerObject(); $mail = $zendMailData['mail']; $transport = $zendMailData['transport']; if (is_array($this->from) && count($this->from)) { if ($this->from[0] != '') { $from = $this->from; } } if (!isset($from)) { $from = array(Config::get('concrete.email.default.address'), Config::get('concrete.email.default.name')); $fromStr = Config::get('concrete.email.default.address'); } // The currently included Zend library has a bug in setReplyTo that // adds the Reply-To address as a recipient of the email. We must // set the Reply-To before any header with addresses and then clear // all recipients so that a copy is not sent to the Reply-To address. if (is_array($this->replyto)) { foreach ($this->replyto as $reply) { $mail->setReplyTo($reply[0], $reply[1]); } } $mail->setFrom($from[0], $from[1]); $mail->setSubject($this->subject); foreach ($this->to as $to) { $mail->addTo($to[0], $to[1]); } if (is_array($this->cc) && count($this->cc)) { foreach ($this->cc as $cc) { $mail->addCc($cc[0], $cc[1]); } } if (is_array($this->bcc) && count($this->bcc)) { foreach ($this->bcc as $bcc) { $mail->addBcc($bcc[0], $bcc[1]); } } $headers = $mail->getHeaders(); if ($headers->has('messageid')) { $messageIdHeader = $headers->get('messageid'); } else { $messageIdHeader = new \Zend\Mail\Header\MessageId(); $headers->addHeader($messageIdHeader); } $headers->addHeaders($this->headers); $messageIdHeader->setId(); $body = new MimeMessage(); if ($this->body !== false && $this->bodyHTML !== false) { $alternatives = new MimeMessage(); $text = new MimePart($this->body); $text->type = 'text/plain'; $text->charset = APP_CHARSET; $alternatives->addPart($text); $html = new MimePart($this->bodyHTML); $html->type = 'text/html'; $html->charset = APP_CHARSET; $alternatives->addPart($html); $alternativesPath = new MimePart($alternatives->generateMessage()); $alternativesPath->type = 'multipart/alternative;' . Mime::LINEEND . ' boundary="' . $alternatives->getMime()->boundary() . '"'; $body->addPart($alternativesPath); } elseif ($this->body !== false) { $text = new MimePart($this->body); $text->type = 'text/plain'; $text->charset = APP_CHARSET; $body->addPart($text); } elseif ($this->bodyHTML !== false) { $html = new MimePart($this->bodyHTML); $html->type = 'text/html'; $html->charset = APP_CHARSET; $body->addPart($html); } foreach ($this->attachments as $att) { $body->addPart($att); } if (count($body->getParts()) === 0) { $text = new MimePart(''); $text->type = 'text/plain'; $text->charset = APP_CHARSET; $body->addPart($text); } $mail->setBody($body); $sent = false; try { if (Config::get('concrete.email.enabled')) { $transport->send($mail); } $sent = true; } catch (Exception $e) { if ($this->getTesting()) { throw $e; } $l = new GroupLogger(LOG_TYPE_EXCEPTIONS, Logger::CRITICAL); $l->write(t('Mail Exception Occurred. Unable to send mail: ') . $e->getMessage()); $l->write($e->getTraceAsString()); if (Config::get('concrete.log.emails')) { $l->write(t('Template Used') . ': ' . $this->template); $l->write(t('To') . ': ' . $toStr); $l->write(t('From') . ': ' . $fromStr); if (isset($this->replyto)) { $l->write(t('Reply-To') . ': ' . $replyStr); } $l->write(t('Subject') . ': ' . $this->subject); $l->write(t('Body') . ': ' . $this->body); } $l->close(); } // add email to log if (Config::get('concrete.log.emails') && !$this->getTesting()) { $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); if (Config::get('concrete.email.enabled')) { $l->write('**' . t('EMAILS ARE ENABLED. THIS EMAIL WAS SENT TO mail()') . '**'); } else { $l->write('**' . t('EMAILS ARE DISABLED. THIS EMAIL WAS LOGGED BUT NOT SENT') . '**'); } $l->write(t('Template Used') . ': ' . $this->template); $l->write(t('Mail Details: %s', $mail->toString())); $l->close(); } // clear data if applicable if ($resetData) { $this->reset(); } return $sent; }
public function testNonMultipartMessageShouldNotRemovePartFromMessage() { $message = new Mime\Message(); // No Parts $part = new Mime\Part('This is a test'); $message->addPart($part); $message->generateMessage(); $parts = $message->getParts(); $test = current($parts); $this->assertSame($part, $test); }
/** * @param Message $mimeMessage * @return array */ protected function getAttachments(Message $mimeMessage) { $attachments = []; $parts = $mimeMessage->getParts(); /** @var \Zend\Mime\Part $part */ foreach ($parts as $part) { if (!isset($part->filename)) { continue; } $attachments[] = ['content' => $part->getContent(), 'type' => $part->type, 'name' => $part->filename]; } return $attachments; }
/** * Sends a mixed message via email * @param string[] $message * @param string[] $templates * @param string[] $variables * @return $this */ public function sendMixedMessage(array $message, array $templates, array $variables = []) { //create the message $mailMessage = $this->createMessage($message); //render the templates $contentMimeMessage = new MimeMessage(); foreach ($templates as $mimeType => $template) { //render the template $viewContent = $this->renderTemplate($template, $variables); //add the template to the message $mimePart = new MimePart($viewContent); $mimePart->type = $mimeType; $contentMimeMessage->addPart($mimePart); } //combine the alternative content into a single mime part if ($contentMimeMessage->isMultiPart()) { $contentMimePart = new MimePart($contentMimeMessage->generateMessage()); $contentMimePart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $contentMimeMessage->getMime()->boundary() . '"'; $contentMimeParts = [$contentMimePart]; } else { $contentMimeParts = $contentMimeMessage->getParts(); } //order the content before any attachments $finalMimeMessage = new MimeMessage(); $finalMimeMessage->setParts(array_merge($contentMimeParts, $mailMessage->getBody()->getParts())); $mailMessage->setBody($finalMimeMessage); //let the client choose which part to display if ($mailMessage->getBody()->isMultiPart()) { $mailMessage->getHeaders()->get('content-type')->setType('multipart/mixed'); } //send the message return $this->sendMessage($mailMessage); }