In particular, it tries to maintain the following features:
/** * Sends an email * @param \Swift_Message $message An email message * @param string $content The message body in HTML * @return int The number of recipients the message was delivered to */ protected static function sendEmail(\Swift_Message $message, $content) { // Lazy mailer instantiation if (!self::$mailer) { self::$mailer = \Swift_Mailer::newInstance(\Swift_SmtpTransport::newInstance(SMTP_SERVER, SMTP_SERVER_PORT)); } $message->setBody($content, 'text/html', 'utf-8'); $message->addPart(\Html2Text\Html2Text::convert(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8')), 'text/plain', 'utf-8'); return self::$mailer->send($message); }
/** * zMontaMessagem * @param string $pathFile */ protected function zMontaMessagem($pathFile) { $dom = new Dom(); $dom->loadXMLFile($pathFile); $infMDFe = $dom->getNode('infMDFe', 0); $ide = $infMDFe->getElementsByTagName('ide')->item(0); $emit = $infMDFe->getElementsByTagName('emit')->item(0); $tot = $infMDFe->getElementsByTagName('tot')->item(0); $razao = $emit->getElementsByTagName('xNome')->item(0)->nodeValue; $nMDF = $ide->getElementsByTagName('nMDF')->item(0)->nodeValue; $serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue; $dhEmi = !empty($ide->getElementsByTagName('dhEmi')->item(0)->nodeValue) ? $ide->getElementsByTagName('dhEmi')->item(0)->nodeValue : $ide->getElementsByTagName('dEmi')->item(0)->nodeValue; $data = date('d/m/Y', DateTime::convertSefazTimeToTimestamp($dhEmi)); $vCarga = $tot->getElementsByTagName('vCarga')->item(0)->nodeValue; $xNome = ''; $this->msgHtml = $this->zRenderTemplate($xNome, $data, $nMDF, $serie, $vCarga, $razao); $this->msgTxt = utf8_encode(Html2Text::convert(utf8_decode($this->msgHtml))); return "MDFe n. {$nMDF} - {$razao}"; }
/** * zMontaMessagem * * @param string $pathFile */ protected function zMontaMessagem($pathFile) { $dom = new Dom(); $dom->loadXMLFile($pathFile); $infNFe = $dom->getNode('infNFe', 0); $ide = $infNFe->getElementsByTagName('ide')->item(0); $dest = $infNFe->getElementsByTagName('dest')->item(0); $emit = $infNFe->getElementsByTagName('emit')->item(0); $icmsTot = $infNFe->getElementsByTagName('ICMSTot')->item(0); $razao = $emit->getElementsByTagName('xNome')->item(0)->nodeValue; $nNF = $ide->getElementsByTagName('nNF')->item(0)->nodeValue; $serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue; $xNome = $dest->getElementsByTagName('xNome')->item(0)->nodeValue; $dhEmi = !empty($ide->getElementsByTagName('dhEmi')->item(0)->nodeValue) ? $ide->getElementsByTagName('dhEmi')->item(0)->nodeValue : $ide->getElementsByTagName('dEmi')->item(0)->nodeValue; $data = date('d/m/Y', DateTime::convertSefazTimeToTimestamp($dhEmi)); $vNF = $icmsTot->getElementsByTagName('vNF')->item(0)->nodeValue; $this->aMail[] = !empty($dest->getElementsByTagName('email')->item(0)->nodeValue) ? $dest->getElementsByTagName('email')->item(0)->nodeValue : ''; //pega os emails que existirem em obsCont $infAdic = $infNFe->getElementsByTagName('infAdic')->item(0); if (!empty($infAdic)) { $obsConts = $infAdic->getElementsByTagName('obsCont'); foreach ($obsConts as $obsCont) { if (strtoupper($obsCont->getAttribute('xCampo')) === 'EMAIL') { $this->aMail[] = $obsCont->getElementsByTagName('xTexto')->item(0)->nodeValue; } } } $this->msgHtml = $this->zRenderTemplate($xNome, $data, $nNF, $serie, $vNF, $razao); $this->msgTxt = utf8_encode(Html2Text::convert(utf8_decode($this->msgHtml))); return "NFe n. {$nNF} - {$razao}"; }
/** * Process the email properties correctly to be able to correctly configure the SwiftMailer Email object. * @param string|array $to To whom the notification should be sent. * @param array $message All the email properties. * * @return array The array of all allowed properties for SwiftMailer Email object. */ protected function processEmailProperties($to, $message) { // If the notification is only a string, create it a email as the notification being the subject of the email $original_message = $message; if (!is_array($message)) { $message = array('subject' => substr(strip_tags($original_message), 0, 75) . '...', 'body' => $original_message); } //Checks is the message is a HTML message, and generate the plain text version of it. $body = $message['body']; if (!is_array($body)) { $body_encode = mb_detect_encoding($body); $body = htmlspecialchars_decode(htmlentities($body, ENT_NOQUOTES, $body_encode), ENT_NOQUOTES); $message['body'] = array('html' => $body, 'plain' => Html2Text::convert($body)); } // Merge and Overwrite configurations. $message = array_merge($this->config, $message); $recipients = $this->handleEmailRecipientsProperties($to); $body = $this->handleEmailBodyProperties($message); $advancedProperties = $this->handleAdvancedEmailProperties($message); $emailProperties = array_merge($recipients, $body, $advancedProperties); return $emailProperties; }
public function send(Participant $participant) { $mail = $this->_em->getRepository('BdeReventBundle:Mail')->findOneBy(array('type' => $participant->getType())); $content = $this->generateMailFromData($mail, $participant); $this->mailgun->sendMessage($this->mailgun_domain, array('from' => 'GALA 20 <*****@*****.**>', 'to' => $participant->getEmail(), 'subject' => $content['subject'], 'text' => Html2Text::convert($content['body']), 'html' => $content['body'])); }
public function sendToSubscriber($campaign, $subscriber, $use_massmailer = false) { $html = $campaign->renderForSubscriber($subscriber); $text = Html2Text::convert(str_replace(array("\r", "\n"), "", $html)); if ($use_massmailer) { if (!defined('MAILHOST')) { define('MAILHOST', 'okaytick.com'); } $backup_original_mailer = Mail::getSwiftMailer(); // Setup our other mailer if needed $transport = Swift_SmtpTransport::newInstance('okaytick.com', 25); // 'ssl', 'tls' $transport->setUsername('okaytick'); $transport->setPassword('2e76656dcba6562ffeb68d524de1053e90a229a0'); // Any other mailer configuration stuff needed... $massmailer = new Swift_Mailer($transport); Mail::setSwiftMailer($massmailer); $to_email = array($subscriber->email => ''); // $to_email = array('*****@*****.**' => 'Dominic O\'Brien', '*****@*****.**' => 'Clive Whittaker', '*****@*****.**' => 'Clive Whittaker', '*****@*****.**' => 'Clive Whittaker', '*****@*****.**' => 'Clive Whittaker'); // $to_email = array('*****@*****.**' => ''); $subject = $campaign->subject; $subject = str_replace("oktick.com", "okaytick.com", $subject); $subject = str_replace("https", "http", $subject); $html = str_replace("oktick.com", "okaytick.com", $html); $html = str_replace("https", "http", $html); $text = str_replace("oktick.com", "okaytick.com", $text); $text = str_replace("https", "http", $text); // Create the message $message = Swift_Message::newInstance()->setReturnPath('*****@*****.**')->setSubject($subject)->setFrom(array('*****@*****.**' => 'OKTicK Search Ltd'))->setTo($to_email)->setBody($html, 'text/html')->addPart($text, 'text/plain')->setId($subscriber->id . ".8938145113." . time() . "@aruba1.generated")->setReplyTo(array('*****@*****.**' => 'OKTicK Search Ltd'))->setSender(array('*****@*****.**' => 'OKTicK Search Ltd'))->setPriority(3); $numSent = $massmailer->send($message); // Restore our original mailer Mail::setSwiftMailer($backup_original_mailer); return $numSent; /***************************************************************************************** $setUrl = "http://okaytick.com/api.php"; $setReturnPath = "*****@*****.**"; $setFrom = array('*****@*****.**' => 'OKTicK Search Ltd'); $setId = $subscriber->id . ".".$campaign->id."." . time() ."@oktick.generated"; $setReplyTo = array('*****@*****.**' => 'OKTicK Search Ltd'); $setSender = array('*****@*****.**' => 'OKTicK Search Ltd'); $setPriority = 3; // normal $query = array( "setReturnPath"=>$setReturnPath, "setSubject" => $campaign->subject, "setFrom" => $setFrom, "setTo" => $subscriber->email, "setBody" => $html, "addPart" => $text, "setId" => $setId, "setReplyTo" => $setReplyTo, "setBody" => $html, "setSender" => $setSender, "setPriority" => $setPriority, ); $query = json_encode($query); $query = $this->encrypt ( $query ,$this->ENCRYPTION_KEY ); $query = urlencode($query); $query = '__PAYLOAD__=' . $query; $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL,$setUrl); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$query); $result = curl_exec($ch); return $result; *****************************************************************************************/ } else { Mail::rawTo($subscriber, ['html' => $html, 'text' => $text], function ($message) use($campaign, $subscriber) { $message->subject($campaign->subject)->setReturnPath('*****@*****.**')->setFrom(array('*****@*****.**' => 'OKTicK Search Ltd'))->setReplyTo(array('*****@*****.**' => 'OKTicK Search Ltd'))->setId($subscriber->id . "." . time() . "@oktick.generated"); }); return true; } }
static function compile($template_id, $arguments = array()) { $template = false; $subject = false; $html_template = false; $template_dir = \Openclerk\Config::get('emails_templates', __DIR__ . '/../../../../emails'); if (file_exists($template_dir . "/" . $template_id . ".txt")) { $template = file_get_contents($template_dir . "/" . $template_id . ".txt"); // strip out the subject from the text $template = explode("\n", $template, 2); $subject = $template[0]; $template = $template[1]; } if (file_exists($template_dir . "/" . $template_id . ".html")) { $html_template = file_get_contents($template_dir . "/" . $template_id . ".html"); if (file_exists($template_dir . "/" . "layout.html")) { $html_layout_template = file_get_contents($template_dir . "/" . "layout.html"); $html_template = \Openclerk\Templates::replace($html_layout_template, array('content' => $html_template)); } // strip out the title from the html $matches = false; if (preg_match("#<title>(.+)</title>#im", $html_template, $matches)) { $subject = $matches[1]; } $html_template = preg_replace("#<title>.+</title>#im", "", $html_template); } if (!$template) { if ($html_template) { // use html2text to generate the text body automatically $template = \Html2Text\Html2Text::convert($html_template); } else { throw new MailerException("Email template '{$template_id}' did not exist within '{$template_dir}'"); } } // replace variables $template = \Openclerk\Templates::replace($template, $arguments); $subject = \Openclerk\Templates::replace($subject, $arguments); if ($html_template) { $html_template = \Openclerk\Templates::replace($html_template, $arguments); } // inline CSS? if (file_exists($template_dir . "/layout.css")) { $css = file_get_contents($template_dir . "/layout.css"); // custom CSS? if (\Openclerk\Config::get("emails_additional_css", false)) { $css .= file_get_contents(\Openclerk\Config::get("emails_additional_css", false)); } $emogrifier = new \Pelago\Emogrifier(); $emogrifier->setHtml($html_template); $emogrifier->setCss($css); $html_template = $emogrifier->emogrify(); } return array('subject' => $subject, 'html' => $html_template, 'text' => $template); }
/** * Convert the html of the body to text * * @access protected * @param string $html * @return string */ protected function createTextBody($html) { $text = \Html2Text\Html2Text::convert($html); return $text; }