| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services\Libs; |
| 4 |
|
| 5 |
use Throwable; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
class Mailer |
| 9 |
{ |
| 10 |
private $subject = ''; |
| 11 |
|
| 12 |
private $body = ''; |
| 13 |
|
| 14 |
private $to = ''; |
| 15 |
|
| 16 |
private $from = ''; |
| 17 |
|
| 18 |
private $cc = []; |
| 19 |
|
| 20 |
private $bcc = []; |
| 21 |
|
| 22 |
private $replyTo = ''; |
| 23 |
|
| 24 |
private $isHtml = true; |
| 25 |
|
| 26 |
public function __construct($to = '', $subject = '', $body = '') |
| 27 |
{ |
| 28 |
$this->to = $to; |
| 29 |
$this->subject = $subject; |
| 30 |
$this->body = $body; |
| 31 |
} |
| 32 |
|
| 33 |
public function setSubject($subject) |
| 34 |
{ |
| 35 |
$this->subject = $subject; |
| 36 |
return $this; |
| 37 |
} |
| 38 |
|
| 39 |
public function setBody($body) |
| 40 |
{ |
| 41 |
$this->body = $body; |
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
public function to($to) |
| 46 |
{ |
| 47 |
$this->to = $to; |
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
public function setIsHtml($isHtml) |
| 52 |
{ |
| 53 |
$this->isHtml = $isHtml; |
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
public function setFrom($from) |
| 58 |
{ |
| 59 |
$this->from = $from; |
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
public function addCC($cc) |
| 64 |
{ |
| 65 |
$this->cc[] = $cc; |
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
public function addBCC($bcc) |
| 70 |
{ |
| 71 |
$this->bcc[] = $bcc; |
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
public function setReplyTo($replyTo) |
| 76 |
{ |
| 77 |
$this->replyTo = $replyTo; |
| 78 |
return $this; |
| 79 |
} |
| 80 |
|
| 81 |
public function send() |
| 82 |
{ |
| 83 |
if (!$this->to && !$this->cc && !$this->bcc) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
$headers = []; |
| 88 |
|
| 89 |
if ($this->isHtml) { |
| 90 |
$headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 91 |
} else { |
| 92 |
$headers[] = 'Content-Type: text/plain; charset=UTF-8'; |
| 93 |
} |
| 94 |
|
| 95 |
if ($this->from) { |
| 96 |
$headers[] = 'From: ' . $this->from; |
| 97 |
} |
| 98 |
|
| 99 |
if ($this->cc) { |
| 100 |
$headers[] = 'Cc: ' . implode(',', $this->cc); |
| 101 |
} |
| 102 |
|
| 103 |
if ($this->bcc) { |
| 104 |
$headers[] = 'Bcc: ' . implode(',', $this->bcc); |
| 105 |
} |
| 106 |
|
| 107 |
if ($this->replyTo) { |
| 108 |
$headers[] = 'Reply-To: ' . $this->replyTo; |
| 109 |
} |
| 110 |
|
| 111 |
return wp_mail($this->to, $this->subject, $this->body, $headers); |
| 112 |
} |
| 113 |
|
| 114 |
/** Send a notification with result reporting, retaining the generic send() contract. */ |
| 115 |
public function sendNotification(array $context) |
| 116 |
{ |
| 117 |
$context['recipient'] = $this->to; |
| 118 |
try { |
| 119 |
$sent = $this->send(); |
| 120 |
} catch (Throwable $e) { |
| 121 |
self::reportFailure($context, $e); |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
if (!$sent) { |
| 126 |
self::reportFailure($context); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
// Accepted by wp_mail, not proof of delivery to the recipient's inbox. |
| 131 |
self::dispatchResult(true, $context); |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
/** Log only type/ID; email_failed receives context and a WP_Error for diagnostics. */ |
| 136 |
public static function reportFailure(array $context, ?Throwable $exception = null) |
| 137 |
{ |
| 138 |
error_log(sprintf( |
| 139 |
'FluentBoards email failed: %s #%d.', |
| 140 |
sanitize_key($context['type'] ?? 'unknown'), |
| 141 |
absint($context['task_id'] ?? $context['comment_id'] ?? 0) |
| 142 |
)); |
| 143 |
$error = new WP_Error('fluent_boards_email_failed', $exception ? $exception->getMessage() : 'wp_mail returned false.'); |
| 144 |
self::dispatchResult(false, $context, $error); |
| 145 |
} |
| 146 |
|
| 147 |
/** Reporting observers must not interrupt delivery or turn accepted mail into failure. */ |
| 148 |
private static function dispatchResult(bool $sent, array $context, $error = null) |
| 149 |
{ |
| 150 |
try { |
| 151 |
if ($sent) { |
| 152 |
do_action('fluent_boards/email_sent', $context, $error); |
| 153 |
} else { |
| 154 |
do_action('fluent_boards/email_failed', $context, $error); |
| 155 |
} |
| 156 |
} catch (Throwable $e) { |
| 157 |
error_log('FluentBoards email reporting observer failed: ' . ($sent ? 'fluent_boards/email_sent' : 'fluent_boards/email_failed')); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|