Connecting Swiftmailer up to DB

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
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Connecting Swiftmailer up to DB

Post by Noobie »

Hi

I installed Swiftmailer 3 yesterday on PHP4 without too much issue (I'm a bit of a Noobie - hence the name!). There was a lot of jumping up and down and cheering I can tell you (I'm easily pleased).

I'd like to connect up the Batch Send with Attachment version to my MySQL DB to send out a Newsletter - what's the best way of doing that?

Dont suppose it's as simple as

Code: Select all

$recipients =& new Swift_RecipientList();
$recipients->addTo($recipient_emails);
Also - I noticed this thread: viewtopic.php?t=64614 about an mid-send error issue, should I change that chunk of code in the version I'm running (if so... which file does it go in?).

Thanks for your help.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can probably just run the DB query, then when you loop through the resultset send each item from inside the loop. Just make sure you create the message object outside the loop.

Code: Select all

$result = mysql_query($query);

$message =& new Swift_Message( .... );
$message->attach(new Swift_Message_Attachment( .... ));

while ($row = mysql_fetch_assoc($result))
{
    $swift->send($message, $row["address"], $your_email);
}
You can also use the batchSend() method but it's exactly the same really... Proof :P

Code: Select all

//From the batchSend() method
                $ret = 0;
                foreach ($to->getTo() as $address)
                {
                        $ret += $this->send($message, $address, $from);
                }
                return $ret;
I have all kinds of plugins planned to build using the new plugin API over the next few months... including mailing list plugins (via DB), template plugins, throttlers....
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thanks Chris - I'll give this a go soon as I can!
Post Reply