1
0
mirror of https://git.tt-rss.org/git/tt-rss.git synced 2025-12-16 03:45:57 +00:00

remove PHPMailer and related directives from config.php-dist; add pluggable Mailer class

This commit is contained in:
Andrew Dolgov
2018-11-22 14:45:14 +03:00
parent 643d1919cc
commit 57932e1837
62 changed files with 108 additions and 6758 deletions

View File

@@ -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);
if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo);
$rc = $mailer->mail(["to" => $line["login"] . " <" . $line["email"] . ">",
"subject" => DIGEST_SUBJECT,
"message" => $digest_text,
"message_html" => $digest]);
//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);
}
}
}

View File

@@ -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;

43
classes/mailer.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
class Mailer {
// TODO: support HTML mail (i.e. MIME messages)
private $last_error = "Unable to send mail: check local configuration.";
function mail($params) {
$to = $params["to"];
$subject = $params["subject"];
$message = $params["message"];
$message_html = $params["message_html"];
$from = $params["from"] ? $params["from"] : SMTP_FROM_NAME . " <" . SMTP_FROM_ADDRESS . ">";
$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;
}
}

View File

@@ -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;

View File

@@ -287,8 +287,6 @@ class Pref_Users extends Handler_Protected {
print_notice(T_sprintf("Sending new password of user <b>%s</b> to <b>%s</b>", $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 "</div>"; #container
}
}
}

View File

@@ -1,63 +0,0 @@
<?php
/* @class ttrssMailer
* @brief A TTRSS extension to the PHPMailer class
* Configures default values through the __construct() function
* @author Derek Murawsky
* @version .1 (alpha)
*
*/
require_once 'lib/phpmailer/class.phpmailer.php';
require_once 'lib/phpmailer/class.smtp.php';
require_once "config.php";
class ttrssMailer extends PHPMailer {
//define all items that we want to override with defaults in PHPMailer
public $From = SMTP_FROM_ADDRESS;
public $FromName = SMTP_FROM_NAME;
public $CharSet = "UTF-8";
public $PluginDir = "lib/phpmailer/";
public $ContentType = "text/html"; //default email type is HTML
function __construct() {
$this->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;
}
}