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