Transport.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Mail; |
| 11 | |
| 12 | use PHPMailer\PHPMailer\PHPMailer; |
| 13 | use PHPMailer\PHPMailer\SMTP; |
| 14 | use Piwik\Config; |
| 15 | use Piwik\Container\StaticContainer; |
| 16 | use Piwik\Mail; |
| 17 | use Piwik\Piwik; |
| 18 | use Piwik\Version; |
| 19 | |
| 20 | class Transport |
| 21 | { |
| 22 | /** |
| 23 | * Sends the given mail |
| 24 | * |
| 25 | * @param Mail $mail |
| 26 | * @return bool |
| 27 | * @throws \DI\NotFoundException |
| 28 | * @throws \PHPMailer\PHPMailer\Exception |
| 29 | */ |
| 30 | public function send(Mail $mail) |
| 31 | { |
| 32 | $phpMailer = new PHPMailer(true); |
| 33 | |
| 34 | PHPMailer::$validator = 'pcre8'; |
| 35 | $phpMailer->CharSet = PHPMailer::CHARSET_UTF8; |
| 36 | $phpMailer->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE; |
| 37 | $phpMailer->XMailer = ' '; |
| 38 | // avoid triggering automated (vacation) responses |
| 39 | $phpMailer->addCustomHeader('Auto-Submitted', 'yes'); |
| 40 | $phpMailer->setLanguage(StaticContainer::get('Piwik\Translation\Translator')->getCurrentLanguage()); |
| 41 | $this->initSmtpTransport($phpMailer); |
| 42 | |
| 43 | if ($mail->isSmtpDebugEnabled()) { |
| 44 | $phpMailer->SMTPDebug = SMTP::DEBUG_SERVER; |
| 45 | } |
| 46 | |
| 47 | $phpMailer->Subject = $mail->getSubject(); |
| 48 | |
| 49 | $htmlContent = $mail->getBodyHtml(); |
| 50 | $textContent = $mail->getBodyText(); |
| 51 | |
| 52 | if (!empty($htmlContent)) { |
| 53 | $phpMailer->msgHTML($htmlContent); |
| 54 | |
| 55 | if (!empty($textContent)) { |
| 56 | $phpMailer->AltBody = $textContent; |
| 57 | } |
| 58 | } else { |
| 59 | $phpMailer->Body = $textContent; |
| 60 | } |
| 61 | |
| 62 | $phpMailer->setFrom($mail->getFrom(), $mail->getFromName()); |
| 63 | |
| 64 | foreach ($mail->getRecipients() as $address => $name) { |
| 65 | $phpMailer->addAddress($address, $name); |
| 66 | } |
| 67 | |
| 68 | foreach ($mail->getBccs() as $address => $name) { |
| 69 | $phpMailer->addBCC($address, $name); |
| 70 | } |
| 71 | |
| 72 | foreach ($mail->getReplyTos() as $address => $name) { |
| 73 | $phpMailer->addReplyTo($address, $name); |
| 74 | } |
| 75 | |
| 76 | foreach ($mail->getAttachments() as $attachment) { |
| 77 | if (!empty($attachment['cid'])) { |
| 78 | $phpMailer->addStringEmbeddedImage( |
| 79 | $attachment['content'], |
| 80 | $attachment['cid'], |
| 81 | $attachment['filename'], |
| 82 | PHPMailer::ENCODING_BASE64, |
| 83 | $attachment['mimetype'] |
| 84 | ); |
| 85 | } else { |
| 86 | $phpMailer->addStringAttachment( |
| 87 | $attachment['content'], |
| 88 | $attachment['filename'], |
| 89 | PHPMailer::ENCODING_BASE64, |
| 90 | $attachment['mimetype'] |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (defined('PIWIK_TEST_MODE')) { // hack |
| 96 | /** |
| 97 | * @ignore |
| 98 | * @internal |
| 99 | */ |
| 100 | Piwik::postTestEvent("Test.Mail.send", array($phpMailer)); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | return $phpMailer->send(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return void |
| 109 | */ |
| 110 | private function initSmtpTransport(PHPMailer $phpMailer) |
| 111 | { |
| 112 | $mailConfig = Config::getInstance()->mail; |
| 113 | |
| 114 | if (empty($mailConfig['host']) |
| 115 | || $mailConfig['transport'] != 'smtp' |
| 116 | ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $phpMailer->isSMTP(); |
| 121 | |
| 122 | if (!empty($mailConfig['type'])) { |
| 123 | $phpMailer->SMTPAuth = true; |
| 124 | $phpMailer->AuthType = strtoupper($mailConfig['type']); |
| 125 | } |
| 126 | |
| 127 | if (!empty($mailConfig['username'])) { |
| 128 | $phpMailer->Username = $mailConfig['username']; |
| 129 | } |
| 130 | |
| 131 | if (!empty($mailConfig['password'])) { |
| 132 | $phpMailer->Password = $mailConfig['password']; |
| 133 | } |
| 134 | |
| 135 | if (!empty($mailConfig['encryption'])) { |
| 136 | if (strtolower($mailConfig['encryption']) === 'none') { |
| 137 | $phpMailer->SMTPAutoTLS = false; // force using no encryption |
| 138 | } else { |
| 139 | $phpMailer->SMTPSecure = $mailConfig['encryption']; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!empty($mailConfig['port'])) { |
| 144 | $phpMailer->Port = $mailConfig['port']; |
| 145 | } |
| 146 | |
| 147 | $phpMailer->Host = trim($mailConfig['host']); |
| 148 | } |
| 149 | } |
| 150 |