PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Mailer / MailerService.php

MailerService.php in The Innovative Form Builder – IvyForms 1.0, at backend/src/Services/Mailer/MailerService.php

120 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Services\Mailer;
9
10 use IvyForms\Entity\Notification\Notification;
11
12 class MailerService
13 {
14 /**
15 * Send an email using WordPress wp_mail function
16 *
17 * @param Notification $notification The notification object with email data
18 * @param mixed[] $formData The submitted form data for template variables
19 *
20 * @return bool Whether the email was sent successfully
21 */
22 public function sendEmail(Notification $notification, array $formData): bool
23 {
24 // Extract notification data
25 $to = $notification->getNotificationContact()->getReceiver();
26 $subject = $notification->getSubject();
27 $message = $this->replacePlaceholders($notification->getMessage(), $formData);
28 $from = $notification->getNotificationContact()->getSender();
29 $replyTo = $notification->getNotificationContact()->getReplyTo();
30
31 // Set up headers
32 $headers = [
33 'Content-Type: text/html; charset=UTF-8',
34 ];
35 if (!empty($from)) {
36 $headers[] = 'From: ' . $from;
37 }
38 // If replyTo is set and not identical to From, add Reply-To header
39 if (!empty($replyTo) && $replyTo !== $from) {
40 $headers[] = 'Reply-To: ' . $replyTo;
41 }
42
43 // Send email using WordPress function
44 return wp_mail($to, $subject, $message, $headers);
45 }
46
47 /**
48 * Replace template variables in content with actual form data
49 * Supports format like {{field_name}}
50 *
51 * @param string $content Content with template variables
52 * @param mixed[] $formData Form submission data
53 *
54 * @return string Content with replaced variables
55 */
56 private function replacePlaceholders(string $content, array $formData): string
57 {
58 // Replace field variables like {{field_name}}
59 preg_match_all('/{{(.+?)}}/s', $content, $matches);
60
61 if (!empty($matches[1])) {
62 foreach ($matches[1] as $key => $fieldName) {
63 $fieldValue = $formData[$fieldName] ?? '';
64 if (is_array($fieldValue)) {
65 $fieldValue = implode(', ', $fieldValue);
66 }
67
68 $content = str_replace($matches[0][$key], $fieldValue, $content);
69 }
70 }
71
72 // Replace special variables
73 $content = str_replace('{{all_fields}}', $this->getAllFieldsHtml($formData), $content);
74
75 return $content;
76 }
77
78 /**
79 * Generate HTML table with all form fields for email template
80 *
81 * @param mixed[] $formData Form submission data
82 *
83 * @return string HTML representation of all fields
84 */
85 private function getAllFieldsHtml(array $formData): string
86 {
87 // Generate HTML table for all fields
88 $html = '<h2>Form Submission Details</h2>';
89 $html .= '<table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">';
90
91 foreach ($formData as $fieldName => $fieldValue) {
92 // Skip system fields
93 if (in_array($fieldName, ['formId', 'nonce'])) {
94 continue;
95 }
96
97 // Format array values
98 if (is_array($fieldValue)) {
99 $fieldValue = implode(', ', $fieldValue);
100 }
101
102 // Format label by converting snake_case or camelCase to Title Case
103 $label = ucwords(str_replace(['_', '-'], ' ', $fieldName));
104
105 $html .= '<tr>';
106 $html .= '<th style="text-align: left; padding: 8px; border-bottom: 1px solid #ddd;">';
107 $html .= esc_html($label) . '</th>';
108 $html .= '</th>';
109 $html .= '<td style="text-align: left; padding: 8px; border-bottom: 1px solid #ddd;">';
110 $html .= esc_html($fieldValue) . '</td>';
111 $html .= '</td>';
112 $html .= '</tr>';
113 }
114
115 $html .= '</table>';
116
117 return $html;
118 }
119 }
120