Page 1 of 1

Connecting Swiftmailer up to DB

Posted: Tue Mar 13, 2007 1:31 pm
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.

Posted: Tue Mar 13, 2007 5:41 pm
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....

Posted: Wed Mar 14, 2007 11:14 am
by Noobie
Thanks Chris - I'll give this a go soon as I can!