getNotificationContact(); $toAddresses = $this->validatedAddresses($contact->getReceiver()); $ccAddresses = $this->validatedAddresses($contact->getCc()); $bccAddresses = $this->validatedAddresses($contact->getBcc()); $to = implode(', ', $toAddresses); $cc = implode(', ', $ccAddresses); $bcc = implode(', ', $bccAddresses); $from = $contact->getSender(); $replyTo = $contact->getReplyTo(); if ($to === '') { return false; } if ($this->hasUnsafeHeaderValues($from, $replyTo, $cc, $bcc)) { return false; } $subject = $notification->getSubject(); $message = $this->replacePlaceholders($notification->getMessage(), $formData); $headers = $this->buildHeaders($from, $replyTo); $visibleHeaders = $headers; if ($cc !== '') { $visibleHeaders[] = 'Cc: ' . $cc; } // Send email using WordPress function $sent = wp_mail($to, $subject, $message, $visibleHeaders); if ($sent && $bccAddresses !== []) { $sent = $this->sendBccCopies($bccAddresses, $subject, $message, $headers); } return $sent; } /** * Build the headers shared by every copy of the notification. * * @return string[] */ private function buildHeaders(string $from, string $replyTo): array { $headers = [ 'Content-Type: text/html; charset=UTF-8', ]; if (!empty($from)) { $headers[] = 'From: ' . $from; } // If replyTo is set and not identical to From, add Reply-To header if (!empty($replyTo) && $replyTo !== $from) { $headers[] = 'Reply-To: ' . $replyTo; } return $headers; } /** * PHPMailer keeps the Bcc header in the message on the mail()/sendmail/qmail * transports, so a shared send would expose Bcc addresses to the To and Cc * recipients. Each Bcc recipient therefore gets its own copy. * * @param string[] $bccAddresses * @param string[] $headers */ private function sendBccCopies(array $bccAddresses, string $subject, string $message, array $headers): bool { $sent = true; foreach ($bccAddresses as $bccAddress) { $sent = wp_mail($bccAddress, $subject, $message, $headers) && $sent; } return $sent; } /** * Replace template variables in content with actual form data * Supports format like {{field_name}} * * @param string $content Content with template variables * @param mixed[] $formData Form submission data * * @return string Content with replaced variables */ private function replacePlaceholders(string $content, array $formData): string { // Replace field variables like {{field_name}} preg_match_all('/{{(.+?)}}/s', $content, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $key => $fieldName) { $fieldValue = $formData[$fieldName] ?? ''; if (is_array($fieldValue)) { $fieldValue = implode(', ', $fieldValue); } $content = str_replace($matches[0][$key], $fieldValue, $content); } } // Replace special variables $content = str_replace('{{all_fields}}', $this->getAllFieldsHtml($formData), $content); return $content; } /** * Generate HTML table with all form fields for email template * * @param mixed[] $formData Form submission data * * @return string HTML representation of all fields */ private function getAllFieldsHtml(array $formData): string { // Generate HTML table for all fields $html = '
| '; $html .= esc_html($label) . ' | '; $html .= ''; $html .= ''; $html .= esc_html($fieldValue) . ' | '; $html .= ''; $html .= '
|---|