I would like to develop a newsletter sending system using Swiftmailer. I would like to know:
1) Do I require to set a cron job for this ?
2) Batchsend()开发者_Go百科
or Send()
with a loop
, which one is better ( I want each address to be in To field ) ?
With regards to the first question; it's best to create the script to send the next x emails from the recipients list and call it repetitively until all the recipients have been processed. You'll need to keep track of who's been processed in the db and introduce error handling in case it fails; a good strategy is for each user:
- mark in db as processing
- send recipient email
- mark in db as processed
This way you can see how many people failed at the end (i.e. those who still are marked as "processing").
In order to repeat the process you could use CRON and repeat every minute; but what if the sending takes over 1 minute (e.g. due to slow SMTP connection) then you'll have two processes running together so you'll either need to prevent this or introduce some sort of locking (the above example of marking users as "processing" will prevent two concurrent instances of the script from processing the same people).
The other problem with CRON every minute is that it might take you ages to send all your emails. I had this exact problem and so I wrote The Fat Controller which handles parallel processing and repeating for you. I made a simple shell script which is started every day by CRON and runs The Fat Controller which then runs many instances of the PHP sending script. Here are some use cases and more information:
http://fat-controller.sourceforge.net/use-cases.html
With regards to your second question; I'm not sure of the internals of Swift mailer, but you'll want to open an SMTP connection, send emails, then close it - so you're not opening and closing a connection for each email. Check the documentation, I've used Swift before and it worked very well and had very clear documentation.
Off topic I know, but possibly useful:
Have you considered using an email newsletter provider such as http://mailchimp.com/? There's a cost involved obviously, but you have to compare that to the cost of developing and running it yourself, and maintaining it.
If a few of your users decide that your mail is spam, which is guaranteed to happen, regardless of how thoroughly you have obtained consent, then you could easily get onto blacklists, which will seriously impede your mail. The big providers are much less likely to get blacklisted, as they're recognised legitimate mass mailers. They'll also take care of unsubscribe management.
Edit: I am in no way affiliated with MailChimp. It's just the one I've used.
send()
is preferred. In fact, batchSend()
has been removed. Construct a loop and send them all manually.
I do a similar thing for notifications in an ERP application, customised emails basically.
The way I do this is to use Gearman, and queue all the emails to be sent, then have a Gearman worker to do the actual sending. That way you can have multiple workers sending the emails and the front-end doesn't lock up waiting for each email to be sent.
精彩评论