From 96bf160198630fb4f2fd62e49d6496a403417686 Mon Sep 17 00:00:00 2001 From: Finley Ghosh Date: Thu, 20 Nov 2025 22:50:02 +1100 Subject: [PATCH] Trying to fix email sending --- app/controllers/documents_controller.php | 17 +++++++++-------- cake/libs/controller/components/email.php | 8 ++++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/controllers/documents_controller.php b/app/controllers/documents_controller.php index c039aa4b..582d1cba 100755 --- a/app/controllers/documents_controller.php +++ b/app/controllers/documents_controller.php @@ -1205,13 +1205,13 @@ EOT; } - function format_email($email) { - $email = trim($email); - // Basic RFC 5322 email validation - if (!preg_match('/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/', $email)) { - return ''; - } - return "$email <$email>"; + function format_email($email) { + $email = trim($email); + // Basic RFC 5322 email validation + if (!preg_match('/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/', $email)) { + return ''; + } + return $email; } function parse_email_to_array($input) { @@ -1326,7 +1326,8 @@ function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $b echo json_encode(array('success' => false, 'message' => $msg)); return; } else { - $this->Email->to = implode(', ', $toArray); + // Pass as array - modified CakePHP EmailComponent now supports arrays for TO field + $this->Email->to = $toArray; } $ccArray = $this->parse_email_to_array($cc); if (!empty($ccArray)) { diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index b2e2ea42..79d4d53b 100755 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -712,8 +712,12 @@ class EmailComponent extends Object{ return false; } - if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) { - return false; + // Handle both single email (string) and multiple emails (array) for TO field + $toRecipients = is_array($this->to) ? $this->to : array($this->to); + foreach ($toRecipients as $to) { + if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($to, true))) { + return false; + } } foreach ($this->cc as $cc) {