ttrss/classes/mailer.php

53 lines
1.7 KiB
PHP
Raw Normal View History

<?php
class Mailer {
2018-11-22 14:55:37 +01:00
// TODO: support HTML mail (i.e. MIME messages)
2018-11-22 14:55:37 +01:00
private $last_error = "Unable to send mail: check local configuration.";
2018-11-22 14:55:37 +01:00
function mail($params) {
2018-11-22 14:55:37 +01:00
$to_name = $params["to_name"];
$to_address = $params["to_address"];
$subject = $params["subject"];
$message = $params["message"];
$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"] ?? [];
$from_combined = $from_name ? "$from_name <$from_address>" : $from_address;
2018-11-22 14:55:37 +01:00
$to_combined = $to_name ? "$to_name <$to_address>" : $to_address;
2021-02-22 20:35:27 +01:00
if (Config::get(Config::LOG_SENT_MAIL))
2021-02-25 13:49:30 +01:00
Logger::log(E_USER_NOTICE, "Sending mail from $from_combined to $to_combined [$subject]: $message");
2018-11-22 14:55:37 +01: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 14:55:37 +01:00
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) {
$rc = $p->hook_send_mail($this, $params);
2018-11-22 14:55:37 +01:00
if ($rc == 1)
return $rc;
2018-11-22 14:55:37 +01:00
if ($rc == -1)
return 0;
}
2018-12-17 09:55:21 +01:00
$headers = [ "From: $from_combined", "Content-Type: text/plain; charset=UTF-8" ];
2018-11-22 14:36:10 +01:00
2018-11-22 14:55:37 +01:00
return mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers)));
}
2018-11-22 14:55:37 +01:00
function set_error($message) {
$this->last_error = $message;
}
2018-11-22 14:55:37 +01:00
function error() {
return $this->last_error;
}
}