Page 1 of 1

Using BatchMailer and the Decorator Plugin

Posted: Tue Jan 29, 2008 5:00 am
by setblue
Hi everyone,

First of all, I'd like to thank the developpers of this great library and the contributors of this board.
I've been using Swift for a while, and I never experienced a problem which couldn't be solved with a little search in the forums.

But this time, I'm kind of stuck.

Here is my problem. I'm using Batch Mailer to send an email to multiple recipients. It works great. But now, I need to know how many recipients opened the mail. For doing this, I add a picture at the end of the mail content. This is not a real picture, it's just an HTML img tag but the src leads to an url which is url rewrited to another url : a PHP script that extract some info (basically the id of the recipient) from the name of the picture and increment a counter if this ID hasn't open the mail already.

Typically, at the end of the mail, I've got this tag:

Code: Select all

<img src='http://www.mydomain.com/tracking/image-0001-89.jpg' />
This URL is rewritten by the server to

Code: Select all

<img src='http://www.mydomain.com/front_tracking/tracking/0001/89' />
Note : I use Code Igniter Framework which allow me to use a segment based approach for the URL of my website. The above URL leads to a PHP controller of my website that perform the tracking operation.

From there I'm able to get the newsletter ID (0001 in the above example) and the User ID (89 in the above example) and do wat I want to do.

I've been testing this and it works just fine. My problem is I'm not able to make it works with Swift as I need to send a slihtly different email to each recipient (each mail content the user id of a particular recipient).

I've been trying to do it with the Decorator Plug in but I'm not able to make it work so far.

Here is the code I use :

Code: Select all

 
$require1= require_once "Swift.php";
$require2= require_once "Swift/Connection/SMTP.php";
$require3 = require_once "Swift/Plugin/Decorator.php";
//note the require_once paths are OK, I just modified the paths to make Swift work with Code Igniter
$sujet = "Test email";
$corps = "blablablablabla"; // Here is the content of the email
 
$mes_emails= $inscrits['email']; //here are the email address coming from my database
$mes_emails_id = $inscrits['id']; //here are the user ID corresponding to each email address
$nombre_sent= count($inscrits['email']); //keeping track of the number of successfull sent mail
            
//Swift Launch
$swift_inscrits= & new Swift(new Swift_Connection_SMTP("localhost"));   
$message= & new Swift_Message($sujet, $corps."<img src='".base_url()."suivi/image-0001-{id_user}.jpg'>", "text/html");
//Note : base_url() function is a built in method in Code Igniter which returns the first part of the correct URL of my website.
 
//Replacements
//Specify the list of replacements as a 2-d array
$replacements = array();
for($i=0;$i<count($mes_emails);$i++){
    $tab = array($mes_emails[$i] => array("{id_user}" => $mes_emails_id[$i]));
    array_push($replacements,$tab);
}
 
//Load the plugin with these replacements
$swift_inscrits->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
    
//Recipients List        
$destinataires_inscrits =& new Swift_RecipientList();
    for ($i= 0; $i < count($mes_emails); $i++)
    {
        $destinataires_inscrits->addTo($mes_emails[$i], $mes_emails[$i]);
    }
 
//Sending the emails
$envoi= & new Swift_BatchMailer($swift_inscrits);
$envoi->send($message, $destinataires_inscrits, new Swift_Address("nepasrepondre@mydomain.com", "My Website Name"));
$swift_inscrits->disconnect();
When I try to send emails using the code above, the img src URL in the email content just look like :
<img src="http://www.mydomain.com/suivi/image-0001-{id_user}.jpg">
The {id_user} part isn't replaced by the correct id number as expected.

I guess I probably forgot something somewhere, I just can't find where. Any help would be really appreciated :)

Re: Using BatchMailer and the Decorator Plugin

Posted: Tue Jan 29, 2008 8:44 am
by John Cartwright
Bonjour!

The problem with your code is you are not properly formatting the decorator's replacement array.

What you want:

Code: Select all

foobar = array(
   email1 => array(replacementtag => text)
   email2 => array(replacementtag2 => text2) 
)
your your doing

Code: Select all

 
foobar = array(
   0 => array(email1 => array(replacementtag => text))
   1 => array(email2 => array(replacementtag => text))
)
 
Try replacing your array_push() with array_merge()

Re: Using BatchMailer and the Decorator Plugin

Posted: Tue Jan 29, 2008 9:01 am
by setblue
It did the trick 8)

Thank you very much for your help, mate !