Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mail.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. return parent::setFrom(
  50. $this->parseDomainPlaceholderAsPiwikHostName($email),
  51. $name
  52. );
  53. }
  54. /**
  55. * Set Reply-To Header
  56. *
  57. * @param string $email
  58. * @param null|string $name
  59. * @return Zend_Mail
  60. */
  61. public function setReplyTo($email, $name = null)
  62. {
  63. return parent::setReplyTo(
  64. $this->parseDomainPlaceholderAsPiwikHostName($email),
  65. $name
  66. );
  67. }
  68. /**
  69. * @return void
  70. */
  71. private function initSmtpTransport()
  72. {
  73. $mailConfig = Config::getInstance()->mail;
  74. if (empty($mailConfig['host'])
  75. || $mailConfig['transport'] != 'smtp'
  76. ) {
  77. return;
  78. }
  79. $smtpConfig = array();
  80. if (!empty($mailConfig['type']))
  81. $smtpConfig['auth'] = strtolower($mailConfig['type']);
  82. if (!empty($mailConfig['username']))
  83. $smtpConfig['username'] = $mailConfig['username'];
  84. if (!empty($mailConfig['password']))
  85. $smtpConfig['password'] = $mailConfig['password'];
  86. if (!empty($mailConfig['encryption']))
  87. $smtpConfig['ssl'] = $mailConfig['encryption'];
  88. $tr = new \Zend_Mail_Transport_Smtp($mailConfig['host'], $smtpConfig);
  89. Mail::setDefaultTransport($tr);
  90. ini_set("smtp_port", $mailConfig['port']);
  91. }
  92. public function send($transport = NULL)
  93. {
  94. if (defined('PIWIK_TEST_MODE')) { // hack
  95. Piwik::postTestEvent("Test.Mail.send", array($this));
  96. } else {
  97. return parent::send($transport);
  98. }
  99. }
  100. /**
  101. * @param string $email
  102. * @return string
  103. */
  104. protected function parseDomainPlaceholderAsPiwikHostName($email)
  105. {
  106. $hostname = Config::getInstance()->mail['defaultHostnameIfEmpty'];
  107. $piwikHost = Url::getCurrentHost($hostname);
  108. // If known Piwik URL, use it instead of "localhost"
  109. $piwikUrl = SettingsPiwik::getPiwikUrl();
  110. $url = parse_url($piwikUrl);
  111. if ($this->isHostDefinedAndNotLocal($url)) {
  112. $piwikHost = $url['host'];
  113. }
  114. return str_replace('{DOMAIN}', $piwikHost, $email);
  115. }
  116. /**
  117. * @param array $url
  118. * @return bool
  119. */
  120. protected function isHostDefinedAndNotLocal($url)
  121. {
  122. return isset($url['host']) && !in_array($url['host'], array('localhost', '127.0.0.1'), true);
  123. }
  124. }