Mailer.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Mail;
  23. use OCP\IConfig;
  24. use OCP\Mail\IMailer;
  25. use OCP\ILogger;
  26. /**
  27. * Class Mailer provides some basic functions to create a mail message that can be used in combination with
  28. * \OC\Mail\Message.
  29. *
  30. * Example usage:
  31. *
  32. * $mailer = \OC::$server->getMailer();
  33. * $message = $mailer->createMessage();
  34. * $message->setSubject('Your Subject');
  35. * $message->setFrom(array('cloud@domain.org' => 'ownCloud Notifier');
  36. * $message->setTo(array('recipient@domain.org' => 'Recipient');
  37. * $message->setBody('The message text');
  38. * $mailer->send($message);
  39. *
  40. * This message can then be passed to send() of \OC\Mail\Mailer
  41. *
  42. * @package OC\Mail
  43. */
  44. class Mailer implements IMailer {
  45. /** @var \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport Cached transport */
  46. private $instance = null;
  47. /** @var IConfig */
  48. private $config;
  49. /** @var ILogger */
  50. private $logger;
  51. /** @var \OC_Defaults */
  52. private $defaults;
  53. /**
  54. * @param IConfig $config
  55. * @param ILogger $logger
  56. * @param \OC_Defaults $defaults
  57. */
  58. function __construct(IConfig $config,
  59. ILogger $logger,
  60. \OC_Defaults $defaults) {
  61. $this->config = $config;
  62. $this->logger = $logger;
  63. $this->defaults = $defaults;
  64. }
  65. /**
  66. * Creates a new message object that can be passed to send()
  67. *
  68. * @return Message
  69. */
  70. public function createMessage() {
  71. return new Message(new \Swift_Message());
  72. }
  73. /**
  74. * Send the specified message. Also sets the from address to the value defined in config.php
  75. * if no-one has been passed.
  76. *
  77. * @param Message $message Message to send
  78. * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
  79. * therefore should be considered
  80. * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
  81. * has been supplied.)
  82. */
  83. public function send(Message $message) {
  84. $debugMode = $this->config->getSystemValue('mail_smtpdebug', false);
  85. if (sizeof($message->getFrom()) === 0) {
  86. $message->setFrom([\OCP\Util::getDefaultEmailAddress($this->defaults->getName())]);
  87. }
  88. $failedRecipients = [];
  89. $mailer = $this->getInstance();
  90. // Enable logger if debug mode is enabled
  91. if($debugMode) {
  92. $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
  93. $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
  94. }
  95. $mailer->send($message->getSwiftMessage(), $failedRecipients);
  96. // Debugging logging
  97. $logMessage = sprintf('Sent mail to "%s" with subject "%s"', print_r($message->getTo(), true), $message->getSubject());
  98. $this->logger->debug($logMessage, ['app' => 'core']);
  99. if($debugMode && isset($mailLogger)) {
  100. $this->logger->debug($mailLogger->dump(), ['app' => 'core']);
  101. }
  102. return $failedRecipients;
  103. }
  104. /**
  105. * Checks if an e-mail address is valid
  106. *
  107. * @param string $email Email address to be validated
  108. * @return bool True if the mail address is valid, false otherwise
  109. */
  110. public function validateMailAddress($email) {
  111. return \Swift_Validate::email($this->convertEmail($email));
  112. }
  113. /**
  114. * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
  115. *
  116. * FIXME: Remove this once SwiftMailer supports IDN
  117. *
  118. * @param string $email
  119. * @return string Converted mail address if `idn_to_ascii` exists
  120. */
  121. protected function convertEmail($email) {
  122. if (!function_exists('idn_to_ascii') || strpos($email, '@') === false) {
  123. return $email;
  124. }
  125. list($name, $domain) = explode('@', $email, 2);
  126. $domain = idn_to_ascii($domain);
  127. return $name.'@'.$domain;
  128. }
  129. /**
  130. * Returns whatever transport is configured within the config
  131. *
  132. * @return \Swift_SmtpTransport|\Swift_SendmailTransport|\Swift_MailTransport
  133. */
  134. protected function getInstance() {
  135. if (!is_null($this->instance)) {
  136. return $this->instance;
  137. }
  138. switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
  139. case 'smtp':
  140. $this->instance = $this->getSMTPInstance();
  141. break;
  142. case 'sendmail':
  143. // FIXME: Move into the return statement but requires proper testing
  144. // for SMTP and mail as well. Thus not really doable for a
  145. // minor release.
  146. $this->instance = \Swift_Mailer::newInstance($this->getSendMailInstance());
  147. break;
  148. default:
  149. $this->instance = $this->getMailInstance();
  150. break;
  151. }
  152. return $this->instance;
  153. }
  154. /**
  155. * Returns the SMTP transport
  156. *
  157. * @return \Swift_SmtpTransport
  158. */
  159. protected function getSmtpInstance() {
  160. $transport = \Swift_SmtpTransport::newInstance();
  161. $transport->setTimeout($this->config->getSystemValue('mail_smtptimeout', 10));
  162. $transport->setHost($this->config->getSystemValue('mail_smtphost', '127.0.0.1'));
  163. $transport->setPort($this->config->getSystemValue('mail_smtpport', 25));
  164. if ($this->config->getSystemValue('mail_smtpauth', false)) {
  165. $transport->setUsername($this->config->getSystemValue('mail_smtpname', ''));
  166. $transport->setPassword($this->config->getSystemValue('mail_smtppassword', ''));
  167. $transport->setAuthMode($this->config->getSystemValue('mail_smtpauthtype', 'LOGIN'));
  168. }
  169. $smtpSecurity = $this->config->getSystemValue('mail_smtpsecure', '');
  170. if (!empty($smtpSecurity)) {
  171. $transport->setEncryption($smtpSecurity);
  172. }
  173. $transport->start();
  174. return $transport;
  175. }
  176. /**
  177. * Returns the sendmail transport
  178. *
  179. * @return \Swift_SendmailTransport
  180. */
  181. protected function getSendMailInstance() {
  182. switch ($this->config->getSystemValue('mail_smtpmode', 'php')) {
  183. case 'qmail':
  184. $binaryPath = '/var/qmail/bin/sendmail';
  185. break;
  186. default:
  187. $binaryPath = '/usr/sbin/sendmail';
  188. break;
  189. }
  190. return \Swift_SendmailTransport::newInstance($binaryPath . ' -bs');
  191. }
  192. /**
  193. * Returns the mail transport
  194. *
  195. * @return \Swift_MailTransport
  196. */
  197. protected function getMailInstance() {
  198. return \Swift_MailTransport::newInstance();
  199. }
  200. }