/** * @param Document\Newsletter $newsletterDocument * @param SendingParamContainer|null $sendingContainer * @param string|null $hostUrl * @return Mail */ public static function prepareMail(Document\Newsletter $newsletterDocument, SendingParamContainer $sendingContainer = null, $hostUrl = null) { $mail = new Mail(); $mail->setIgnoreDebugMode(true); if (\Pimcore\Config::getSystemConfig()->newsletter->usespecific) { $mail->init("newsletter"); } if (!Tool::getHostUrl() && $hostUrl) { $mail->setHostUrl($hostUrl); } $mail->setDocument($newsletterDocument); if ($sendingContainer && $sendingContainer->getParams()) { $mail->setParams($sendingContainer->getParams()); } $contentHTML = $mail->getBodyHtmlRendered(); $contentText = $mail->getBodyTextRendered(); // render the document and rewrite the links (if analytics is enabled) if ($newsletterDocument->getEnableTrackingParameters()) { if ($contentHTML) { include_once "simple_html_dom.php"; $html = str_get_html($contentHTML); if ($html) { $links = $html->find("a"); foreach ($links as $link) { if (preg_match("/^(mailto)/", trim(strtolower($link->href)))) { continue; } $glue = "?"; if (strpos($link->href, "?")) { $glue = "&"; } $link->href = $link->href . $glue . "utm_source=" . $newsletterDocument->getTrackingParameterSource() . "&utm_medium=" . $newsletterDocument->getTrackingParameterMedium() . "&utm_campaign=" . $newsletterDocument->getTrackingParameterName(); } $contentHTML = $html->save(); $html->clear(); unset($html); } $mail->setBodyHtml($contentHTML); } } $mail->setBodyHtml($contentHTML); $mail->setBodyText($contentText); $mail->setSubject($mail->getSubjectRendered()); return $mail; }
/** * @param MailClient $mail * @return Model\Tool\Email\Log */ public static function logEmail(MailClient $mail) { $emailLog = new Model\Tool\Email\Log(); $document = $mail->getDocument(); if ($document instanceof Model\Document) { $emailLog->setDocumentId($document->getId()); } $emailLog->setRequestUri(htmlspecialchars($_SERVER['REQUEST_URI'])); $emailLog->setParams($mail->getParams()); $emailLog->setSentDate(time()); $subject = $mail->getSubjectRendered(); if (0 === strpos($subject, '=?')) { $mbIntEnc = mb_internal_encoding(); mb_internal_encoding($mail->getCharset()); $subject = mb_decode_mimeheader($subject); mb_internal_encoding($mbIntEnc); } $emailLog->setSubject($subject); $mailFrom = $mail->getFrom(); if ($mailFrom) { $emailLog->setFrom($mailFrom); } else { $defaultFrom = $mail->getDefaultFrom(); $tmpString = $defaultFrom['email']; if ($defaultFrom['name']) { $tmpString .= " (" . $defaultFrom["name"] . ")"; } $emailLog->setFrom($tmpString); } $html = $mail->getBodyHtml(); if ($html instanceof \Zend_Mime_Part) { $emailLog->setBodyHtml($html->getRawContent()); } $text = $mail->getBodyText(); if ($text instanceof \Zend_Mime_Part) { $emailLog->setBodyText($text->getRawContent()); } $temporaryStorage = $mail->getTemporaryStorage(); foreach (['To', 'Cc', 'Bcc'] as $key) { if (isset($temporaryStorage[$key]) && is_array($temporaryStorage[$key])) { if (method_exists($emailLog, 'set' . $key)) { $emailLog->{"set{$key}"}(self::formatDebugReceivers($temporaryStorage[$key])); } } } $emailLog->save(); return $emailLog; }