diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-05-14 10:52:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 10:52:48 +0300 |
commit | 0187b71729484a9f173d978239ff0a3cb37a5d7b (patch) | |
tree | 770d517a1268808f85abe291371b84b03a0b3d14 /core/Mail | |
parent | fb1be4d50ad1f5c3c2c2915fc42caf1f4fd31a2d (diff) |
Replace Zend Mail with PHPMailer (#15891)
* require composer package phpmailer/phpmailer
* change mail class to use phpmailer instead of Zend_Mail
* removes Zend_Mail and Zend_Mime
* submodule
* fix tests
* enable smtp debug for test mail command
* fix embedded images
* set language for phpmailer
* apply some review feedback
* removes Zend_Validate (#15907)
* don't inline file without cid
* submodule update
* fix test
* decouples Mail class from PHPMailer and moves sending mails to new transport layer
* submodule
* cleanup
* unset xmailer header
* simplify mail attachment handling
* adds methods to add bcc recipients
* change implementation of setReplyTo
Diffstat (limited to 'core/Mail')
-rw-r--r-- | core/Mail/Transport.php | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/core/Mail/Transport.php b/core/Mail/Transport.php new file mode 100644 index 0000000000..de2a2e8425 --- /dev/null +++ b/core/Mail/Transport.php @@ -0,0 +1,143 @@ +<?php +/** + * Matomo - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Mail; + +use PHPMailer\PHPMailer\PHPMailer; +use PHPMailer\PHPMailer\SMTP; +use Piwik\Config; +use Piwik\Container\StaticContainer; +use Piwik\Mail; +use Piwik\Piwik; +use Piwik\Version; + +class Transport +{ + /** + * Sends the given mail + * + * @param Mail $mail + * @return bool + * @throws \DI\NotFoundException + * @throws \PHPMailer\PHPMailer\Exception + */ + public function send(Mail $mail) + { + $phpMailer = new PHPMailer(true); + + PHPMailer::$validator = 'pcre8'; + $phpMailer->CharSet = PHPMailer::CHARSET_UTF8; + $phpMailer->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE; + $phpMailer->XMailer = ' '; + $phpMailer->setLanguage(StaticContainer::get('Piwik\Translation\Translator')->getCurrentLanguage()); + $this->initSmtpTransport($phpMailer); + + if ($mail->isSmtpDebugEnabled()) { + $phpMailer->SMTPDebug = SMTP::DEBUG_SERVER; + } + + $phpMailer->Subject = $mail->getSubject(); + + $htmlContent = $mail->getBodyHtml(); + $textContent = $mail->getBodyText(); + + if (!empty($htmlContent)) { + $phpMailer->msgHTML($htmlContent); + + if (!empty($textContent)) { + $phpMailer->AltBody = $textContent; + } + } else { + $phpMailer->Body = $textContent; + } + + $phpMailer->setFrom($mail->getFrom(), $mail->getFromName()); + + foreach ($mail->getRecipients() as $address => $name) { + $phpMailer->addAddress($address, $name); + } + + foreach ($mail->getBccs() as $address => $name) { + $phpMailer->addBCC($address, $name); + } + + foreach ($mail->getReplyTos() as $address => $name) { + $phpMailer->addReplyTo($address, $name); + } + + foreach ($mail->getAttachments() as $attachment) { + if (!empty($attachment['cid'])) { + $phpMailer->addStringEmbeddedImage( + $attachment['content'], + $attachment['cid'], + $attachment['filename'], + PHPMailer::ENCODING_BASE64, + $attachment['mimetype'] + ); + } else { + $phpMailer->addStringAttachment( + $attachment['content'], + $attachment['filename'], + PHPMailer::ENCODING_BASE64, + $attachment['mimetype'] + ); + } + } + + if (defined('PIWIK_TEST_MODE')) { // hack + /** + * @ignore + * @deprecated + */ + Piwik::postTestEvent("Test.Mail.send", array($phpMailer)); + return true; + } + + return $phpMailer->send(); + } + + /** + * @return void + */ + private function initSmtpTransport($phpMailer) + { + $mailConfig = Config::getInstance()->mail; + + if (empty($mailConfig['host']) + || $mailConfig['transport'] != 'smtp' + ) { + return; + } + + $phpMailer->isSMTP(); + + if (!empty($mailConfig['type'])) { + $phpMailer->SMTPAuth = true; + $phpMailer->AuthType = strtoupper($mailConfig['type']); + } + + if (!empty($mailConfig['username'])) { + $phpMailer->Username = $mailConfig['username']; + } + + if (!empty($mailConfig['password'])) { + $phpMailer->Password = $mailConfig['password']; + } + + if (!empty($mailConfig['encryption'])) { + $phpMailer->SMTPSecure = $mailConfig['encryption']; + } + + if (!empty($mailConfig['port'])) { + $phpMailer->Port = $mailConfig['port']; + } + + $phpMailer->Host = trim($mailConfig['host']); + } +}
\ No newline at end of file |