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

326 lines 13.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\Admin\Form\Helpers;
6 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
7 use BitCode\BitForm\Core\Form\FormManager;
8 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
9 use BitCode\BitForm\Core\Messages\PdfTemplateHandler;
10 use BitCode\BitFormPro\Admin\AppSetting\Pdf;
11
12 final class MailNotifier
13 {
14 public static function notify($notifyDetails, $formID, $fieldValue, $entryID, $isDblOptin = false, $logId = '')
15 {
16 $apiResponse = new ApiResponse();
17 $formManager = FormManager::getInstance($formID);
18 $entryDetails = ['formId' => $formID, 'entryId' => $entryID, 'fieldValues' => $fieldValue];
19 $emailTemplateHandler = new EmailTemplateHandler($formID);
20 $attachments = [];
21 $tempPdfLink = '';
22 $pdfPassForEmail = '';
23 if (!empty($notifyDetails->pdfId) && is_string($notifyDetails->pdfId)) {
24 $pdfTemplateID = json_decode($notifyDetails->pdfId)->id;
25 $pdfTemplateHandler = new PdfTemplateHandler($formID);
26
27 $pdfTemplate = $pdfTemplateHandler->getById($pdfTemplateID);
28
29 $pdfSetting = json_decode($pdfTemplate[0]->setting);
30 // error_log('PDF Setting before pdfFileName: ' . print_r([$pdfSetting, $fieldValue], true));
31
32 $path = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'pdf';
33 // $fileName = 'bit-form-pdf-' . $formID . '-' . $entryID;
34
35 if (!is_dir($path)) {
36 mkdir($path, 0777, true);
37 }
38
39 if (class_exists('\BitCode\BitFormPro\Admin\AppSetting\Pdf')) {
40 $serverPath = Helpers::getFullPathWithEncryptedEntryId($formID, $entryID);
41 $webPath = Helpers::getWebPathWithEncryptedEntryId($formID, $entryID);
42
43 if (isset($pdfSetting->password)) {
44 if (isset($pdfSetting->password->static) && $pdfSetting->password->static && !empty($pdfSetting->password->pass)) {
45 $pass = FieldValueHandler::replaceFieldWithValue($pdfSetting->password->pass, $fieldValue);
46 $pdfSetting->password->pass = $pass;
47 } elseif (isset($pdfSetting->password->dynamic)) {
48 $pass = Helpers::PDFPassHash($entryID);
49 $pdfSetting->password->pass = $pass;
50 }
51 }
52 if (isset($pdfSetting->pdfFileName)) {
53 $pdfSetting->pdfFileName = FieldValueHandler::replaceFieldWithValue($pdfSetting->pdfFileName, $fieldValue);
54 // allow developers to modify PDF filename
55 $pdfSetting->pdfFileName = apply_filters(
56 'bitform_filter_pdf_filename',
57 $pdfSetting->pdfFileName,
58 [
59 'form_id' => $formID,
60 'entry_id' => $entryID,
61 'field_values' => $fieldValue,
62 'pdf_setting' => $pdfSetting,
63 'template' => $pdfTemplate[0] ?? null,
64 ]
65 );
66 }
67
68 $fieldValue['entry_id'] = $entryID;
69
70 $pdfBody = FieldValueHandler::replaceFieldWithValue($pdfTemplate[0]->body, $fieldValue, $formID);
71 $pdfBody = FieldValueHandler::changeImagePathInHTMLString($pdfBody, $serverPath);
72 $pdfBody = FieldValueHandler::changeHrefPathInHTMLString($pdfBody, $webPath); // replace anchor tag href with constructed weburl
73
74 // allow developers to modify PDF body
75 $pdfBody = apply_filters(
76 'bitform_filter_pdf_body',
77 $pdfBody,
78 [
79 'form_id' => $formID,
80 'entry_id' => $entryID,
81 'field_values' => $fieldValue,
82 'pdf_setting' => $pdfSetting,
83 'template' => $pdfTemplate[0] ?? null,
84 ]
85 );
86
87 $generatedPdf = Pdf::getInstance()->generator($pdfSetting, $pdfBody, $path, $entryID, 'F');
88
89 if (!is_wp_error($generatedPdf) && file_exists($generatedPdf)) {
90 $attachments[] = $generatedPdf;
91 $tempPdfLink = $generatedPdf;
92 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'pdf'], 'success', 'PDF successfully generated.', $entryDetails);
93 Log::debug_log([
94 'status' => 'success',
95 'code' => 'pdf_generated',
96 'message' => 'PDF successfully generated',
97 'inputDetails' => [
98 'notifyDetails' => $notifyDetails,
99 'formID' => $formID,
100 'entryID' => $entryID,
101 'isDblOptin' => $isDblOptin,
102 'logId' => $logId
103 ],
104 'responseDetails' => $generatedPdf
105 ]);
106 } else {
107 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'pdf'], 'errors', 'Error in generating PDF.', $entryDetails);
108 Log::debug_log([
109 'status' => 'error',
110 'code' => 'pdf_generation_error',
111 'message' => 'Error in generating PDF',
112 'inputDetails' => [
113 'notifyDetails' => $notifyDetails,
114 'formID' => $formID,
115 'entryID' => $entryID,
116 'isDblOptin' => $isDblOptin,
117 'logId' => $logId
118 ],
119 'responseDetails' => $generatedPdf->get_error_message()
120 ]);
121 }
122 }
123 }
124
125 if (is_string($notifyDetails->id)) {
126 $mailTemplateID = json_decode($notifyDetails->id)->id;
127 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
128 if (!is_wp_error($mailTemplate)) {
129 $mailTo = FieldValueHandler::validateMailArry($notifyDetails->to, $fieldValue);
130 if (!empty($mailTo)) {
131 $from_name = '';
132 if (isset($notifyDetails->from_name) && !empty($notifyDetails->from_name)) {
133 $from_name = $notifyDetails->from_name;
134 }
135 $mailHeaders = [
136 // 'Content-Type: text/html; charset=UTF-8',
137 // $embeddedMailHeader
138 ];
139 $from_mail = '';
140 if (!empty($notifyDetails->from)) {
141 $fromMail = FieldValueHandler::validateMailArry($notifyDetails->from, $fieldValue);
142 $headerFromName = !empty($notifyDetails->fromName) ? $notifyDetails->fromName : explode('@', $fromMail[0])[0];
143 $mailHeaders[] = "FROM: $headerFromName " . '<' . sanitize_email($fromMail[0]) . '>';
144 $from_mail = $fromMail[0];
145 }
146 (new MailConfig())->sendMail(['from_name' => $from_name, 'from_email' => $from_mail]);
147 $mailSubject = FieldValueHandler::replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue, $formID);
148
149 // allow developers to modify email subject
150 $mailSubject = apply_filters(
151 'bitform_filter_email_subject',
152 $mailSubject,
153 [
154 'form_id' => $formID,
155 'entry_id' => $entryID,
156 'field_values' => $fieldValue,
157 'template' => $mailTemplate[0] ?? null,
158 'is_double_optin' => (bool) $isDblOptin,
159 ]
160 );
161
162 $mailBody = $mailTemplate[0]->body;
163 if (class_exists('\BitCode\BitFormPro\Admin\DownloadFile')) {
164 $downloadFile = new \BitCode\BitFormPro\Admin\DownloadFile();
165 $mailBody = $downloadFile->replacePdfShortCodeToLink($mailBody, $formID, $entryID);
166 $mailBody = $downloadFile->replaceShortCodeToPdfPassword($mailBody, $formID, $entryID);
167 }
168
169 $mailBody = FieldValueHandler::replaceFieldWithValue($mailBody, $fieldValue, $formID);
170 $webUrl = BITFORMS_UPLOAD_BASE_URL . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . Helpers::getEncryptedEntryId($entryID) . DIRECTORY_SEPARATOR;
171 $mailBody = FieldValueHandler::changeImagePathInHTMLString($mailBody, $webUrl);
172 $mailBody = FieldValueHandler::changeHrefPathInHTMLString($mailBody, $webUrl); // replace anchor tag href with constructed weburl
173
174 // allow developers to modify email body
175 $mailBody = apply_filters(
176 'bitform_filter_email_body',
177 $mailBody,
178 [
179 'form_id' => $formID,
180 'entry_id' => $entryID,
181 'field_values' => $fieldValue,
182 'template' => $mailTemplate[0] ?? null,
183 'is_double_optin' => (bool) $isDblOptin,
184 ]
185 );
186 if (!empty($notifyDetails->replyto)) {
187 $mailReplyTo = FieldValueHandler::validateMailArry($notifyDetails->replyto, $fieldValue);
188 if (is_array($mailReplyTo)) {
189 foreach ($mailReplyTo as $key => $emailAddress) {
190 $mailHeaders[] = 'Reply-To: ' . explode('@', $emailAddress)[0] . '<' . sanitize_email($emailAddress) . '>';
191 }
192 } else {
193 $mailHeaders[] = 'Reply-To: ' . explode('@', $mailReplyTo)[0] . '<' . sanitize_email($mailReplyTo) . '>';
194 }
195 }
196 $oldMailBody = $mailBody;
197 $data = [];
198 if ($isDblOptin && true === has_filter('bf_email_body_text')) {
199 $urlParams = $formID . '_' . $entryID . '_' . $logId;
200 $data = apply_filters('bf_email_body_text', $mailBody, $urlParams);
201 $mailBody = $data['mailbody'];
202 }
203
204 if (!empty($notifyDetails->bcc)) {
205 $mailBCC = FieldValueHandler::validateMailArry($notifyDetails->bcc, $fieldValue);
206 if (is_array($mailBCC)) {
207 foreach ($mailBCC as $key => $emailAddress) {
208 $mailHeaders[] = 'Bcc: ' . sanitize_email($emailAddress);
209 }
210 } else {
211 $mailHeaders[] = 'Bcc: ' . sanitize_email($mailBCC);
212 }
213 }
214 if (!empty($notifyDetails->cc)) {
215 $mailCC = FieldValueHandler::validateMailArry($notifyDetails->cc, $fieldValue);
216 if (is_array($mailCC)) {
217 foreach ($mailCC as $key => $emailAddress) {
218 $mailHeaders[] = 'Cc: ' . sanitize_email($emailAddress);
219 }
220 } else {
221 $mailHeaders[] = 'Cc: ' . sanitize_email($mailCC);
222 }
223 }
224 if (!empty($notifyDetails->attachment)) {
225 $fileFldKeys = $notifyDetails->attachment;
226 $fileBasePath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
227
228 // Normalize to array for consistent processing
229 $fileFldKeys = is_array($fileFldKeys) ? $fileFldKeys : [$fileFldKeys];
230
231 foreach ($fileFldKeys as $fldKey) {
232 $repeaterFieldKey = $formManager->isRepeatedField($fldKey);
233
234 if ($repeaterFieldKey) {
235 // Handle repeated file field
236 FileHandler::processRepeaterAttachment(
237 $repeaterFieldKey,
238 $fldKey,
239 $fieldValue,
240 $fileBasePath,
241 $attachments
242 );
243 } else {
244 // Handle regular file field
245 FileHandler::processRegularAttachment(
246 $fldKey,
247 $fieldValue,
248 $fileBasePath,
249 $attachments
250 );
251 }
252 }
253 }
254 $mailBody = stripcslashes($mailBody);
255 $mailSubject = stripcslashes($mailSubject);
256 add_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
257 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders, $attachments);
258
259 if (!$status) {
260 Log::debug_log([
261 'status' => 'error',
262 'code' => 'mail_not_sent',
263 'message' => 'Mail not sent',
264 'inputDetails' => [
265 'to' => $mailTo,
266 'subject' => $mailSubject,
267 'body' => $mailBody,
268 'headers' => $mailHeaders,
269 'attachments' => $attachments,
270 'notifyDetails' => $notifyDetails,
271 'formID' => $formID,
272 'entryID' => $entryID,
273 'isDblOptin' => $isDblOptin,
274 'logId' => $logId
275 ],
276 'responseDetails' => $status
277 ]);
278 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'errors', 'Mail dose not send successfully', $entryDetails);
279 } else {
280 Log::debug_log([
281 'status' => 'success',
282 'code' => 'mail_sent',
283 'message' => 'Mail successfully sent',
284 'inputDetails' => [
285 'to' => $mailTo,
286 'subject' => $mailSubject,
287 'body' => $mailBody,
288 'headers' => $mailHeaders,
289 'attachments' => $attachments,
290 'notifyDetails' => $notifyDetails,
291 'formID' => $formID,
292 'entryID' => $entryID,
293 'isDblOptin' => $isDblOptin,
294 'logId' => $logId
295 ],
296 'responseDetails' => $status
297 ]);
298 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'success', 'Mail successfully send.', $entryDetails);
299 }
300 if ($status && $isDblOptin && false !== strpos($oldMailBody, 'entry_confirmation_url')) {
301 $entryMeta = new FormEntryMetaModel();
302 $apiResponse->apiResponse($logId, '', ['type' => 'record', 'type_name' => 'smtp'], 'success', 'Mail successfully send.', $entryDetails);
303 $entryMeta->insert(
304 [
305 'bitforms_form_entry_id' => $entryID,
306 'meta_key' => 'entry_confirm_activation',
307 'meta_value' => $data['token']
308 ]
309 );
310 }
311 remove_filter('wp_mail_content_type', [self::class, 'filterMailContentType']);
312 }
313 }
314 }
315
316 if (!empty($tempPdfLink)) {
317 wp_delete_file($tempPdfLink);
318 }
319 }
320
321 public static function filterMailContentType()
322 {
323 return 'text/html; charset=UTF-8';
324 }
325 }
326