Mailer.php 2.8 KB

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