2018-11-22 11:45:14 +00:00
|
|
|
<?php
|
|
|
|
class Mailer {
|
2021-11-12 21:17:31 +00:00
|
|
|
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+
|
|
|
|
/** @var string */
|
|
|
|
private $last_error = "";
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2021-11-12 06:16:18 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $params
|
|
|
|
* @return bool|int bool if the default mail function handled the request, otherwise an int as described in Mailer#mail()
|
|
|
|
*/
|
|
|
|
function mail(array $params) {
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2021-03-15 11:45:50 +00:00
|
|
|
$to_name = $params["to_name"] ?? "";
|
2018-11-22 13:55:37 +00:00
|
|
|
$to_address = $params["to_address"];
|
|
|
|
$subject = $params["subject"];
|
|
|
|
$message = $params["message"];
|
2021-03-02 10:20:41 +00:00
|
|
|
$message_html = $params["message_html"] ?? "";
|
|
|
|
$from_name = $params["from_name"] ?? Config::get(Config::SMTP_FROM_NAME);
|
|
|
|
$from_address = $params["from_address"] ?? Config::get(Config::SMTP_FROM_ADDRESS);
|
|
|
|
$additional_headers = $params["headers"] ?? [];
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2019-01-09 13:49:35 +00:00
|
|
|
$from_combined = $from_name ? "$from_name <$from_address>" : $from_address;
|
2018-11-22 13:55:37 +00:00
|
|
|
$to_combined = $to_name ? "$to_name <$to_address>" : $to_address;
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2021-02-22 19:35:27 +00:00
|
|
|
if (Config::get(Config::LOG_SENT_MAIL))
|
2021-02-25 12:49:30 +00:00
|
|
|
Logger::log(E_USER_NOTICE, "Sending mail from $from_combined to $to_combined [$subject]: $message");
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2018-11-22 13:55:37 +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()
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2021-11-16 06:19:12 +00:00
|
|
|
$hooks_tried = 0;
|
|
|
|
|
2018-11-22 13:55:37 +00:00
|
|
|
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) {
|
|
|
|
$rc = $p->hook_send_mail($this, $params);
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2018-11-22 13:55:37 +00:00
|
|
|
if ($rc == 1)
|
|
|
|
return $rc;
|
2018-11-22 13:39:10 +00:00
|
|
|
|
2018-11-22 13:55:37 +00:00
|
|
|
if ($rc == -1)
|
|
|
|
return 0;
|
2021-11-16 06:19:12 +00:00
|
|
|
|
|
|
|
++$hooks_tried;
|
2018-11-22 13:55:37 +00:00
|
|
|
}
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2022-05-24 02:09:46 +00:00
|
|
|
$headers = [ "From: $from_combined" ];
|
|
|
|
|
2022-06-06 18:05:15 +00:00
|
|
|
if($message_html){
|
|
|
|
$headers[]="MIME-Version: 1.0";
|
|
|
|
$headers[]="Content-Type: text/html; charset=UTF-8";
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$headers[]="Content-Type: text/plain; charset=UTF-8";
|
2022-05-24 02:09:46 +00:00
|
|
|
}
|
2018-11-22 13:36:10 +00:00
|
|
|
|
2021-03-03 11:00:18 +00:00
|
|
|
$rc = mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers)));
|
|
|
|
|
|
|
|
if (!$rc) {
|
2021-11-16 06:19:12 +00:00
|
|
|
$this->set_error(error_get_last()['message'] ?? T_sprintf("Unknown error while sending mail. Hooks tried: %d.", $hooks_tried));
|
2021-03-03 11:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rc;
|
2018-11-22 13:55:37 +00:00
|
|
|
}
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2021-11-12 06:16:18 +00:00
|
|
|
function set_error(string $message): void {
|
2018-11-22 13:55:37 +00:00
|
|
|
$this->last_error = $message;
|
2021-03-03 11:00:18 +00:00
|
|
|
user_error("Error sending mail: $message", E_USER_WARNING);
|
2018-11-22 13:55:37 +00:00
|
|
|
}
|
2018-11-22 11:45:14 +00:00
|
|
|
|
2021-11-12 06:16:18 +00:00
|
|
|
function error(): string {
|
2018-11-22 13:55:37 +00:00
|
|
|
return $this->last_error;
|
|
|
|
}
|
2018-11-22 11:45:14 +00:00
|
|
|
}
|