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.php | |
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.php')
-rw-r--r-- | core/Mail.php | 272 |
1 files changed, 220 insertions, 52 deletions
diff --git a/core/Mail.php b/core/Mail.php index c930aaa3f6..8c6330715b 100644 --- a/core/Mail.php +++ b/core/Mail.php @@ -12,28 +12,46 @@ use Piwik\Container\StaticContainer; use Piwik\Email\ContentGenerator; use Piwik\Plugins\CoreAdminHome\CustomLogo; use Piwik\Translation\Translator; -use Zend_Mail; /** - * Class for sending mails, for more information see: - * {@link http://framework.zend.com/manual/en/zend.mail.html} + * Class for sending mails * - * @see Zend_Mail, libs/Zend/Mail.php * @api */ -class Mail extends Zend_Mail +class Mail { + protected $fromEmail = ''; + protected $fromName = ''; + protected $bodyHTML = ''; + protected $bodyText = ''; + protected $subject = ''; + protected $recipients = []; + protected $replyTos = []; + protected $bccs = []; + protected $attachments = []; + protected $smtpDebug = false; + + public function __construct() + { + } + /** - * Constructor. + * Sets the sender. * - * @param string $charset charset, defaults to utf-8 + * @param string $email Email address of the sender. + * @param null|string $name Name of the sender. */ - public function __construct($charset = 'utf-8') + public function setFrom($email, $name = null) { - parent::__construct($charset); - $this->initSmtpTransport(); + $this->fromName = $name; + $this->fromEmail = $this->parseDomainPlaceholderAsPiwikHostName($email); } + /** + * Sets the default sender + * + * @throws \DI\NotFoundException + */ public function setDefaultFromPiwik() { $customLogo = new CustomLogo(); @@ -54,6 +72,26 @@ class Mail extends Zend_Mail } /** + * Returns the address the mail will be sent from + * + * @return string + */ + public function getFrom() + { + return $this->fromEmail; + } + + /** + * Returns the address the mail will be sent from + * + * @return string + */ + public function getFromName() + { + return $this->fromName; + } + + /** * @param View|string $body * @throws \DI\NotFoundException */ @@ -61,56 +99,181 @@ class Mail extends Zend_Mail { $contentGenerator = StaticContainer::get(ContentGenerator::class); $bodyHtml = $contentGenerator->generateHtmlContent($body); - $this->setBodyHtml($bodyHtml); + $this->bodyHTML = $bodyHtml; } /** - * Sets the sender. + * Sets the HTML part of the mail * - * @param string $email Email address of the sender. - * @param null|string $name Name of the sender. - * @return Zend_Mail + * @param $html */ - public function setFrom($email, $name = null) + public function setBodyHtml($html) + { + $this->bodyHTML = $html; + } + + /** + * Sets the text part of the mail. + * If bodyHtml is set, this will be used as alternative text part + * + * @param $txt + */ + public function setBodyText($txt) + { + $this->bodyText = $txt; + } + + /** + * Returns html content of the mail + * + * @return string + */ + public function getBodyHtml() + { + return $this->bodyHTML; + } + + /** + * Returns text content of the mail + * + * @return string + */ + public function getBodyText() + { + return $this->bodyText; + } + + /** + * Sets the subject of the mail + * + * @param $subject + */ + public function setSubject($subject) + { + $subject = $this->sanitiseString($subject); + $this->subject = $subject; + } + + /** + * Return the subject of the mail + * + * @return string + */ + public function getSubject() + { + return $this->subject; + } + + /** + * Adds a recipient + * + * @param string $address + * @param string $name + */ + public function addTo($address, $name = '') + { + $this->recipients[$address] = $name; + } + + /** + * Returns the list of recipients + * + * @return array + */ + public function getRecipients() + { + return $this->recipients; + } + + /** + * Add Bcc address + * + * @param string $email + * @param null|string $name + */ + public function addBcc($email, $name = null) + { + $this->bccs[$email] = $name; + } + + /** + * Returns the list of bcc addresses + * + * @return array + */ + public function getBccs() + { + return $this->bccs; + } + + /** + * Removes all recipients and bccs from the list + */ + public function clearAllRecipients() { - return parent::setFrom( - $this->parseDomainPlaceholderAsPiwikHostName($email), - $name - ); + $this->recipients = []; + $this->bcc = []; } /** - * Set Reply-To Header + * Add Reply-To address * * @param string $email * @param null|string $name - * @return Zend_Mail + */ + public function addReplyTo($email, $name = null) + { + $this->replyTos[$this->parseDomainPlaceholderAsPiwikHostName($email)] = $name; + } + + /** + * Sets the reply to address (all previously added addresses will be removed) + * + * @param string $email + * @param string $name */ public function setReplyTo($email, $name = null) { - return parent::setReplyTo( - $this->parseDomainPlaceholderAsPiwikHostName($email), - $name - ); + $this->replyTos = []; + $this->addReplyTo($email, $name); } /** - * @return void + * Returns the list of reply to addresses + * + * @return array */ - private function initSmtpTransport() + public function getReplyTos() { - $tr = StaticContainer::get('Zend_Mail_Transport_Abstract'); - if (empty($tr)) { - return; - } + return $this->replyTos; + } + + public function addAttachment($body, $mimeType = '', $filename = null, $cid = null) + { + $filename = $this->sanitiseString($filename); + $this->attachments[] = [ + 'content' => $body, + 'filename' => $filename, + 'mimetype' => $mimeType, + 'cid' => $cid + ]; + } - Mail::setDefaultTransport($tr); + public function getAttachments() + { + return $this->attachments; } - public function send($transport = null) + /** + * Sends the mail + * + * @return bool + * @throws \DI\NotFoundException + */ + public function send() { if (!$this->shouldSendMail()) { - return $this; + return false; } $mail = $this; @@ -119,33 +282,38 @@ class Mail extends Zend_Mail * This event is posted right before an email is sent. You can use it to customize the email by, for example, replacing * the subject/body, changing the from address, etc. * - * @param Mail $this The Mail instance that is about to be sent. + * @param Mail $mail The Mail instance that is about to be sent. */ Piwik::postEvent('Mail.send', [$mail]); - if (defined('PIWIK_TEST_MODE')) { // hack - /** - * @ignore - * @deprecated - */ - Piwik::postTestEvent("Test.Mail.send", array($this)); - } else { - return parent::send($transport); - } + return StaticContainer::get('Piwik\Mail\Transport')->send($mail); } - public function createAttachment($body, $mimeType = null, $disposition = null, $encoding = null, $filename = null) + /** + * Enables SMTP debugging + * + * @param bool $smtpDebug + */ + public function setSmtpDebug($smtpDebug = true) { - $filename = $this->sanitiseString($filename); - return parent::createAttachment($body, $mimeType, $disposition, $encoding, $filename); + $this->smtpDebug = $smtpDebug; } - public function setSubject($subject) + /** + * Returns whether SMTP debugging is enabled or not + * + * @return bool + */ + public function isSmtpDebugEnabled() { - $subject = $this->sanitiseString($subject); - return parent::setSubject($subject); + return $this->smtpDebug; } + /** + * Returns the hostname mails will be sent from + * + * @return string + */ public function getMailHost() { $hostname = Config::getInstance()->mail['defaultHostnameIfEmpty']; @@ -185,7 +353,7 @@ class Mail extends Zend_Mail * @param $string * @return mixed */ - function sanitiseString($string) + public function sanitiseString($string) { $search = array('–', '’'); $replace = array('-', '\''); |