Sending email to multiple Recipients with swiftmailer

12,319

Solution 1

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

Solution 2

You should validate the input-data by first exploding them into single E-Mail-Adresses and push the valid Data into an Array. After this you can suppy the generated Array to setTo().

<input type="text" name="recipients" value="[email protected];[email protected];...">

On Submit

$recipients = array();

$emails = preg_split('/[;,]/', $_POST['recipients']);
foreach($emails as $email){
 //check and trim the Data
 if($valid){
  $recipients[] = trim($email);
  // do something else if valid
 }else{
  // Error-Handling goes here
 }
}
Share:
12,319
Mario brown
Author by

Mario brown

Updated on June 18, 2022

Comments

  • Mario brown
    Mario brown almost 2 years

    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.

  • Mario brown
    Mario brown almost 7 years
    I know if i set the whole html within heredoc tag and assign to a variable , it would work, but what am looking at is having to paste the html code into the message field and make it be within heredoc before sending to a user email. It works without the heredoc tag but am not sure its advisable to do without the tag. If I try adding heredoc withing the message field , it all appears like text in the inbox. which means it is not processed by php.
  • Kirk
    Kirk almost 7 years
    I am not 100% sure what you are asking there. You can have a 'template' in your php code and just replace variables in the heredoc using curly braces as demonstrated above. You can only use heredoc when assigning to a variable its the ONLY way it works.
  • Mario brown
    Mario brown almost 7 years
    OK now i got it. Thanks that 's solved . Next it passing the recipients as an array of emails.
  • Mario brown
    Mario brown almost 7 years
    I don't want to do it that way. I don't need to paste recipients in the within the code before i would send to them. For example I have 100 emails to sent to , would i have to setTo each of the emails .
  • Kirk
    Kirk almost 7 years
    Just change to...... implode(" ',' ",$recipients_emails); but cast to a variable first so you can prepend and append the single quotes... look here tehplayground.com/iapnnaWSR9iqYrMn
  • Mario brown
    Mario brown almost 7 years
    I thought emails should be comma delimited rather than semicolon ?
  • Bernhard
    Bernhard almost 7 years
    I don't know. But you can use multiple Delimiters with preg_split() if needed.