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
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik;
use Piwik\Plugins\CoreAdminHome\CustomLogo;
use Zend_Mail;
/**
* Class for sending mails, for more information see:
* {@link http://framework.zend.com/manual/en/zend.mail.html}
*
* @see Zend_Mail, libs/Zend/Mail.php
* @api
*/
class Mail extends Zend_Mail
{
/**
* Constructor.
*
* @param string $charset charset, defaults to utf-8
*/
public function __construct($charset = 'utf-8')
{
parent::__construct($charset);
$this->initSmtpTransport();
}
public function setDefaultFromPiwik()
{
$customLogo = new CustomLogo();
$fromEmailName = $customLogo->isEnabled()
? Piwik::translate('CoreHome_WebAnalyticsReports')
: Piwik::translate('ScheduledReports_PiwikReports');
$fromEmailAddress = Config::getInstance()->General['noreply_email_address'];
$this->setFrom($fromEmailAddress, $fromEmailName);
}
/**
* Sets the sender.
*
* @param string $email Email address of the sender.
* @param null|string $name Name of the sender.
* @return Zend_Mail
*/
public function setFrom($email, $name = null)
{
$hostname = Config::getInstance()->mail['defaultHostnameIfEmpty'];
$piwikHost = Url::getCurrentHost($hostname);
// If known Piwik URL, use it instead of "localhost"
$piwikUrl = SettingsPiwik::getPiwikUrl();
$url = parse_url($piwikUrl);
if (isset($url['host'])
&& $url['host'] != 'localhost'
&& $url['host'] != '127.0.0.1'
) {
$piwikHost = $url['host'];
}
$email = str_replace('{DOMAIN}', $piwikHost, $email);
return parent::setFrom($email, $name);
}
/**
* @return void
*/
private function initSmtpTransport()
{
$mailConfig = Config::getInstance()->mail;
if (empty($mailConfig['host'])
|| $mailConfig['transport'] != 'smtp'
) {
return;
}
$smtpConfig = array();
if (!empty($mailConfig['type']))
$smtpConfig['auth'] = strtolower($mailConfig['type']);
if (!empty($mailConfig['username']))
$smtpConfig['username'] = $mailConfig['username'];
if (!empty($mailConfig['password']))
$smtpConfig['password'] = $mailConfig['password'];
if (!empty($mailConfig['encryption']))
$smtpConfig['ssl'] = $mailConfig['encryption'];
$tr = new \Zend_Mail_Transport_Smtp($mailConfig['host'], $smtpConfig);
Mail::setDefaultTransport($tr);
ini_set("smtp_port", $mailConfig['port']);
}
public function send($transport = NULL)
{
if (defined('PIWIK_TEST_MODE')) { // hack
Piwik::postTestEvent("Test.Mail.send", array($this));
} else {
return parent::send($transport);
}
}
}
|