Esempio n. 1
0
 public function __construct(ConfigObject $config = null)
 {
     $this->message = new \Swift_Message();
     if ($config) {
         $this->message->setCharset($config->get('CharacterSet', 'utf-8'));
         $this->message->setMaxLineLength($config->get('MaxLineLength', 78));
         if ($config->get('Priority', false)) {
             $this->message->setPriority($config->get('Priority', 3));
         }
     }
 }
 public function testSend()
 {
     $message = new Swift_Message();
     $message->setFrom('*****@*****.**', 'Johnny #5');
     $message->setSubject('Is alive!');
     $message->addTo('*****@*****.**', 'A. Friend');
     $message->addTo('*****@*****.**');
     $message->addCc('*****@*****.**');
     $message->addCc('*****@*****.**', 'Extra 2');
     $message->addBcc('*****@*****.**');
     $message->addBcc('*****@*****.**', 'Extra 4');
     $message->addPart('<q>Help me Rhonda</q>', 'text/html');
     $message->addPart('Doo-wah-ditty.', 'text/plain');
     $attachment = Swift_Attachment::newInstance('This is the plain text attachment.', 'hello.txt', 'text/plain');
     $attachment2 = Swift_Attachment::newInstance('This is the plain text attachment.', 'hello.txt', 'text/plain');
     $attachment2->setDisposition('inline');
     $message->attach($attachment);
     $message->attach($attachment2);
     $message->setPriority(1);
     $headers = $message->getHeaders();
     $headers->addTextHeader('X-PM-Tag', 'movie-quotes');
     $messageId = $headers->get('Message-ID')->getId();
     $transport = new PostmarkTransportStub('TESTING_SERVER');
     $client = $this->getMock('GuzzleHttp\\Client', array('request'));
     $transport->setHttpClient($client);
     $o = PHP_OS;
     $v = phpversion();
     $client->expects($this->once())->method('request')->with($this->equalTo('POST'), $this->equalTo('https://api.postmarkapp.com/email'), $this->equalTo(['headers' => ['X-Postmark-Server-Token' => 'TESTING_SERVER', 'User-Agent' => "swiftmailer-postmark (PHP Version: {$v}, OS: {$o})", 'Content-Type' => 'application/json'], 'json' => ['From' => '"Johnny #5" <*****@*****.**>', 'To' => '"A. Friend" <*****@*****.**>,you+two@example.com', 'Cc' => 'another+1@example.com,"Extra 2" <*****@*****.**>', 'Bcc' => 'another+3@example.com,"Extra 4" <*****@*****.**>', 'Subject' => 'Is alive!', 'Tag' => 'movie-quotes', 'TextBody' => 'Doo-wah-ditty.', 'HtmlBody' => '<q>Help me Rhonda</q>', 'Headers' => [['Name' => 'Message-ID', 'Value' => '<' . $messageId . '>'], ['Name' => 'X-PM-KeepID', 'Value' => 'true'], ['Name' => 'X-Priority', 'Value' => '1 (Highest)']], 'Attachments' => [['ContentType' => 'text/plain', 'Content' => 'VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBhdHRhY2htZW50Lg==', 'Name' => 'hello.txt'], ['ContentType' => 'text/plain', 'Content' => 'VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBhdHRhY2htZW50Lg==', 'Name' => 'hello.txt', 'ContentID' => 'cid:' . $attachment2->getId()]]]]));
     $transport->send($message);
 }
Esempio n. 3
0
 /**
  * Create message from config params
  *
  * @param       $subject
  * @param       $message
  * @param array $placeholders email message placeholders in body
  *
  */
 public function createMessage($subject, $message, array $placeholders = [])
 {
     // get mail configurations
     $config = $this->getConfig();
     // add smtp exception plugin to resolve SMTP errors
     $this->mailer->registerPlugin(new MailSMTPException());
     // add decorator plugin to resolve messages placeholders
     $this->mailer->registerPlugin(new \Swift_Plugins_DecoratorPlugin($placeholders));
     // prepare message to transport
     $this->message = \Swift_Message::newInstance();
     $this->message->setFrom([$config['fromEmail'] => $config['fromName']]);
     $this->message->setSubject($subject);
     $this->message->setBody($message, 'text/html');
     $this->message->setCharset('UTF-8');
     $this->message->setReadReceiptTo($config['fromEmail']);
     $this->message->setPriority(1);
 }
Esempio n. 4
0
 /**
  * Set the priority of this message.
  * The value is an integer where 1 is the highest priority and 5 is the lowest.
  *
  * Modified version to also accept a string $message->setPriority('high');
  *
  * @param   mixed  $priority  integer|string
  * @return  object
  */
 public function setPriority($priority)
 {
     if (is_string($priority)) {
         switch (strtolower($priority)) {
             case 'high':
                 $priority = 1;
                 break;
             case 'normal':
                 $priority = 3;
                 break;
             case 'low':
                 $priority = 5;
                 break;
             default:
                 $priority = 3;
                 break;
         }
     }
     return parent::setPriority($priority);
 }
Esempio n. 5
0
 /**
  * Set the message priority level.
  *
  * @param  int  $level
  * @return $this
  */
 public function priority($level)
 {
     $this->swift->setPriority($level);
     return $this;
 }
 /**
  * Send the mail
  * @param string|array $to - mail reciver, can be also as array('*****@*****.**' => 'John Doe')
  * @param enum(html|text) $format - format of letter (html or text)
  * @return boolean
  */
 public function send($to, $format = 'text')
 {
     //include_once LIBPATH
     rad_mailtemplate::setCurrentItem($this);
     $o = rad_rsmarty::getSmartyObject();
     if ($this->getVars()) {
         foreach ($this->getVars() as $key => $value) {
             $o->assign($key, $value);
         }
     }
     if (!is_file(MAILTEMPLATESPATH . $this->getTemplateName())) {
         throw new rad_exception('File "' . MAILTEMPLATESPATH . $this->getTemplateName() . '" not found!');
     }
     $o->fetch(MAILTEMPLATESPATH . $this->getTemplateName());
     $o->clearAllAssign();
     if (empty($this->_blocks[$format])) {
         throw new rad_exception('Format "' . $format . '" is not declared in file: "' . MAILTEMPLATESPATH . $this->getTemplateName() . '"');
     }
     if (!empty($this->_mailer)) {
         $this->_mailer->setSubject($this->_blocks[$format]['subject']);
         if (!empty($this->_blocks[$format]['Cc'])) {
             $this->_mailer->setCc($this->_blocks[$format]['Cc']);
         }
         if (!empty($this->_blocks[$format]['Bcc'])) {
             $this->_mailer->setBcc($this->_blocks[$format]['Bcc']);
         }
         if (!empty($this->_blocks[$format]['headers'])) {
             $headers = rad_mailtemplate::parseHeader($this->_blocks[$format]['headers']);
             if (!empty($headers)) {
                 foreach ($headers as $headerName => $headerValue) {
                     switch (strtolower($headerName)) {
                         case 'x-priority':
                             $this->_mailer->setPriority((int) $headerValue);
                             break;
                         default:
                             $this->_mailer->getHeaders()->addTextHeader($headerName, $headerValue);
                             break;
                     }
                 }
             }
         }
         if (!empty($this->_blocks[$format]['body'])) {
             $this->_mailer->setBody($this->_blocks[$format]['body'], $format == 'text' ? 'text/plain' : 'text/html');
         }
         if (!empty($this->_blocks[$format]['from'])) {
             $from = explode("\n", str_replace("\r", '', $this->_blocks[$format]['from']));
             if (count($from)) {
                 foreach ($from as $fromString) {
                     $fromItem = explode('<', $fromString);
                     if (count($fromItem) > 1) {
                         $fromName = trim($fromItem[0]);
                         $fromEmail = trim(str_replace('>', '', $fromItem[1]));
                     } else {
                         $fromName = trim($fromItem[0]);
                         $fromEmail = trim($fromItem[0]);
                     }
                     $this->_mailer->setFrom(array($fromEmail => $fromName));
                     $this->_mailer->setReturnPath($fromEmail);
                 }
             }
         }
         if (!empty($this->_blocks[$format]['transport'])) {
             $transport = explode("\n", str_replace("\r", '', $this->_blocks[$format]['transport']));
             if (!empty($transport)) {
                 $transportParams = array();
                 foreach ($transport as $transportKey => $transportString) {
                     $transportString = trim($transportString);
                     if (!empty($transportString)) {
                         $transportItem = explode(':', $transportString);
                         if (count($transportItem) > 1) {
                             $transportItemKey = trim($transportItem[0]);
                             unset($transportItem[0]);
                             $transportItemValue = trim(implode(':', $transportItem));
                             $transportParams[$transportItemKey] = $transportItemValue;
                         }
                     }
                 }
             }
             if (empty($transportParams['type'])) {
                 throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" at transport block: type of the transport required!');
             }
             switch (strtolower($transportParams['type'])) {
                 case 'smtp':
                     if (empty($transportParams['host']) or empty($transportParams['port']) or empty($transportParams['user']) or !isset($transportParams['password'])) {
                         throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" at transport block: Not enouph actual params!');
                     }
                     $this->_transportInstance = Swift_SmtpTransport::newInstance($transportParams['host'], $transportParams['port'])->setUsername($transportParams['user'])->setPassword($transportParams['password']);
                     if (!empty($transportParams['security'])) {
                         $this->_transportInstance->setEncryption($transportParams['security']);
                     }
                     break;
                 case 'mail':
                     $this->_transportInstance = Swift_MailTransport::newInstance();
                     break;
                 default:
                     throw new rad_exception('Error in mailtemplate "' . $this->getTemplateName() . '" Unknown transport type "' . $transportParams['type'] . '"!');
                     break;
             }
             //switch
         }
         $this->_mailer->setTo($to);
         $this->_mailer->setCharset('utf-8');
         if (!$this->_transportInstance) {
             $this->_transportInstance = Swift_MailTransport::newInstance();
         }
         return rad_mailtemplate::getMailer($this->_transportInstance)->send($this->_mailer);
     } else {
         $headers = 'MIME-Version: 1.0' . PHP_EOL;
         $headers .= 'Content-Transfer-Encoding: base64' . PHP_EOL;
         $headers .= 'From: ' . $this->_blocks[$format]['from'] . PHP_EOL;
         switch ($format) {
             case 'text':
                 $headers = 'Content-Type: text/plain; charset=utf-8' . PHP_EOL;
                 break;
             case 'html':
                 $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
                 break;
             default:
                 throw new rad_exception('Unknown format: "' . $format . '"');
                 break;
         }
         if (!empty($this->_blocks[$format]['Cc'])) {
             $headers .= 'Cc: ' . $this->_blocks[$format]['Cc'] . PHP_EOL;
         }
         if (!empty($this->_blocks[$format]['Bcc'])) {
             $headers .= 'Bcc: ' . $this->_blocks[$format]['Bcc'] . PHP_EOL;
         }
         if (!empty($this->_blocks[$format]['headers'])) {
             $headers .= $this->_blocks[$format]['headers'];
         }
         if (is_array($to)) {
             $toString = '';
             foreach ($to as $toEmail => $toName) {
                 $toString .= $toName . ' <' . $toEmail . '>,';
             }
             $to = substr($toString, 0, strlen($toString) - 1);
         }
         return mail($to, $this->_blocks[$format]['subject'], chunk_split(base64_encode($this->_blocks[$format]['body'])), $headers);
     }
 }
 /**
  * Set the message priority
  * This is an integer between 1 (high) and 5 (low)
  * @param int The level of priority to use
  */
 public function setPriority($priority)
 {
     $this->message->setPriority($priority);
 }
 /**
  * The maximum range for the priority is 1-5.
  */
 public function testPriorityIsAdjustedIfSetTooHighOrLow()
 {
     for ($i = -10; $i < 1; $i++) {
         $msg = new Swift_Message();
         $msg->setPriority($i);
         $this->assertEqual(Swift_Message::PRIORITY_HIGH, $msg->getPriority());
         $structure = $msg->build()->readFull();
         $this->assertPattern("~X-Priority: 1\r\nX-MSMail-Priority: High~s", $structure);
     }
     for ($i = 15; $i > 5; $i--) {
         $msg = new Swift_Message();
         $msg->setPriority($i);
         $this->assertEqual(Swift_Message::PRIORITY_LOW, $msg->getPriority());
         $structure = $msg->build()->readFull();
         $this->assertPattern("~X-Priority: 5\r\nX-MSMail-Priority: Low~s", $structure);
     }
 }
Esempio n. 9
0
 /**
  * Send the e-mail
  *
  * Friendly name portions (e.g. Admin <*****@*****.**>) are allowed. The
  * method takes an unlimited number of recipient addresses.
  * 
  * @return boolean True if the e-mail was sent successfully
  */
 public function sendTo()
 {
     $arrRecipients = $this->compileRecipients(func_get_args());
     if (empty($arrRecipients)) {
         return false;
     }
     $this->objMessage->setTo($arrRecipients);
     $this->objMessage->setCharset($this->strCharset);
     $this->objMessage->setPriority($this->intPriority);
     // Default subject
     if ($this->strSubject == '') {
         $this->strSubject = 'No subject';
     }
     $this->objMessage->setSubject($this->strSubject);
     // HTML e-mail
     if ($this->strHtml != '') {
         // Embed images
         if ($this->blnEmbedImages) {
             if ($this->strImageDir == '') {
                 $this->strImageDir = TL_ROOT . '/';
             }
             $arrMatches = array();
             preg_match_all('/(src=|url\\()"([^"]+\\.(jpe?g|png|gif|bmp|tiff?|swf))"/Ui', $this->strHtml, $arrMatches);
             $strBase = \Environment::get('base');
             // Check for internal images
             foreach (array_unique($arrMatches[2]) as $url) {
                 // Try to remove the base URL
                 $src = str_replace($strBase, '', $url);
                 $src = rawurldecode($src);
                 // see #3713
                 // Embed the image if the URL is now relative
                 if (!preg_match('@^https?://@', $src) && file_exists($this->strImageDir . $src)) {
                     $cid = $this->objMessage->embed(\Swift_EmbeddedFile::fromPath($this->strImageDir . $src));
                     $this->strHtml = str_replace(array('src="' . $url . '"', 'url("' . $url . '"'), array('src="' . $cid . '"', 'url("' . $cid . '"'), $this->strHtml);
                 }
             }
         }
         $this->objMessage->setBody($this->strHtml, 'text/html');
     }
     // Text content
     if ($this->strText != '') {
         if ($this->strHtml != '') {
             $this->objMessage->addPart($this->strText, 'text/plain');
         } else {
             $this->objMessage->setBody($this->strText, 'text/plain');
         }
     }
     // Add the administrator e-mail as default sender
     if ($this->strSender == '') {
         list($this->strSenderName, $this->strSender) = $this->splitFriendlyName($GLOBALS['TL_CONFIG']['adminEmail']);
     }
     // Sender
     if ($this->strSenderName != '') {
         $this->objMessage->setFrom(array($this->strSender => $this->strSenderName));
     } else {
         $this->objMessage->setFrom($this->strSender);
     }
     // Send e-mail
     $intSent = self::$objMailer->send($this->objMessage, $this->arrFailures);
     // Log failures
     if (!empty($this->arrFailures)) {
         log_message('E-mail address rejected: ' . implode(', ', $this->arrFailures), $this->strLogFile);
     }
     // Return if no e-mails have been sent
     if ($intSent < 1) {
         return false;
     }
     $arrCc = $this->objMessage->getCc();
     $arrBcc = $this->objMessage->getBcc();
     // Add a log entry
     $strMessage = 'An e-mail has been sent to ' . implode(', ', array_keys($this->objMessage->getTo()));
     if (!empty($arrCc)) {
         $strMessage .= ', CC to ' . implode(', ', array_keys($arrCc));
     }
     if (!empty($arrBcc)) {
         $strMessage .= ', BCC to ' . implode(', ', array_keys($arrBcc));
     }
     log_message($strMessage, $this->strLogFile);
     return true;
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  *
  * @return $this|self
  */
 public function setPriority($priority) : self
 {
     $this->message->setPriority($priority);
     return $this;
 }
Esempio n. 11
-2
 /**
  * Send the e-mail
  *
  * Friendly name portions (e.g. Admin <*****@*****.**>) are allowed. The
  * method takes an unlimited number of recipient addresses.
  *
  * @return boolean True if the e-mail was sent successfully
  */
 public function sendTo()
 {
     $arrRecipients = $this->compileRecipients(func_get_args());
     if (empty($arrRecipients)) {
         return false;
     }
     $this->objMessage->setTo($arrRecipients);
     $this->objMessage->setCharset($this->strCharset);
     $this->objMessage->setPriority($this->intPriority);
     // Default subject
     if ($this->strSubject == '') {
         $this->strSubject = 'No subject';
     }
     $this->objMessage->setSubject($this->strSubject);
     // HTML e-mail
     if ($this->strHtml != '') {
         // Embed images
         if ($this->blnEmbedImages) {
             if ($this->strImageDir == '') {
                 $this->strImageDir = TL_ROOT . '/';
             }
             $arrCid = array();
             $arrMatches = array();
             $strBase = \Environment::get('base');
             // Thanks to @ofriedrich and @aschempp (see #4562)
             preg_match_all('/<[a-z][a-z0-9]*\\b[^>]*((src=|background=|url\\()["\']??)(.+\\.(jpe?g|png|gif|bmp|tiff?|swf))(["\' ]??(\\)??))[^>]*>/Ui', $this->strHtml, $arrMatches);
             // Check for internal images
             if (!empty($arrMatches) && isset($arrMatches[0])) {
                 for ($i = 0, $c = count($arrMatches[0]); $i < $c; $i++) {
                     $url = $arrMatches[3][$i];
                     // Try to remove the base URL
                     $src = str_replace($strBase, '', $url);
                     $src = rawurldecode($src);
                     // see #3713
                     // Embed the image if the URL is now relative
                     if (!preg_match('@^https?://@', $src) && file_exists($this->strImageDir . $src)) {
                         if (!isset($arrCid[$src])) {
                             $arrCid[$src] = $this->objMessage->embed(\Swift_EmbeddedFile::fromPath($this->strImageDir . $src));
                         }
                         $this->strHtml = str_replace($arrMatches[1][$i] . $arrMatches[3][$i] . $arrMatches[5][$i], $arrMatches[1][$i] . $arrCid[$src] . $arrMatches[5][$i], $this->strHtml);
                     }
                 }
             }
         }
         $this->objMessage->setBody($this->strHtml, 'text/html');
     }
     // Text content
     if ($this->strText != '') {
         if ($this->strHtml != '') {
             $this->objMessage->addPart($this->strText, 'text/plain');
         } else {
             $this->objMessage->setBody($this->strText, 'text/plain');
         }
     }
     // Add the administrator e-mail as default sender
     if ($this->strSender == '') {
         list($this->strSenderName, $this->strSender) = \String::splitFriendlyEmail(\Config::get('adminEmail'));
     }
     // Sender
     if ($this->strSenderName != '') {
         $this->objMessage->setFrom(array($this->strSender => $this->strSenderName));
     } else {
         $this->objMessage->setFrom($this->strSender);
     }
     // Set the return path (see #5004)
     $this->objMessage->setReturnPath($this->strSender);
     // Send e-mail
     $intSent = self::$objMailer->send($this->objMessage, $this->arrFailures);
     // Log failures
     if (!empty($this->arrFailures)) {
         log_message('E-mail address rejected: ' . implode(', ', $this->arrFailures), $this->strLogFile);
     }
     // Return if no e-mails have been sent
     if ($intSent < 1) {
         return false;
     }
     $arrCc = $this->objMessage->getCc();
     $arrBcc = $this->objMessage->getBcc();
     // Add a log entry
     $strMessage = 'An e-mail has been sent to ' . implode(', ', array_keys($this->objMessage->getTo()));
     if (!empty($arrCc)) {
         $strMessage .= ', CC to ' . implode(', ', array_keys($arrCc));
     }
     if (!empty($arrBcc)) {
         $strMessage .= ', BCC to ' . implode(', ', array_keys($arrBcc));
     }
     log_message($strMessage, $this->strLogFile);
     return true;
 }