init.php 2.0 KB

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