PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/core/Mail.php

https://github.com/CodeYellowBV/piwik
PHP | 103 lines | 65 code | 8 blank | 30 comment | 14 complexity | d64e6373075b7cb5e3b1c96d29b995cd MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Piwik\Plugins\CoreAdminHome\CustomLogo;
  11. use Zend_Mail;
  12. /**
  13. * Class for sending mails, for more information see:
  14. * {@link http://framework.zend.com/manual/en/zend.mail.html}
  15. *
  16. * @see Zend_Mail, libs/Zend/Mail.php
  17. * @api
  18. */
  19. class Mail extends Zend_Mail
  20. {
  21. /**
  22. * Constructor.
  23. *
  24. * @param string $charset charset, defaults to utf-8
  25. */
  26. public function __construct($charset = 'utf-8')
  27. {
  28. parent::__construct($charset);
  29. $this->initSmtpTransport();
  30. }
  31. public function setDefaultFromPiwik()
  32. {
  33. $customLogo = new CustomLogo();
  34. $fromEmailName = $customLogo->isEnabled()
  35. ? Piwik::translate('CoreHome_WebAnalyticsReports')
  36. : Piwik::translate('ScheduledReports_PiwikReports');
  37. $fromEmailAddress = Config::getInstance()->General['noreply_email_address'];
  38. $this->setFrom($fromEmailAddress, $fromEmailName);
  39. }
  40. /**
  41. * Sets the sender.
  42. *
  43. * @param string $email Email address of the sender.
  44. * @param null|string $name Name of the sender.
  45. * @return Zend_Mail
  46. */
  47. public function setFrom($email, $name = null)
  48. {
  49. $hostname = Config::getInstance()->mail['defaultHostnameIfEmpty'];
  50. $piwikHost = Url::getCurrentHost($hostname);
  51. // If known Piwik URL, use it instead of "localhost"
  52. $piwikUrl = SettingsPiwik::getPiwikUrl();
  53. $url = parse_url($piwikUrl);
  54. if (isset($url['host'])
  55. && $url['host'] != 'localhost'
  56. && $url['host'] != '127.0.0.1'
  57. ) {
  58. $piwikHost = $url['host'];
  59. }
  60. $email = str_replace('{DOMAIN}', $piwikHost, $email);
  61. return parent::setFrom($email, $name);
  62. }
  63. /**
  64. * @return void
  65. */
  66. private function initSmtpTransport()
  67. {
  68. $mailConfig = Config::getInstance()->mail;
  69. if (empty($mailConfig['host'])
  70. || $mailConfig['transport'] != 'smtp'
  71. ) {
  72. return;
  73. }
  74. $smtpConfig = array();
  75. if (!empty($mailConfig['type']))
  76. $smtpConfig['auth'] = strtolower($mailConfig['type']);
  77. if (!empty($mailConfig['username']))
  78. $smtpConfig['username'] = $mailConfig['username'];
  79. if (!empty($mailConfig['password']))
  80. $smtpConfig['password'] = $mailConfig['password'];
  81. if (!empty($mailConfig['encryption']))
  82. $smtpConfig['ssl'] = $mailConfig['encryption'];
  83. $tr = new \Zend_Mail_Transport_Smtp($mailConfig['host'], $smtpConfig);
  84. Mail::setDefaultTransport($tr);
  85. ini_set("smtp_port", $mailConfig['port']);
  86. }
  87. public function send($transport = NULL)
  88. {
  89. if (defined('PIWIK_TEST_MODE')) { // hack
  90. Piwik::postTestEvent("Test.Mail.send", array($this));
  91. } else {
  92. return parent::send($transport);
  93. }
  94. }
  95. }