Swiftmailer Code Examples for Batch Mail using Cron?

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
nlewis
Forum Newbie
Posts: 3
Joined: Tue Jan 29, 2008 3:27 pm

Swiftmailer Code Examples for Batch Mail using Cron?

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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]
nlewis
Forum Newbie
Posts: 3
Joined: Tue Jan 29, 2008 3:27 pm

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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.
nlewis
Forum Newbie
Posts: 3
Joined: Tue Jan 29, 2008 3:27 pm

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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 ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Swiftmailer Code Examples for Batch Mail using Cron?

Post 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.
Post Reply