Sending email to multiple Recipients with swiftmailer
Asked Answered
R

3

6

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.

Remunerate answered 29/6, 2017 at 14:52 Comment(1)
I know this is old, but aren't you using implode incorrectly? Shouldn't the parameters be array and then the separator? It seems they are backwardsHeifetz
G
8

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

Gameto answered 29/6, 2017 at 15:4 Comment(1)
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 .Remunerate
E
1

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
 }
}
Eskilstuna answered 29/6, 2017 at 15:10 Comment(2)
I thought emails should be comma delimited rather than semicolon ?Remunerate
I don't know. But you can use multiple Delimiters with preg_split() if needed.Eskilstuna
S
0

Ok so first of all you need to create a variable that form your heredoc contents. The end tag of heredoc has to have no whitespace before it and then use that variable. (to output variables inside heredoc you need to wrap them with curly braces) see example..

$body = <<<EOT
 Here is the email {$html_email}

EOT;

$message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($from)
        ->setTo($recipients_emails)
        ->setBody($body, 'text/html');
Shinbone answered 29/6, 2017 at 15:1 Comment(4)
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.Remunerate
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.Shinbone
OK now i got it. Thanks that 's solved . Next it passing the recipients as an array of emails.Remunerate
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/iapnnaWSR9iqYrMnShinbone

© 2022 - 2024 — McMap. All rights reserved.