PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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 2.0, at includes/Core/Util/MailNotifier.php

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