| 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 |
// 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 |
$from = $notification->getSender(); |
| 34 |
$replyTo = $notification->getReplyTo(); |
| 35 |
|
| 36 |
// Set up headers |
| 37 |
$headers = [ |
| 38 |
'Content-Type: text/html; charset=UTF-8', |
| 39 |
]; |
| 40 |
if (!empty($from)) { |
| 41 |
$headers[] = 'From: ' . $from; |
| 42 |
} |
| 43 |
// If replyTo is set and not identical to From, add Reply-To header |
| 44 |
if (!empty($replyTo) && $replyTo !== $from) { |
| 45 |
$headers[] = 'Reply-To: ' . $replyTo; |
| 46 |
} |
| 47 |
|
| 48 |
// Send email using WordPress function |
| 49 |
return wp_mail($to, $subject, $message, $headers); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Replace template variables in content with actual form data |
| 54 |
* Supports format like {{field_name}} |
| 55 |
* |
| 56 |
* @param string $content Content with template variables |
| 57 |
* @param mixed[] $formData Form submission data |
| 58 |
* |
| 59 |
* @return string Content with replaced variables |
| 60 |
*/ |
| 61 |
private function replacePlaceholders(string $content, array $formData): string |
| 62 |
{ |
| 63 |
// Replace field variables like {{field_name}} |
| 64 |
preg_match_all('/{{(.+?)}}/s', $content, $matches); |
| 65 |
|
| 66 |
if (!empty($matches[1])) { |
| 67 |
foreach ($matches[1] as $key => $fieldName) { |
| 68 |
$fieldValue = $formData[$fieldName] ?? ''; |
| 69 |
if (is_array($fieldValue)) { |
| 70 |
$fieldValue = implode(', ', $fieldValue); |
| 71 |
} |
| 72 |
|
| 73 |
$content = str_replace($matches[0][$key], $fieldValue, $content); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Replace special variables |
| 78 |
$content = str_replace('{{all_fields}}', $this->getAllFieldsHtml($formData), $content); |
| 79 |
|
| 80 |
return $content; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Generate HTML table with all form fields for email template |
| 85 |
* |
| 86 |
* @param mixed[] $formData Form submission data |
| 87 |
* |
| 88 |
* @return string HTML representation of all fields |
| 89 |
*/ |
| 90 |
private function getAllFieldsHtml(array $formData): string |
| 91 |
{ |
| 92 |
// Generate HTML table for all fields |
| 93 |
$html = '<h2>Form Submission Details</h2>'; |
| 94 |
$html .= '<table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">'; |
| 95 |
|
| 96 |
foreach ($formData as $fieldName => $fieldValue) { |
| 97 |
// Skip system fields |
| 98 |
if (in_array($fieldName, ['formId', 'nonce'])) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
// Format array values |
| 103 |
if (is_array($fieldValue)) { |
| 104 |
$fieldValue = implode(', ', $fieldValue); |
| 105 |
} |
| 106 |
|
| 107 |
// Format label by converting snake_case or camelCase to Title Case |
| 108 |
$label = ucwords(str_replace(['_', '-'], ' ', $fieldName)); |
| 109 |
|
| 110 |
$html .= '<tr>'; |
| 111 |
$html .= '<th style="text-align: left; padding: 8px; border-bottom: 1px solid #ddd;">'; |
| 112 |
$html .= esc_html($label) . '</th>'; |
| 113 |
$html .= '</th>'; |
| 114 |
$html .= '<td style="text-align: left; padding: 8px; border-bottom: 1px solid #ddd;">'; |
| 115 |
$html .= esc_html($fieldValue) . '</td>'; |
| 116 |
$html .= '</td>'; |
| 117 |
$html .= '</tr>'; |
| 118 |
} |
| 119 |
|
| 120 |
$html .= '</table>'; |
| 121 |
|
| 122 |
return $html; |
| 123 |
} |
| 124 |
} |
| 125 |
|