init.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. class mailer_smtp extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Sends mail via SMTP using PHPMailer. Read README.txt before enabling.",
  7. "fox",
  8. 1,
  9. "https://git.tt-rss.org/fox/ttrss-mailer-smtp");
  10. }
  11. function init($host) {
  12. $this->host = $host;
  13. $host->add_hook($host::HOOK_SEND_MAIL, $this);
  14. }
  15. function hook_send_mail($mailer, $params) {
  16. if (defined('SMTP_SERVER') && SMTP_SERVER) {
  17. $phpmailer = new PHPMailer\PHPMailer\PHPMailer();
  18. $phpmailer->isSMTP();
  19. $pair = explode(":", SMTP_SERVER, 2);
  20. $phpmailer->Host = $pair[0];
  21. $phpmailer->Port = $pair[1];
  22. $phpmailer->CharSet = "UTF-8";
  23. if (!$phpmailer->Port) $phpmailer->Port = 25;
  24. if (defined('SMTP_LOGIN') && SMTP_LOGIN) {
  25. $phpmailer->SMTPAuth = true;
  26. $phpmailer->Username = SMTP_LOGIN;
  27. $phpmailer->Password = SMTP_PASSWORD;
  28. }
  29. if (defined('SMTP_SECURE') && SMTP_SECURE) {
  30. $phpmailer->SMTPSecure = SMTP_SECURE;
  31. } else {
  32. $phpmailer->SMTPAutoTLS = false;
  33. }
  34. if (defined('SMTP_SKIP_CERT_CHECKS') && SMTP_SKIP_CERT_CHECKS) {
  35. $phpmailer->SMTPOptions = array(
  36. 'ssl' => array(
  37. 'verify_peer' => false,
  38. 'verify_peer_name' => false,
  39. 'allow_self_signed' => true
  40. )
  41. );
  42. } elseif (defined('SMTP_CA_FILE') && SMTP_CA_FILE) {
  43. $phpmailer->SMTPOptions = array(
  44. 'ssl' => array(
  45. 'cafile' => SMTP_CA_FILE
  46. )
  47. );
  48. }
  49. $from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME;
  50. $from_address = $params["from_address"] ? $params["from_address"] : SMTP_FROM_ADDRESS;
  51. $phpmailer->setFrom($from_address, $from_name);
  52. $phpmailer->addAddress($params["to_address"], $params["to_name"]);
  53. $phpmailer->Subject = $params["subject"];
  54. $phpmailer->CharSet = "UTF-8";
  55. if ($params["message_html"]) {
  56. $phpmailer->msgHTML($params["message_html"]);
  57. $phpmailer->AltBody = $params["message"];
  58. } else {
  59. $phpmailer->Body = $params["message"];
  60. }
  61. foreach ($params['headers'] as $header) {
  62. $phpmailer->addCustomHeader($header);
  63. }
  64. $rc = $phpmailer->send();
  65. if (!$rc)
  66. $mailer->set_error($rc . " " . $phpmailer->ErrorInfo);
  67. return $rc;
  68. }
  69. }
  70. function api_version() {
  71. return 2;
  72. }
  73. }
  74. ?>