Search code examples
javascriptphphtmlfirebaseswiftmailer

swiftmailer email error with multiple emails


I keep getting a fatal error message when getting multiple emails from firebase to php (passing data from firebase to HTML then to html to PHP)

Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [Person1@outlook.com,Person2@gmail.com] does not comply with RFC 2822, 3.6.2

I tried getting a single email from firebase and it worked perfectly, but multiple emails with any email addressed I used the same issue arise

<?php

require '/Vendor/Mail/lib/swift_required.php';

// validation expected data exists if required later
if (!isset($_POST['agent_e'])  {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$email_to = $_POST['agent_e']; // required
$headers .= "MIME-Version: 1.0\r\n";

$mailer   = Swift_Mailer::newInstance($transport);
$message  = Swift_Message::newInstance('')
    ->setSubject($subject)
    ->setFrom(array('mailer@outlook.com' => 'mailer'))
    ->setTo(array($email_to))
    ->setBody('<html>' .
' <body>' .
'  ' . // Embed the file
$messageBody .
'  ' .
' </body>' .
'</html>',
'text/html' // Mark the content-type as HTML
);

// Send the message
$result = $mailer->send($message);

Emails from database example :

enter image description here

Error :

enter image description here


Solution

  • i solved the problem with Splitting the emails i got from firebase to an array

    as below code :

    $email_to = str_replace(' ','',$email_to);
    $email_to = (string) $email_to;
    $email_to = explode(',',$email_to);