Sending email to multiple Recipients with swiftmailer

Mario brown picture Mario brown · Jun 29, 2017 · Viewed 9.9k times · Source

I am trying to use swiftmailer in my project so that I can send html newsletter to multiple users. I have searched thoroughly but all i got never worked for me. I want to paste more than one recipient in the form input field seperated by comma and send the html email to them. I set the recipients to a variable($recipients_emails) and pass it to setTo() method in the sending code, same with the html_email.

Questions are: Q1 How do i send to more than one recipient from the recipient input field.

I tried this:

if (isset($_POST['recipients_emails'])) {
    $recipients_emails  = array($_POST['recipients_emails'] );
    $recipients_emails= implode(',',$recipients_emails);
}

Q2 How do I make the Html within heredoc tag. when i tried concatenating like this ->setBody('<<<EOT'.$html_email.'EOT;', 'text/html'); , my message would appears with the heredoc tag.

if (isset($_POST['html_email'])) {
    $html_email = $_POST['html_email'];
}

How do I have input from $_POST['html_email']; to be within EOT tag;

this in part of swiftmailer sending script ;

$message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($recipients_emails)
        ->setBody($html_email, 'text/html');

Nota bene : Am still learning these things.

Answer

Huy Trịnh picture Huy Trịnh · Jun 29, 2017

According to this document

// Using setTo() to set all recipients in one go
$message->setTo([
  '[email protected]',
  '[email protected]' => 'Person 2 Name',
  '[email protected]',
  '[email protected]',
  '[email protected]' => 'Person 5 Name'
]);

You can input array directly into setTo, setCc or setBcc function, do not need to convert it into string