Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email to multiple Recipients with swiftmailer

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.

like image 820
Mario brown Avatar asked Jun 29 '17 14:06

Mario brown


People also ask

How do I send an email to multiple recipients with different content?

After creating your Gmail draft and your spreadsheet, select Extensions > Yet Another Mail Merge: Mail Merge for Gmail > Start Mail Merge. Then select the draft you created in your Gmail account next to Email Template and click Send emails. Each recipient receives an email with personalized content.

How do I send email to multiple users individually?

Open a new email and write the message you intend to send to your contact list. Click BCC in the top-right of your Compose window. Add all the email addresses to which you intend to send the message. It might help to copy and paste your list into this field.

How do I send an email with multiple Cc's?

Add multiple Cc recipients or Bcc recipients It's the same! You can add multiple Cc and Bcc by separating them with comma. Add the recipients in a column called "cc" for Cc recipients and "bcc" for Bcc recipients. To add multiple recipients, Cc or Bcc, just separate them with comma.

Can we send multiple mails using SMTP?

By default, a single SMTP transport creates a single connection and re-uses it for the lifetime of the script execution. You may send multiple e-mails through this SMTP connection.


1 Answers

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

like image 115
Huy Trịnh Avatar answered Sep 25 '22 01:09

Huy Trịnh