| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\WpInit; |
| 4 |
|
| 5 |
use AcyMailing\Helpers\MailerHelper; |
| 6 |
|
| 7 |
class OverrideEmail |
| 8 |
{ |
| 9 |
public function __construct() |
| 10 |
{ |
| 11 |
add_filter('wp_mail', [$this, 'overrideEmailFunction']); |
| 12 |
} |
| 13 |
|
| 14 |
public function overrideEmailFunction($args) |
| 15 |
{ |
| 16 |
if (empty($args['to'])) { |
| 17 |
return $args; |
| 18 |
} |
| 19 |
|
| 20 |
$passedTo = $args['to']; |
| 21 |
if (!is_array($passedTo)) { |
| 22 |
$passedTo = explode(',', $passedTo); |
| 23 |
array_map('trim', $passedTo); |
| 24 |
} |
| 25 |
|
| 26 |
$to = array_shift($passedTo); |
| 27 |
$bcc = $passedTo; |
| 28 |
|
| 29 |
$contentType = ''; |
| 30 |
$boundary = ''; |
| 31 |
$headers = []; |
| 32 |
if (!empty($args['headers'])) { |
| 33 |
$rawHeaders = is_array($args['headers']) ? $args['headers'] : explode("\n", str_replace("\r\n", "\n", $args['headers'])); |
| 34 |
|
| 35 |
foreach ($rawHeaders as $oneHeader) { |
| 36 |
if (strpos($oneHeader, ':') === false) { |
| 37 |
if (stripos($oneHeader, 'boundary=') !== false) { |
| 38 |
$parts = preg_split('/boundary=/i', trim($oneHeader)); |
| 39 |
$boundary = trim(str_replace(["'", '"'], '', $parts[1])); |
| 40 |
} |
| 41 |
|
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
[$name, $content] = explode(':', trim($oneHeader), 2); |
| 46 |
|
| 47 |
if (in_array(strtolower($name), ['cc', 'bcc'])) { |
| 48 |
$bcc[] = trim($content); |
| 49 |
} elseif (strtolower($name) === 'content-type') { |
| 50 |
if (strpos($content, ';') !== false) { |
| 51 |
[$type, $charsetContent] = explode(';', $content); |
| 52 |
$contentType = trim($type); |
| 53 |
if (stripos($charsetContent, 'boundary=') !== false) { |
| 54 |
$boundary = trim(str_replace(['BOUNDARY=', 'boundary=', '"'], '', $charsetContent)); |
| 55 |
} |
| 56 |
} elseif (trim($content) !== '') { |
| 57 |
$contentType = trim($content); |
| 58 |
} |
| 59 |
} else { |
| 60 |
$headers[$name] = $content; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (stripos($contentType, 'multipart') !== false && !empty($boundary)) { |
| 66 |
$headers['Content-Type'] = $contentType.'; boundary="'.$boundary.'"'; |
| 67 |
} |
| 68 |
|
| 69 |
$attachments = []; |
| 70 |
if (!empty($args['attachments'])) { |
| 71 |
$rawAttachments = is_array($args['attachments']) ? $args['attachments'] : explode("\n", str_replace("\r\n", "\n", $args['attachments'])); |
| 72 |
|
| 73 |
foreach ($rawAttachments as $fileName => $attachmentUrl) { |
| 74 |
$fileName = is_string($fileName) ? $fileName : basename($attachmentUrl); |
| 75 |
$attachments[] = (object)[ |
| 76 |
'filename' => $attachmentUrl, |
| 77 |
'name' => $fileName, |
| 78 |
'url' => str_replace(ACYM_ROOT, ACYM_LIVE, $attachmentUrl), |
| 79 |
]; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$mailerHelper = new MailerHelper(); |
| 84 |
$mailerHelper->report = false; |
| 85 |
|
| 86 |
$success = $mailerHelper->overrideEmail( |
| 87 |
[ |
| 88 |
'subject' => $args['subject'], |
| 89 |
'message' => $args['message'], |
| 90 |
'to' => $to, |
| 91 |
'isHtml' => $contentType !== 'text/plain', |
| 92 |
'headers' => $headers, |
| 93 |
'bcc' => $bcc, |
| 94 |
'attachments' => $attachments, |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
if ($success) { |
| 99 |
add_action('phpmailer_init', [$this, 'blockEmailSending']); |
| 100 |
add_filter('post_smtp_do_send_email', [$this, 'blockEmailSendingPostSMTP']); |
| 101 |
} |
| 102 |
|
| 103 |
return $args; |
| 104 |
} |
| 105 |
|
| 106 |
public function blockEmailSending(&$phpmailer) |
| 107 |
{ |
| 108 |
$phpmailer = new FakePhpMailer(); |
| 109 |
} |
| 110 |
|
| 111 |
public function blockEmailSendingPostSMTP($shouldSend) |
| 112 |
{ |
| 113 |
return false; |
| 114 |
} |
| 115 |
} |
| 116 |
|