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