Mailer.php 3.0 KB

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