PluginProbe
The Innovative Form Builder – IvyForms / 0.1.2
The Innovative Form Builder – IvyForms v0.1.2
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 0.1.2, at backend/src/Services/Mailer/MailerService.php

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