Mailer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\Exception;
  4. /**
  5. * Allow to send emails.
  6. *
  7. * The Minz_Mailer class must be inherited by classes under app/Mailers.
  8. * They work similarly to the ActionControllers in the way they have a view to
  9. * which you can pass params (eg. $this->view->foo = 'bar').
  10. *
  11. * The view file is not determined automatically, so you have to select one
  12. * with, for instance:
  13. *
  14. * ```
  15. * $this->view->_path('user_mailer/email_need_validation.txt.php')
  16. * ```
  17. *
  18. * Minz_Mailer uses the PHPMailer library under the hood. The latter requires
  19. * PHP >= 5.5 to work. If you instantiate a Minz_Mailer with PHP < 5.5, a
  20. * warning will be logged.
  21. *
  22. * The email is sent by calling the `mail` method.
  23. */
  24. class Minz_Mailer {
  25. /**
  26. * The view attached to the mailer.
  27. * You should set its file with `$this->view->_path($path)`
  28. *
  29. * @var Minz_View
  30. */
  31. protected $view;
  32. private $mailer;
  33. private $smtp_config;
  34. private $debug_level;
  35. /**
  36. * Constructor.
  37. */
  38. public function __construct () {
  39. $this->view = new Minz_View();
  40. $this->view->_layout(false);
  41. $this->view->attributeParams();
  42. $conf = Minz_Configuration::get('system');
  43. $this->mailer = $conf->mailer;
  44. $this->smtp_config = $conf->smtp;
  45. // According to https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging#debug-levels
  46. // we should not use debug level above 2 unless if we have big trouble
  47. // to connect.
  48. if ($conf->environment === 'development') {
  49. $this->debug_level = 2;
  50. } else {
  51. $this->debug_level = 0;
  52. }
  53. }
  54. /**
  55. * Send an email.
  56. *
  57. * @param string $to The recipient of the email
  58. * @param string $subject The subject of the email
  59. *
  60. * @return bool true on success, false if a SMTP error happens
  61. */
  62. public function mail($to, $subject) {
  63. ob_start();
  64. $this->view->render();
  65. $body = ob_get_contents();
  66. ob_end_clean();
  67. PHPMailer::$validator = 'html5';
  68. $mail = new PHPMailer(true);
  69. try {
  70. // Server settings
  71. $mail->SMTPDebug = $this->debug_level;
  72. $mail->Debugoutput = 'error_log';
  73. if ($this->mailer === 'smtp') {
  74. $mail->isSMTP();
  75. $mail->Hostname = $this->smtp_config['hostname'];
  76. $mail->Host = $this->smtp_config['host'];
  77. $mail->SMTPAuth = $this->smtp_config['auth'];
  78. $mail->Username = $this->smtp_config['username'];
  79. $mail->Password = $this->smtp_config['password'];
  80. $mail->SMTPSecure = $this->smtp_config['secure'];
  81. $mail->Port = $this->smtp_config['port'];
  82. } else {
  83. $mail->isMail();
  84. }
  85. // Recipients
  86. $mail->setFrom($this->smtp_config['from']);
  87. $mail->addAddress($to);
  88. // Content
  89. $mail->isHTML(false);
  90. $mail->CharSet = 'utf-8';
  91. $mail->Subject = $subject;
  92. $mail->Body = $body;
  93. $mail->send();
  94. return true;
  95. } catch (Exception $e) {
  96. Minz_Log::error('Minz_Mailer cannot send a message: ' . $mail->ErrorInfo);
  97. return false;
  98. }
  99. }
  100. }