From 57932e183745bada9c6183056597cb5276f68d10 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 22 Nov 2018 14:45:14 +0300 Subject: remove PHPMailer and related directives from config.php-dist; add pluggable Mailer class --- classes/digest.php | 15 ++++++----- classes/handler/public.php | 11 ++++---- classes/mailer.php | 43 +++++++++++++++++++++++++++++++ classes/pluginhost.php | 1 + classes/pref/users.php | 14 +++++------ classes/ttrssmailer.php | 63 ---------------------------------------------- 6 files changed, 64 insertions(+), 83 deletions(-) create mode 100644 classes/mailer.php delete mode 100644 classes/ttrssmailer.php (limited to 'classes') diff --git a/classes/digest.php b/classes/digest.php index 75dda4984..641fbe97f 100644 --- a/classes/digest.php +++ b/classes/digest.php @@ -11,8 +11,6 @@ class Digest */ static function send_headlines_digests($debug = false) { - require_once 'classes/ttrssmailer.php'; - $user_limit = 15; // amount of users to process (e.g. emails to send out) $limit = 1000; // maximum amount of headlines to include @@ -56,11 +54,16 @@ class Digest if ($headlines_count > 0) { - $mail = new ttrssMailer(); + $mailer = new Mailer(); + + //$rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text); - $rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text); + $rc = $mailer->mail(["to" => $line["login"] . " <" . $line["email"] . ">", + "subject" => DIGEST_SUBJECT, + "message" => $digest_text, + "message_html" => $digest]); - if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo); + //if (!$rc && $debug) _debug("ERROR: " . $mailer->lastError()); if ($debug) _debug("RC=$rc"); @@ -198,4 +201,4 @@ class Digest return array($tmp, $headlines_count, $affected_ids, $tmp_t); } -} \ No newline at end of file +} diff --git a/classes/handler/public.php b/classes/handler/public.php index dbed3fee6..cadb9873d 100755 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -777,7 +777,6 @@ class Handler_Public extends Handler { $resetpass_link = get_self_url_prefix() . "/public.php?op=forgotpass&hash=" . $resetpass_token . "&login=" . urlencode($login); - require_once 'classes/ttrssmailer.php'; require_once "lib/MiniTemplator.class.php"; $tpl = new MiniTemplator; @@ -793,13 +792,13 @@ class Handler_Public extends Handler { $tpl->generateOutputToString($message); - $mail = new ttrssMailer(); + $mailer = new Mailer(); - $rc = $mail->quickMail($email, $login, - __("[tt-rss] Password reset request"), - $message, false); + $rc = $mailer->mail(["to" => "$login <$email>", + "subject" => __("[tt-rss] Password reset request"), + "message" => $message]); - if (!$rc) print_error($mail->ErrorInfo); + if (!$rc) print_error($mailer->error()); $resetpass_token_full = time() . ":" . $resetpass_token; diff --git a/classes/mailer.php b/classes/mailer.php new file mode 100644 index 000000000..ae22776d4 --- /dev/null +++ b/classes/mailer.php @@ -0,0 +1,43 @@ +"; + $additional_headers = $params["headers"] ? $params["headers"] : []; + + $headers[] = "From: $from"; + + Logger::get()->log("Sending mail from $from to $to [$subject]: $message"); + + // 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; + } + + return mail($to, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers))); + } + + function set_error($message) { + $this->last_error = $message; + } + + function error($value) { + return $this->last_error; + } +} diff --git a/classes/pluginhost.php b/classes/pluginhost.php index 5c545d18b..f40705dd1 100755 --- a/classes/pluginhost.php +++ b/classes/pluginhost.php @@ -57,6 +57,7 @@ class PluginHost { const HOOK_FEED_BASIC_INFO = 36; const HOOK_SEND_LOCAL_FILE = 37; const HOOK_UNSUBSCRIBE_FEED = 38; + const HOOK_SEND_MAIL = 39; const KIND_ALL = 1; const KIND_SYSTEM = 2; diff --git a/classes/pref/users.php b/classes/pref/users.php index ab8ac775b..ad48e7a72 100644 --- a/classes/pref/users.php +++ b/classes/pref/users.php @@ -287,8 +287,6 @@ class Pref_Users extends Handler_Protected { print_notice(T_sprintf("Sending new password of user %s to %s", $login, $email)); } - require_once 'classes/ttrssmailer.php'; - if ($email) { require_once "lib/MiniTemplator.class.php"; @@ -305,13 +303,13 @@ class Pref_Users extends Handler_Protected { $tpl->generateOutputToString($message); - $mail = new ttrssMailer(); + $mailer = new Mailer(); - $rc = $mail->quickMail($email, $login, - __("[tt-rss] Password change notification"), - $message, false); + $rc = $mailer->mail(["to" => "$login <$email>", + "subject" => __("[tt-rss] Password change notification"), + "message" => $message]); - if (!$rc) print_error($mail->ErrorInfo); + if (!$rc) print_error($mailer->error()); } } @@ -458,4 +456,4 @@ class Pref_Users extends Handler_Protected { print ""; #container } - } \ No newline at end of file + } diff --git a/classes/ttrssmailer.php b/classes/ttrssmailer.php deleted file mode 100644 index cd10b7cb3..000000000 --- a/classes/ttrssmailer.php +++ /dev/null @@ -1,63 +0,0 @@ -SetLanguage("en", "lib/phpmailer/language/"); - - if (SMTP_SERVER) { - $pair = explode(":", SMTP_SERVER, 2); - $this->Mailer = "smtp"; - - $this->Host = $pair[0]; - $this->Port = $pair[1]; - - if (!$this->Port) $this->Port = 25; - } else { - $this->Host = ''; - $this->Port = ''; - } - - - //if SMTP_LOGIN is specified, set credentials and enable auth - if(SMTP_LOGIN){ - $this->SMTPAuth = true; - $this->Username = SMTP_LOGIN; - $this->Password = SMTP_PASSWORD; - } - if(SMTP_SECURE) - $this->SMTPSecure = SMTP_SECURE; - } - /* @brief a simple mail function to send email using the defaults - * This will send an HTML email using the configured defaults - * @param $toAddress A string with the recipients email address - * @param $toName A string with the recipients name - * @param $subject A string with the emails subject - * @param $body A string containing the body of the email - */ - public function quickMail ($toAddress, $toName, $subject, $body, $altbody=""){ - $this->addAddress($toAddress, $toName); - $this->Subject = $subject; - $this->Body = $body; - $this->IsHTML($altbody != ''); - $rc=$this->send(); - return $rc; - } -} \ No newline at end of file -- cgit v1.2.3-54-g00ecf