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

173 lines 7.4 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 use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
8 use BitCode\BitFormPro\Admin\AppSetting\Pdf;
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 $attachments = [];
16 $tempPdfLink = '';
17 if (!empty($notifyDetails->pdfId) && is_string($notifyDetails->pdfId)) {
18 $pdfTemplateID = json_decode($notifyDetails->pdfId)->id;
19 $pdfTemplateHandler = new PdfTemplateHandler($formID);
20
21 $pdfTemplate = $pdfTemplateHandler->getById($pdfTemplateID);
22 $pdfBody = FieldValueHandler::replaceFieldWithValue($pdfTemplate[0]->body, $fieldValue);
23
24 $pdfSetting = json_decode($pdfTemplate[0]->setting);
25
26 $path = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'pdf';
27 $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID;
28
29 if (!is_dir($path)) {
30 mkdir($path, 0777, true);
31 }
32
33 if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
34 $serverPath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR;
35 $pdfBody = FieldValueHandler::changeImagePathInHTMLString($pdfBody, $serverPath);
36 $generatedPdf = Pdf::getInstance()->generator($pdfSetting, $pdfBody, $path, $entryID, 'F');
37
38 if (!is_wp_error($generatedPdf) && file_exists($generatedPdf)) {
39 $attachments[] = $generatedPdf;
40 $tempPdfLink = $generatedPdf;
41 } else {
42 Log::debug_log('Error in generating PDF: ' . $generatedPdf->get_error_message());
43 }
44 }
45 }
46
47 if (is_string($notifyDetails->id)) {
48 $mailTemplateID = json_decode($notifyDetails->id)->id;
49 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
50 if (!is_wp_error($mailTemplate)) {
51 $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue);
52 if (!empty($mailTo)) {
53 $from_name = '';
54 if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) {
55 $from_name = $notifyDetails->from_name;
56 }
57
58 $mailHeaders = [
59 // 'Content-Type: text/html; charset=UTF-8',
60 // $embeddedMailHeader
61 ];
62 if (!empty($notifyDetails->from)) {
63 $fromMail = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue);
64 $headerFromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $fromMail[0])[0];
65 $mailHeaders[] = "FROM: $headerFromName " . '<' . sanitize_email($fromMail[0]) . '>';
66 }
67 (new MailConfig())->sendMail(['from_name' => $from_name, 'from_email' => 'mdrobiulhasan2622@gmail.com']);
68 $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue);
69 $mailBody = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->body, $fieldValue);
70 $webUrl = BITFORMS_UPLOAD_BASE_URL . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR;
71 $mailBody = FieldValueHandler::changeImagePathInHTMLString($mailBody, $webUrl);
72
73 if (!empty($notifyDetails->replyto)) {
74 $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
75 if (is_array($mailReplyTo)) {
76 foreach ($mailReplyTo as $key => $emailAddress) {
77 $mailHeaders[] = 'Reply-To: ' . explode('@', $emailAddress)[0] . '<' . sanitize_email($emailAddress) . '>';
78 }
79 } else {
80 $mailHeaders[] = 'Reply-To: ' . explode('@', $mailReplyTo)[0] . '<' . sanitize_email($mailReplyTo) . '>';
81 }
82 }
83 $oldMailBody = $mailBody;
84 $data = [];
85 if ($isDblOptin && true === has_filter('bf_email_body_text')) {
86 $urlParams = $formID . '_' . $entryID . '_' . $logId;
87 $data = apply_filters('bf_email_body_text', $mailBody, $urlParams);
88 $mailBody = $data['mailbody'];
89 }
90
91 if (!empty($notifyDetails->bcc)) {
92 $mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue);
93 if (is_array($mailBCC)) {
94 foreach ($mailBCC as $key => $emailAddress) {
95 $mailHeaders[] = 'Bcc: ' . sanitize_email($emailAddress);
96 }
97 } else {
98 $mailHeaders[] = 'Bcc: ' . sanitize_email($mailBCC);
99 }
100 }
101 if (!empty($notifyDetails->cc)) {
102 $mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue);
103 if (is_array($mailCC)) {
104 foreach ($mailCC as $key => $emailAddress) {
105 $mailHeaders[] = 'Cc: ' . sanitize_email($emailAddress);
106 }
107 } else {
108 $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC);
109 }
110 }
111 if (!empty($notifyDetails->attachment)) {
112 $files = $notifyDetails->attachment;
113 $fileBasePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
114 if (is_array($files)) {
115 foreach ($files as $file) {
116 if (isset($fieldValue[$file])) {
117 if (is_array($fieldValue[$file])) {
118 foreach ($fieldValue[$file] as $singleFile) {
119 if (\is_readable("{$fileBasePath}{$singleFile}")) {
120 $attachments[] = "{$fileBasePath}{$singleFile}";
121 }
122 }
123 } elseif (\is_readable("{$fileBasePath}{$fieldValue[$file]}")) {
124 $attachments[] = "{$fileBasePath}{$fieldValue[$file]}";
125 }
126 }
127 }
128 } elseif (isset($fieldValue[$files])) {
129 if (is_array($fieldValue[$files])) {
130 foreach ($fieldValue[$files] as $singleFile) {
131 if (\is_readable("{$fileBasePath}{$singleFile}")) {
132 $attachments[] = "{$fileBasePath}{$singleFile}";
133 }
134 }
135 } elseif (\is_readable("{$fileBasePath}{$fieldValue[$files]}")) {
136 $attachments[] = "{$fileBasePath}{$fieldValue[$files]}";
137 }
138 }
139 }
140 $mailBody = stripcslashes($mailBody);
141 $mailSubject = stripcslashes($mailSubject);
142 add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
143 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
144 if (!$status) {
145 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
146 }
147 if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) {
148 $entryMeta = new FormEntryMetaModel();
149
150 $entryMeta->insert(
151 [
152 'bitforms_form_entry_id' => $entryID,
153 'meta_key' => 'entry_confirm_activation',
154 'meta_value' => $data['token']
155 ]
156 );
157 }
158 remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
159 }
160 }
161 }
162
163 if (!empty($tempPdfLink)) {
164 wp_delete_file($tempPdfLink);
165 }
166 }
167
168 public static function filterMailContentType()
169 {
170 return 'text/html; charset=UTF-8';
171 }
172 }
173