1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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']);
}
}
|