| 1 |
<?php |
| 2 |
namespace BitCode\BitForm\Core\Util; |
| 3 |
|
| 4 |
use BitCode\BitForm\Core\Util\MailConfig; |
| 5 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 6 |
use BitCode\BitForm\Core\Messages\EmailTemplateHandler; |
| 7 |
|
| 8 |
final class MailNotifier |
| 9 |
{ |
| 10 |
public static function notify($notifyDetails, $formID, $fieldValue, $entryID) |
| 11 |
{ |
| 12 |
$emailTemplateHandler = new EmailTemplateHandler($formID); |
| 13 |
if (is_string($notifyDetails->id)) { |
| 14 |
$mailTemplateID = json_decode($notifyDetails->id)->id; |
| 15 |
$mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID); |
| 16 |
if (!is_wp_error($mailTemplate)) { |
| 17 |
$mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue); |
| 18 |
if (!empty($mailTo)) { |
| 19 |
(new MailConfig())->sendMail(); |
| 20 |
$mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue); |
| 21 |
$mailBody = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->body, $fieldValue); |
| 22 |
|
| 23 |
$mailHeaders = array( |
| 24 |
// "Content-Type: text/html; charset=UTF-8", |
| 25 |
); |
| 26 |
if (!empty($notifyDetails->replyto)) { |
| 27 |
$mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue); |
| 28 |
if (is_array($mailReplyTo)) { |
| 29 |
foreach ($mailReplyTo as $key => $emailAddress) { |
| 30 |
$mailHeaders[] = "Reply-To: ".explode('@', $emailAddress)[0] ."<". sanitize_email($emailAddress) . ">"; |
| 31 |
} |
| 32 |
} else { |
| 33 |
$mailHeaders[] = "Reply-To: ".explode('@', $mailReplyTo)[0] ."<". sanitize_email($mailReplyTo) . ">"; |
| 34 |
} |
| 35 |
} |
| 36 |
if (!empty($notifyDetails->bcc)) { |
| 37 |
$mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue); |
| 38 |
if (is_array($mailBCC)) { |
| 39 |
foreach ($mailBCC as $key => $emailAddress) { |
| 40 |
$mailHeaders[] = "Bcc: " . sanitize_email($emailAddress); |
| 41 |
} |
| 42 |
} else { |
| 43 |
$mailHeaders[] = "Bcc: " . sanitize_email($mailBCC); |
| 44 |
} |
| 45 |
} |
| 46 |
if (!empty($notifyDetails->cc)) { |
| 47 |
$mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue); |
| 48 |
if (is_array($mailCC)) { |
| 49 |
foreach ($mailCC as $key => $emailAddress) { |
| 50 |
$mailHeaders[] = "Cc: " . sanitize_email($emailAddress); |
| 51 |
} |
| 52 |
} else { |
| 53 |
$mailHeaders[] = "Cc: " . sanitize_email($mailCC); |
| 54 |
} |
| 55 |
} |
| 56 |
if (!empty($notifyDetails->from)) { |
| 57 |
$mailFrom = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue); |
| 58 |
$fromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $mailFrom[0])[0]; |
| 59 |
$mailHeaders[] = "FROM: $fromName ". "<" . sanitize_email($mailFrom[0]) . ">"; |
| 60 |
} |
| 61 |
$attachments = []; |
| 62 |
if (!empty($notifyDetails->attachment)) { |
| 63 |
$files = $notifyDetails->attachment; |
| 64 |
$fileBasePath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 65 |
if (is_array($files)) { |
| 66 |
foreach ($files as $file) { |
| 67 |
if (isset($fieldValue[$file])) { |
| 68 |
if (is_array($fieldValue[$file])) { |
| 69 |
foreach ($fieldValue[$file] as $singleFile) { |
| 70 |
if (\is_readable("{$fileBasePath}{$singleFile}")) { |
| 71 |
$attachments[] = "{$fileBasePath}{$singleFile}"; |
| 72 |
} |
| 73 |
} |
| 74 |
} elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) { |
| 75 |
$attachments[] = "{$fileBasePath}{$fieldValue[$file]}"; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} else if (isset($fieldValue[$files])) { |
| 80 |
if (is_array($fieldValue[$files])) { |
| 81 |
foreach ($fieldValue[$files] as $singleFile) { |
| 82 |
if (\is_readable("{$fileBasePath}{$singleFile}")) { |
| 83 |
$attachments[] = "{$fileBasePath}{$singleFile}"; |
| 84 |
} |
| 85 |
} |
| 86 |
} elseif (\is_readable("{$fileBasePath}{$fieldValue[$files]}")) { |
| 87 |
$attachments[] = "{$fileBasePath}{$fieldValue[$files]}"; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
$mailBody = stripcslashes($mailBody); |
| 92 |
$mailSubject = stripcslashes($mailSubject); |
| 93 |
add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']); |
| 94 |
$status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments); |
| 95 |
if (!$status) { |
| 96 |
wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders); |
| 97 |
} |
| 98 |
remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public static function filterMailContentType() |
| 105 |
{ |
| 106 |
return 'text/html; charset=UTF-8'; |
| 107 |
} |
| 108 |
} |
| 109 |
|