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

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