summaryrefslogtreecommitdiff
path: root/classes/mailer.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2018-11-22 14:45:14 +0300
committerAndrew Dolgov <[email protected]>2018-11-22 14:45:14 +0300
commit57932e183745bada9c6183056597cb5276f68d10 (patch)
tree7d64a815dd4bbf40dec51ee95be16c4ef7f7a212 /classes/mailer.php
parent643d1919cc27a80aff424970b337e83be72720d1 (diff)
remove PHPMailer and related directives from config.php-dist; add pluggable Mailer class
Diffstat (limited to 'classes/mailer.php')
-rw-r--r--classes/mailer.php43
1 files changed, 43 insertions, 0 deletions
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 @@
+<?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;
+ }
+}