| @@ -1,126 +1,0 @@ | ||
| 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 | - // A previous email in the same request may have registered the "blockEmailSending" and "blockEmailSendingPostSMTP" hooks below. | |
| 17 | - // We remove them systematically so each email is evaluated independently and we never missblock an email | |
| 18 | - remove_action('phpmailer_init', [$this, 'blockEmailSending']); | |
| 19 | - remove_filter('post_smtp_do_send_email', [$this, 'blockEmailSendingPostSMTP']); | |
| 20 | - | |
| 21 | - // Let integrators exclude specific emails from AcyMailing's handling | |
| 22 | - if (!apply_filters('acym_override_wp_mail', true, $args)) { | |
| 23 | - return $args; | |
| 24 | - } | |
| 25 | - | |
| 26 | - if (empty($args['to'])) { | |
| 27 | - return $args; | |
| 28 | - } | |
| 29 | - | |
| 30 | - $passedTo = $args['to']; | |
| 31 | - if (!is_array($passedTo)) { | |
| 32 | - $passedTo = explode(',', $passedTo); | |
| 33 | - array_map('trim', $passedTo); | |
| 34 | - } | |
| 35 | - | |
| 36 | - $to = array_shift($passedTo); | |
| 37 | - $bcc = $passedTo; | |
| 38 | - | |
| 39 | - $contentType = ''; | |
| 40 | - $boundary = ''; | |
| 41 | - $headers = []; | |
| 42 | - if (!empty($args['headers'])) { | |
| 43 | - $rawHeaders = is_array($args['headers']) ? $args['headers'] : explode("\n", str_replace("\r\n", "\n", $args['headers'])); | |
| 44 | - | |
| 45 | - foreach ($rawHeaders as $oneHeader) { | |
| 46 | - if (strpos($oneHeader, ':') === false) { | |
| 47 | - if (stripos($oneHeader, 'boundary=') !== false) { | |
| 48 | - $parts = preg_split('/boundary=/i', trim($oneHeader)); | |
| 49 | - $boundary = trim(str_replace(["'", '"'], '', $parts[1])); | |
| 50 | - } | |
| 51 | - | |
| 52 | - continue; | |
| 53 | - } | |
| 54 | - | |
| 55 | - [$name, $content] = explode(':', trim($oneHeader), 2); | |
| 56 | - | |
| 57 | - if (in_array(strtolower($name), ['cc', 'bcc'])) { | |
| 58 | - $bcc[] = trim($content); | |
| 59 | - } elseif (strtolower($name) === 'content-type') { | |
| 60 | - if (strpos($content, ';') !== false) { | |
| 61 | - [$type, $charsetContent] = explode(';', $content); | |
| 62 | - $contentType = trim($type); | |
| 63 | - if (stripos($charsetContent, 'boundary=') !== false) { | |
| 64 | - $boundary = trim(str_replace(['BOUNDARY=', 'boundary=', '"'], '', $charsetContent)); | |
| 65 | - } | |
| 66 | - // Avoid setting an empty $content_type. | |
| 67 | - } elseif (trim($content) !== '') { | |
| 68 | - $contentType = trim($content); | |
| 69 | - } | |
| 70 | - } else { | |
| 71 | - $headers[$name] = $content; | |
| 72 | - } | |
| 73 | - } | |
| 74 | - } | |
| 75 | - | |
| 76 | - if (stripos($contentType, 'multipart') !== false && !empty($boundary)) { | |
| 77 | - $headers['Content-Type'] = $contentType.'; boundary="'.$boundary.'"'; | |
| 78 | - } | |
| 79 | - | |
| 80 | - $attachments = []; | |
| 81 | - if (!empty($args['attachments'])) { | |
| 82 | - $rawAttachments = is_array($args['attachments']) ? $args['attachments'] : explode("\n", str_replace("\r\n", "\n", $args['attachments'])); | |
| 83 | - | |
| 84 | - foreach ($rawAttachments as $fileName => $attachmentUrl) { | |
| 85 | - $fileName = is_string($fileName) ? $fileName : basename($attachmentUrl); | |
| 86 | - $attachments[] = (object)[ | |
| 87 | - 'filename' => $attachmentUrl, | |
| 88 | - 'name' => $fileName, | |
| 89 | - 'url' => str_replace(ACYM_ROOT, ACYM_LIVE, $attachmentUrl), | |
| 90 | - ]; | |
| 91 | - } | |
| 92 | - } | |
| 93 | - | |
| 94 | - $mailerHelper = new MailerHelper(); | |
| 95 | - $mailerHelper->report = false; | |
| 96 | - | |
| 97 | - $success = $mailerHelper->overrideEmail( | |
| 98 | - [ | |
| 99 | - 'subject' => $args['subject'], | |
| 100 | - 'message' => $args['message'], | |
| 101 | - 'to' => $to, | |
| 102 | - 'isHtml' => $contentType !== 'text/plain', | |
| 103 | - 'headers' => $headers, | |
| 104 | - 'bcc' => $bcc, | |
| 105 | - 'attachments' => $attachments, | |
| 106 | - ] | |
| 107 | - ); | |
| 108 | - | |
| 109 | - if ($success) { | |
| 110 | - add_action('phpmailer_init', [$this, 'blockEmailSending']); | |
| 111 | - add_filter('post_smtp_do_send_email', [$this, 'blockEmailSendingPostSMTP']); | |
| 112 | - } | |
| 113 | - | |
| 114 | - return $args; | |
| 115 | - } | |
| 116 | - | |
| 117 | - public function blockEmailSending(&$phpmailer) | |
| 118 | - { | |
| 119 | - $phpmailer = new MailDisabler(); | |
| 120 | - } | |
| 121 | - | |
| 122 | - public function blockEmailSendingPostSMTP($shouldSend) | |
| 123 | - { | |
| 124 | - return false; | |
| 125 | - } | |
| 126 | -} | |