/** * @return \Zend\Mail\AbstractTransport|null */ public function getMail() { if (null === $this->_transport) { $options = $this->getOptions(); foreach($options as $key => $option) { $options[strtolower($key)] = $option; } $this->setOptions($options); if(isset($options['transport']) && !is_numeric($options['transport'])) { $this->_transport = $this->_setupTransport($options['transport']); if(!isset($options['transport']['register']) || $options['transport']['register'] == '1' || (isset($options['transport']['register']) && !is_numeric($options['transport']['register']) && (bool) $options['transport']['register'] == true)) { \Zend\Mail\Mail::setDefaultTransport($this->_transport); } } $this->_setDefaults('from'); $this->_setDefaults('replyTo'); } return $this->_transport; }
/** * Send a mail using this transport * * @param \Zend\Mail\Mail $mail * @access public * @return void * @throws \Zend\Mail\Transport\Exception if mail is empty */ public function send(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 Transport\Exception\RuntimeException('Empty mail cannot be sent'); } if ($count > 1) { // Multipart message; create new MIME object and boundary $mime = new Mime\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\Mail object can be used in // multiple transports $message = new Mime\Message(); $message->setParts($this->_parts); $message->setMime($mime); $this->body = $message->generateMessage($this->EOL); // Send to transport! $this->_sendMail(); }
protected function _prepareMail() { $mail = new Mail\Mail(); $mail->setBodyText('This is the text of the mail.'); $mail->setFrom('*****@*****.**', 'Alexander Steshenko'); $mail->addTo('*****@*****.**', 'Oleg Lobach'); $mail->setSubject('TestSubject'); return $mail; }
/** * Sends mail to recipient(s) if log entries are present. Note that both * plaintext and HTML portions of email are handled here. * * @return void */ public function shutdown() { // If there are events to mail, use them as message body. Otherwise, // there is no mail to be sent. if (empty($this->_eventsToMail)) { return; } if ($this->_subjectPrependText !== null) { // Tack on the summary of entries per-priority to the subject // line and set it on the Zend_Mail object. $numEntries = $this->_getFormattedNumEntriesPerPriority(); $this->_mail->setSubject( "{$this->_subjectPrependText} ({$numEntries})"); } // Always provide events to mail as plaintext. $this->_mail->setBodyText(implode('', $this->_eventsToMail)); // If a Zend_Layout instance is being used, set its "events" // value to the lines formatted for use with the layout. if ($this->_layout) { // Set the required "messages" value for the layout. Here we // are assuming that the layout is for use with HTML. $this->_layout->events = implode('', $this->_layoutEventsToMail); // If an exception occurs during rendering, convert it to a notice // so we can avoid an exception thrown without a stack frame. try { $this->_mail->setBodyHtml($this->_layout->render()); } catch (\Exception $e) { trigger_error( "exception occurred when rendering layout; " . "unable to set html body for message; " . "message = {$e->getMessage()}; " . "code = {$e->getCode()}; " . "exception class = " . get_class($e), E_USER_NOTICE); } } // Finally, send the mail. If an exception occurs, convert it into a // warning-level message so we can avoid an exception thrown without a // stack frame. try { $this->_mail->send(); } catch (\Exception $e) { trigger_error( "unable to send log entries via email; " . "message = {$e->getMessage()}; " . "code = {$e->getCode()}; " . "exception class = " . get_class($e), E_USER_WARNING); } }
/** * E-mails the formatted text as attachment. * * @param string $formatedText */ public function write($formatedText) { $mail = new Mail(); $mail->addTo($this->to); $mail->setFrom($this->from); $mail->setSubject($this->subject); $mail->setBodyHtml(file_get_contents($this->emailTemplate)); $at = new Part($formatedText); $at->type = 'text/html'; $at->disposition = Mime::DISPOSITION_INLINE; $at->encoding = Mime::ENCODING_BASE64; $at->filename = $this->attachmentName; $at->description = 'LiveTest Attachment'; $mail->addAttachment($at); $mail->send(); }
private function writeMail($bodyText, $atText = null) { $mail = new Mail(); $mail->addTo($this->to); $mail->setFrom($this->from); $mail->setSubject($this->subject); $mail->setBodyHtml(file_get_contents($this->template) . $bodyText); if ($at !== null) { $at = new Part($atText); $at->type = 'text/html'; $at->disposition = Mime::DISPOSITION_INLINE; $at->encoding = Mime::ENCODING_BASE64; $at->filename = $this->attachmentName; $at->description = 'LiveTest Attachment'; $mail->addAttachment($at); } $mail->send(); }
/** * @group ZF-9011 * */ public function testSendmailTransportThrowsExceptionWithInvalidParams() { $mail = new Mail\Mail("UTF-8"); $mail->setBodyText('My Nice Test Text'); $mail->addTo('*****@*****.**'); $mail->setSubject('hello world!'); $transport = new Transport\Sendmail(); $transport->parameters = true; try { $mail->send($transport); $this->fail('Exception should have been thrown, but wasn\'t'); } catch(Transport\Exception $e) { // do nothing } }
/** * @group ZF-8981 */ public function testNumericRegisterDirectiveIsPassedOnCorrectly() { $options = array('transport' => array('type' => 'sendmail', 'register' => '1')); // Culprit $resource = new MailResource(array()); $resource->setBootstrap($this->bootstrap); $resource->setOptions($options); $resource->init(); $this->assertTrue(Mail::getDefaultTransport() instanceof \Zend\Mail\Transport\Sendmail); }
protected function tearDown() { \Zend\Mail\Mail::clearDefaultTransport(); }