2018-11-22 11:45:14 +00:00
|
|
|
<?php
|
|
|
|
class Mailer {
|
|
|
|
// TODO: support HTML mail (i.e. MIME messages)
|
|
|
|
|
|
|
|
private $last_error = "Unable to send mail: check local configuration.";
|
|
|
|
|
|
|
|
function mail($params) {
|
|
|
|
|
2018-11-22 13:36:10 +00:00
|
|
|
$to_name = $params["to_name"];
|
|
|
|
$to_address = $params["to_address"];
|
2018-11-22 11:45:14 +00:00
|
|
|
$subject = $params["subject"];
|
|
|
|
$message = $params["message"];
|
|
|
|
$message_html = $params["message_html"];
|
2018-11-22 13:36:10 +00:00
|
|
|
$from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME;
|
|
|
|
$from_address = $params["from_address"] ? $params["from_address"] : SMTP_FROM_ADDRESS;
|
|
|
|
|
2018-11-22 11:45:14 +00:00
|
|
|
$additional_headers = $params["headers"] ? $params["headers"] : [];
|
|
|
|
|
2018-11-22 13:36:10 +00:00
|
|
|
$from_combined = $from_name ? "$to_name <$to_address>" : $to_address;
|
|
|
|
$to_combined = $to_name ? "$to_name <$to_address>" : $to_address;
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2018-11-22 13:36:10 +00:00
|
|
|
Logger::get()->log("Sending mail from $from_combined to $to_combined <$to_address> [$subject]: $message");
|
2018-11-22 11:45:14 +00:00
|
|
|
|
|
|
|
// HOOK_SEND_MAIL plugin instructions:
|
|
|
|
// 1. return 1 or true if mail is handled
|
|
|
|
// 2. return -1 if there's been a fatal error and no further action is allowed
|
|
|
|
// 3. any other return value will allow cycling to the next handler and, eventually, to default mail() function
|
|
|
|
// 4. set error message if needed via passed Mailer instance function set_error()
|
|
|
|
|
|
|
|
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) {
|
|
|
|
$rc = $p->hook_send_mail($this, $params);
|
|
|
|
|
|
|
|
if ($rc == 1 || $rc == -1)
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
|
2018-11-22 13:36:10 +00:00
|
|
|
$headers[] = "From: $from_combined";
|
|
|
|
|
|
|
|
return mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers)));
|
2018-11-22 11:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function set_error($message) {
|
|
|
|
$this->last_error = $message;
|
|
|
|
}
|
|
|
|
|
2018-11-22 13:36:10 +00:00
|
|
|
function error() {
|
2018-11-22 11:45:14 +00:00
|
|
|
return $this->last_error;
|
|
|
|
}
|
|
|
|
}
|