PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.5.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.5.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Util / MailNotifier.php

MailNotifier.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.5.2, at includes/Core/Util/MailNotifier.php

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