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 | public function mail($params) { |
||
8 | |||
9 | $to_name = $params["to_name"]; |
||
10 | $to_address = $params["to_address"]; |
||
11 | $subject = $params["subject"]; |
||
12 | $message = $params["message"]; |
||
13 | $from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
14 | $from_address = $params["from_address"] ? $params["from_address"] : SMTP_FROM_ADDRESS; |
||
0 ignored issues
–
show
|
|||
15 | |||
16 | $additional_headers = $params["headers"] ? $params["headers"] : []; |
||
17 | |||
18 | $from_combined = $from_name ? "$from_name <$from_address>" : $from_address; |
||
19 | $to_combined = $to_name ? "$to_name <$to_address>" : $to_address; |
||
20 | |||
21 | if (defined('_LOG_SENT_MAIL') && _LOG_SENT_MAIL) { |
||
0 ignored issues
–
show
|
|||
22 | Logger::get()->log("Sending mail from $from_combined to $to_combined [$subject]: $message"); |
||
23 | } |
||
24 | |||
25 | // HOOK_SEND_MAIL plugin instructions: |
||
26 | // 1. return 1 or true if mail is handled |
||
27 | // 2. return -1 if there's been a fatal error and no further action is allowed |
||
28 | // 3. any other return value will allow cycling to the next handler and, eventually, to default mail() function |
||
29 | // 4. set error message if needed via passed Mailer instance function set_error() |
||
30 | |||
31 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) { |
||
32 | $rc = $p->hook_send_mail($this, $params); |
||
33 | |||
34 | if ($rc == 1) { |
||
35 | return $rc; |
||
36 | } |
||
37 | |||
38 | if ($rc == -1) { |
||
39 | return 0; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | $headers = ["From: $from_combined", "Content-Type: text/plain; charset=UTF-8"]; |
||
44 | |||
45 | return mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers))); |
||
46 | } |
||
47 | |||
48 | public function set_error($message) { |
||
49 | $this->last_error = $message; |
||
50 | } |
||
51 | |||
52 | public function error() { |
||
53 | return $this->last_error; |
||
54 | } |
||
55 | } |
||
56 |