]>
Commit | Line | Data |
---|---|---|
57932e18 AD |
1 | <?php |
2 | class Mailer { | |
3 | // TODO: support HTML mail (i.e. MIME messages) | |
4 | ||
5 | private $last_error = "Unable to send mail: check local configuration."; | |
6 | ||
7 | function mail($params) { | |
8 | ||
55bf4bc1 AD |
9 | $to_name = $params["to_name"]; |
10 | $to_address = $params["to_address"]; | |
57932e18 AD |
11 | $subject = $params["subject"]; |
12 | $message = $params["message"]; | |
13 | $message_html = $params["message_html"]; | |
55bf4bc1 AD |
14 | $from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME; |
15 | $from_address = $params["from_address"] ? $params["from_address"] : SMTP_FROM_ADDRESS; | |
16 | ||
57932e18 AD |
17 | $additional_headers = $params["headers"] ? $params["headers"] : []; |
18 | ||
55bf4bc1 AD |
19 | $from_combined = $from_name ? "$to_name <$to_address>" : $to_address; |
20 | $to_combined = $to_name ? "$to_name <$to_address>" : $to_address; | |
57932e18 | 21 | |
55bf4bc1 | 22 | Logger::get()->log("Sending mail from $from_combined to $to_combined <$to_address> [$subject]: $message"); |
57932e18 AD |
23 | |
24 | // HOOK_SEND_MAIL plugin instructions: | |
25 | // 1. return 1 or true if mail is handled | |
26 | // 2. return -1 if there's been a fatal error and no further action is allowed | |
27 | // 3. any other return value will allow cycling to the next handler and, eventually, to default mail() function | |
28 | // 4. set error message if needed via passed Mailer instance function set_error() | |
29 | ||
30 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) { | |
31 | $rc = $p->hook_send_mail($this, $params); | |
32 | ||
ac5e55c2 | 33 | if ($rc == 1) |
57932e18 | 34 | return $rc; |
ac5e55c2 AD |
35 | |
36 | if ($rc == -1) | |
37 | return 0; | |
57932e18 AD |
38 | } |
39 | ||
55bf4bc1 AD |
40 | $headers[] = "From: $from_combined"; |
41 | ||
42 | return mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers))); | |
57932e18 AD |
43 | } |
44 | ||
45 | function set_error($message) { | |
46 | $this->last_error = $message; | |
47 | } | |
48 | ||
55bf4bc1 | 49 | function error() { |
57932e18 AD |
50 | return $this->last_error; |
51 | } | |
52 | } |