Page 1 of 1

Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Jan 29, 2008 3:35 pm
by nlewis
Hello Everyone,
I just found out about Swiftmailer about Noon Today. The past 4 Hours I've been combing this forum trying to absorb as MUCH information as I can. I'm not the most saavy php programmer, in fact I'm not one at all. But I do understand SOME of the concepts, at least enough to implement. However what helps me most is examples. So I'm asking a few more experienced users to help me out a bit?

I Have a E-Mail list of about 6000 Names. 3-4 Times a month, I'd like to send out an HTML Email to these addresses. I've read the docs and the forums and understand that BatchSend() along with the Throttler Plugin can make this possible. However, I'm COMPLETELY confused as to how to create a CRON script to pull in each email in small batches. Can someone post an example of how to setup this cron script? I've never used Crontab in the past, but do have a dedicated server and I'd like to learn how.

How do I setup cron to pull the emails from the DB? How do I setup the DB? I can setup a db table with 2 columns; Name and Email, but is it that simple? How do I tell Swiftmailer to connect to this DB, and ultimately tell the cron script to pull those emails into Swiftmailer?

I know these probabaly are all BASIC questions. But I'm Extremly excited about LEAVING PhpMail and moving over to Swiftmailer. Go easy on me, I'm just looking to learn! I'd appreciate any examples and explanations, I'll be checking the forums often.

B.regards,
Nick

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Wed Jan 30, 2008 12:16 am
by Chris Corbyn
It's almost as simple as your two columns for name and email yes. The only other things you need are two more columns: one which identifies a particular batch being sent (could just be a name, or an incremented number), and another which marks that particular address as sent or unsent.

Your cron script would do something like:

[sql]SELECT name, email FROM mailqueue WHERE flag = 'unsent' AND batchid = 'some-id' LIMT 100[/sql]

Then send to all those addresses and update each one in turn setting the flag to 'sent'. Each time the script runs there will be less addresses to send to since they'll gradually be marked off as sent.

You'll also need a way of linking a specific message with that batchid so you know what content the email needs.... that could just be a file on disk with the same name as the batchid, or another table in the database (preferred).

[sql]SELECT subject, body FROM messages WHERE batchid = 'some-id'[/sql]

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Feb 05, 2008 9:00 am
by nlewis
Hmm... ok i can grasp that? I'm a bit confused as to HOW Swiftmailer connects to the DB tho? Where do I provide the db name / password details? I plan on creating a new DB specifically for swiftmailer. Can someone elaborate?

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Feb 05, 2008 1:10 pm
by John Cartwright
Swift does not connect to the mailer itself. You query the database elsewhere in the script, iterate the results and add the address to Swift_RecipientList::addTo() method.

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Feb 05, 2008 1:37 pm
by nlewis
Jcart, Thanks for the reply.

The part im hung up on is how to tell the script to query the DB, as in where do i give it the DB details? Is there anyway you can provide me with an example of what you mean?

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Feb 05, 2008 4:31 pm
by Chris Corbyn
Swift won't do that for you. Swift only cares about email. Using MySQL databases is a completely separate topic all together. Probably better off googling for "mysql tutorial php" or something ;)

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Feb 05, 2008 11:01 pm
by John Cartwright
Jcart wrote:Swift does not connect to the mailer itself. You query the database elsewhere in the script, iterate the results and add the address to Swift_RecipientList::addTo() method.
I mean Swift does not connect to the database itself

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Posted: Tue Feb 05, 2008 11:22 pm
by Kieran Huggins
There's some example code here (in production, works):
viewtopic.php?f=52&t=76722&p=439267#p439267

The MySQL tables are pretty straight forward from the example, but I can find and post them if needs be.