Question

I'm developping a newsletter module in my website and i begin with Symfony 1.4 version. I would like to send my emails to multiple recipients.

I tried this simple solution (with the realtime delivery strategy):

$message = $this->getMailer()->compose();
$message->setSubject('My subject');
$message->setTo(array('email1@domain.com', 'email2@domain.com'));
$message->setFrom('expeditor@domain.com', 'Name of expeditor');
$html = $this->getPartial('module/action', array('var'=>$var));
$message->setBody($html, 'text/html');
$number = $this->getMailer()->send($message);

The email was sent but in the list of recipients you can see all users and not only you ...

Moreover it is possible there are a lot of recipients. So maybe i will need a waiting list to avoid a max execution time error ?

May you help to find a better solution with example if it's possible ?

Thank you

Was it helpful?

Solution

First, to hide the recipient list you must use the setBcc() function of Swiftmailer instead of setTo(). This adds the e-mail addresses as blind-carbon-copy repicients, which means they won't be able to "see" each other. Additionally, add your own address (for example noreply@domain.com) as the setTo() address.

Second, the max execution time won't be a problem if you add all recpients to just one e-mail, as it will only connect to the underlying SMTP/mailer once, when you call $this->getMailer()->send($message).

I would however advise you to look at the spool functionality, as it offers much better performance, and lets a background process take care of the actual emailing (you just add to a table in your database and let it wait for the cronjob to send the mail). Change your code above to loop through all recipients and create one e-mail for each recipient and add it to the spool. This way you can also use the setTo() function to get the correct to-address showing for the recipient.

Read more about the spool functionality in the More With Symfony book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top