Mailer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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')
  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. /**
  33. * Constructor.
  34. *
  35. * If PHP version is < 5.5, a warning is logged.
  36. */
  37. public function __construct () {
  38. if (version_compare(PHP_VERSION, '5.5') < 0) {
  39. Minz_Log::warning('Minz_Mailer cannot be used with a version of PHP < 5.5.');
  40. }
  41. $this->view = new Minz_View();
  42. $this->view->_layout(false);
  43. $this->view->attributeParams();
  44. $conf = Minz_Configuration::get('system');
  45. $this->mailer = $conf->mailer;
  46. $this->smtp_config = $conf->smtp;
  47. // According to https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging#debug-levels
  48. // we should not use debug level above 2 unless if we have big trouble
  49. // to connect.
  50. if ($conf->environment === 'development') {
  51. $this->debug_level = 2;
  52. } else {
  53. $this->debug_level = 0;
  54. }
  55. }
  56. /**
  57. * Send an email.
  58. *
  59. * @param string $to The recipient of the email
  60. * @param string $subject The subject of the email
  61. *
  62. * @return bool true on success, false if a SMTP error happens
  63. */
  64. public function mail($to, $subject) {
  65. ob_start();
  66. $this->view->render();
  67. $body = ob_get_contents();
  68. ob_end_clean();
  69. PHPMailer::$validator = 'html5';
  70. $mail = new PHPMailer(true);
  71. try {
  72. // Server settings
  73. $mail->SMTPDebug = $this->debug_level;
  74. $mail->Debugoutput = 'error_log';
  75. if ($this->mailer === 'smtp') {
  76. $mail->isSMTP();
  77. $mail->Hostname = $this->smtp_config['hostname'];
  78. $mail->Host = $this->smtp_config['host'];
  79. $mail->SMTPAuth = $this->smtp_config['auth'];
  80. $mail->Username = $this->smtp_config['username'];
  81. $mail->Password = $this->smtp_config['password'];
  82. $mail->SMTPSecure = $this->smtp_config['secure'];
  83. $mail->Port = $this->smtp_config['port'];
  84. } else {
  85. $mail->isMail();
  86. }
  87. // Recipients
  88. $mail->setFrom($this->smtp_config['from']);
  89. $mail->addAddress($to);
  90. // Content
  91. $mail->isHTML(false);
  92. $mail->CharSet = 'utf-8';
  93. $mail->Subject = $subject;
  94. $mail->Body = $body;
  95. $mail->send();
  96. return true;
  97. } catch (Exception $e) {
  98. Minz_Log::error('Minz_Mailer cannot send a message: ' . $mail->ErrorInfo);
  99. return false;
  100. }
  101. }
  102. }