mailer.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. class Mailer {
  3. // TODO: support HTML mail (i.e. MIME messages)
  4. private $last_error = "Unable to send mail: check local configuration.";
  5. function mail($params) {
  6. $to_name = $params["to_name"];
  7. $to_address = $params["to_address"];
  8. $subject = $params["subject"];
  9. $message = $params["message"];
  10. $message_html = $params["message_html"];
  11. $from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME;
  12. $from_address = $params["from_address"] ? $params["from_address"] : SMTP_FROM_ADDRESS;
  13. $additional_headers = $params["headers"] ? $params["headers"] : [];
  14. $from_combined = $from_name ? "$to_name <$to_address>" : $to_address;
  15. $to_combined = $to_name ? "$to_name <$to_address>" : $to_address;
  16. Logger::get()->log("Sending mail from $from_combined to $to_combined <$to_address> [$subject]: $message");
  17. // HOOK_SEND_MAIL plugin instructions:
  18. // 1. return 1 or true if mail is handled
  19. // 2. return -1 if there's been a fatal error and no further action is allowed
  20. // 3. any other return value will allow cycling to the next handler and, eventually, to default mail() function
  21. // 4. set error message if needed via passed Mailer instance function set_error()
  22. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) {
  23. $rc = $p->hook_send_mail($this, $params);
  24. if ($rc == 1)
  25. return $rc;
  26. if ($rc == -1)
  27. return 0;
  28. }
  29. $headers[] = "From: $from_combined";
  30. return mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers)));
  31. }
  32. function set_error($message) {
  33. $this->last_error = $message;
  34. }
  35. function error() {
  36. return $this->last_error;
  37. }
  38. }