| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Support; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use FluentCommunity\Framework\Foundation\App; |
| 7 |
use FluentCommunity\Framework\View\View; |
| 8 |
|
| 9 |
class Mail |
| 10 |
{ |
| 11 |
protected array $to = []; |
| 12 |
protected array $cc = []; |
| 13 |
protected array $bcc = []; |
| 14 |
protected string $from = ''; |
| 15 |
protected string $fromName = ''; |
| 16 |
protected string $subject = ''; |
| 17 |
protected string $body = ''; |
| 18 |
protected array $headers = []; |
| 19 |
protected array $attachments = []; |
| 20 |
protected string $contentType = 'text/html'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Set one or more recipients. |
| 24 |
* |
| 25 |
* @param string|array $emails |
| 26 |
* @return $this |
| 27 |
*/ |
| 28 |
public function to($emails) |
| 29 |
{ |
| 30 |
$this->to = (array) $emails; |
| 31 |
return $this; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set the email sender. |
| 36 |
* |
| 37 |
* @param string $email |
| 38 |
* @param string|null $name |
| 39 |
* @return $this |
| 40 |
*/ |
| 41 |
public function from($email, $name = null) |
| 42 |
{ |
| 43 |
$this->from = $email; |
| 44 |
$this->fromName = $name ?: get_bloginfo('name'); |
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set one or more CC recipients. |
| 50 |
* |
| 51 |
* @param string|array $emails |
| 52 |
* @return $this |
| 53 |
*/ |
| 54 |
public function cc($emails) |
| 55 |
{ |
| 56 |
$this->cc = array_merge($this->cc, (array) $emails); |
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Set one or more BCC recipients. |
| 62 |
* |
| 63 |
* @param string|array $emails |
| 64 |
* @return $this |
| 65 |
*/ |
| 66 |
public function bcc($emails) |
| 67 |
{ |
| 68 |
$this->bcc = array_merge($this->bcc, (array) $emails); |
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Set the email subject. |
| 74 |
* |
| 75 |
* @param string $subject |
| 76 |
* @return $this |
| 77 |
*/ |
| 78 |
public function subject($subject) |
| 79 |
{ |
| 80 |
$this->subject = $subject; |
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Set the email body (HTML or plain text). |
| 86 |
* |
| 87 |
* @param string $body |
| 88 |
* @return $this |
| 89 |
*/ |
| 90 |
public function body($body) |
| 91 |
{ |
| 92 |
$this->body = $body; |
| 93 |
return $this; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Set the body from a PHP template file with optional data. |
| 98 |
* |
| 99 |
* @param string $templatePath |
| 100 |
* @param array $data |
| 101 |
* @return $this |
| 102 |
*/ |
| 103 |
public function view($templatePath, $data = []) |
| 104 |
{ |
| 105 |
$this->body = App::make(View::class)->make($templatePath, $data); |
| 106 |
|
| 107 |
$this->contentType('text/html'); |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add or override headers. |
| 114 |
* |
| 115 |
* @param array|string $headers |
| 116 |
* @param string|null $value |
| 117 |
* @return $this |
| 118 |
*/ |
| 119 |
public function withHeader($headers, $value = null) |
| 120 |
{ |
| 121 |
if (is_array($headers)) { |
| 122 |
$this->headers = array_merge($this->headers, $headers); |
| 123 |
} else { |
| 124 |
$this->headers[$headers] = $value; |
| 125 |
} |
| 126 |
|
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Set Content-Type header (default is text/html). |
| 132 |
* |
| 133 |
* @param string $contentType |
| 134 |
* @return $this |
| 135 |
*/ |
| 136 |
public function contentType($contentType) |
| 137 |
{ |
| 138 |
$this->contentType = $contentType; |
| 139 |
|
| 140 |
return $this; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Attach one or more files. |
| 145 |
* |
| 146 |
* @param string|array $paths |
| 147 |
* @return $this |
| 148 |
*/ |
| 149 |
public function attach($paths) |
| 150 |
{ |
| 151 |
$paths = (array) $paths; |
| 152 |
foreach ($paths as $path) { |
| 153 |
if (!file_exists($path)) { |
| 154 |
throw new InvalidArgumentException( |
| 155 |
"Attachment file not found: {$path}" |
| 156 |
); |
| 157 |
} |
| 158 |
$this->attachments[] = $path; |
| 159 |
} |
| 160 |
|
| 161 |
return $this; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Prepare headers for wp_mail. |
| 166 |
* |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
protected function prepareHeaders() |
| 170 |
{ |
| 171 |
$headers = []; |
| 172 |
|
| 173 |
// Note: We removed Content-Type and From from here |
| 174 |
// because we handle them via filters in send() now. |
| 175 |
// This prevents duplicate headers which triggers some spam filters. |
| 176 |
|
| 177 |
if (!empty($this->cc)) { |
| 178 |
$headers[] = 'Cc: ' . implode(', ', $this->cc); |
| 179 |
} |
| 180 |
|
| 181 |
if (!empty($this->bcc)) { |
| 182 |
$headers[] = 'Bcc: ' . implode(', ', $this->bcc); |
| 183 |
} |
| 184 |
|
| 185 |
foreach ($this->headers as $key => $value) { |
| 186 |
$headers[] = is_int($key) ? $value : "{$key}: {$value}"; |
| 187 |
} |
| 188 |
|
| 189 |
return $headers; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Send the email immediately. |
| 194 |
* |
| 195 |
* @return bool |
| 196 |
*/ |
| 197 |
public function send() |
| 198 |
{ |
| 199 |
if (empty($this->to)) { |
| 200 |
throw new InvalidArgumentException( |
| 201 |
"At least one recipient (to) must be specified." |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
// 1. Set Content Type Filter |
| 206 |
$contentType = function() { return $this->contentType; }; |
| 207 |
add_filter('wp_mail_content_type', $contentType); |
| 208 |
|
| 209 |
// 2. Set From Filters (if provided) |
| 210 |
$fromEmailFilter = fn() => $this->from ?: get_bloginfo('admin_email'); |
| 211 |
$fromNameFilter = fn() => $this->fromName ?: get_bloginfo('name'); |
| 212 |
|
| 213 |
if ($this->from) { |
| 214 |
add_filter('wp_mail_from', $fromEmailFilter); |
| 215 |
add_filter('wp_mail_from_name', $fromNameFilter); |
| 216 |
} |
| 217 |
|
| 218 |
$to = implode(', ', $this->to); |
| 219 |
$headers = $this->prepareHeaders(); |
| 220 |
|
| 221 |
$result = wp_mail( |
| 222 |
$to, |
| 223 |
$this->subject, |
| 224 |
$this->body, |
| 225 |
$headers, |
| 226 |
$this->attachments |
| 227 |
); |
| 228 |
|
| 229 |
// 3. Clean up filters so we don't affect other emails |
| 230 |
remove_filter('wp_mail_content_type', $contentType); |
| 231 |
|
| 232 |
if ($this->from) { |
| 233 |
remove_filter('wp_mail_from', $fromEmailFilter); |
| 234 |
remove_filter('wp_mail_from_name', $fromNameFilter); |
| 235 |
} |
| 236 |
|
| 237 |
return $result; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Static helper to start a new mail instance. |
| 242 |
* |
| 243 |
* @return static |
| 244 |
*/ |
| 245 |
public static function make() |
| 246 |
{ |
| 247 |
return new static(); |
| 248 |
} |
| 249 |
} |
| 250 |
|