Page 1 of 1

Customizing email for each recipient

Posted: Mon Mar 10, 2008 2:00 pm
by sahaishivam
I am using swift to send customized emails to email addressess taken from db. I am using 'Decorator' plugin to customize each email. I have modified the following code;

Code: Select all

//Load in the files we'll need
require_once "email_swift/lib/Swift.php";
require_once "email_swift/lib/Swift/Connection/SMTP.php";
require_once "email_swift/lib/Swift/Plugin/Decorator.php";
//Start Swift
//Connect to Gmail (PHP5)
 
 $smtp =& new Swift_Connection_SMTP("smtp.gmail.com", 465, Swift_Connection_SMTP::ENC_TLS);
 $smtp->setUsername('xxx@gmail.com');
 $smtp->setPassword('xxx');
 $swift =& new Swift($smtp);
 
//Create the message, using some unique variables to search for
$message_body=$_GET["emailbody"];
$message_subject=$_GET["subject"];
$message =& new Swift_Message($message_subject, $message_body);
 
//add recepients
$recipients =& new Swift_RecipientList();
 
////add to fields
 
 $recipients_tofield_array =$_GET["tofield"]; echo "<br>";
    if($recipients_tofield_array!=''){
             $recipients_tofield_array = explode(",", $recipients_tofield_array); echo "<br>";
            //print_r($recipients_tofield_array); echo "<br>";
            echo $numberof_tofield = count($recipients_tofield_array);
            for($i=0;$i<$numberof_tofield;$i++){
                //echo "for ex for $i";
            //  echo $recipients_tofield_array[$i]; echo "<br>";
                $recipients->addTo($recipients_tofield_array[$i]);  
            } 
        }
//
////add cc fields
 
 $recipients_ccfield_array =$_GET["ccfield"]; echo "<br>";
    if($recipients_ccfield_array!=''){
             $recipients_ccfield_array = explode(",", $recipients_ccfield_array); echo "<br>";
            //print_r($recipients_ccfield_array); echo "<br>";
            echo $numberof_ccfield = count($recipients_ccfield_array);
            for($i=0;$i<$numberof_ccfield;$i++){
            //  echo "for ex for $i";
            //  echo $recipients_ccfield_array[$i]; echo "<br>";
                $recipients->addCc($recipients_ccfield_array[$i]);  
            } 
        }
//add bcc fields
 
 $recipients_bccfield_array =$_GET["bccfield"]; echo "<br>";
    if($recipients_bccfield_array!=''){
             $recipients_bccfield_array = explode(",", $recipients_bccfield_array); echo "<br>";
            //print_r($recipients_bccfield_array); echo "<br>";
            echo $numberof_bccfield = count($recipients_bccfield_array);
            for($i=0;$i<$numberof_bccfield;$i++){
            //  echo "for ex for $i";
            //  echo $recipients_bccfield_array[$i]; echo "<br>";
                $recipients->addBcc("shivam.sahai@yahoo.com", "Mike");  
            } 
            //an email sent to self for bcc to work
            $recipients->addTo("vvv@gmail.com", "Mike");    
            //an email sent to self for bcc to work ends
        }
    echo $total_intended_recipients=$numberof_tofield + $numberof_ccfield + $numberof_bccfield;
    //if more than 30 email ids are there, do not send the emails
    if($total_intended_recipients>30){
        echo "You cannot add more than 30 email ids at one time. <br><br>";?>
        <input type="button" name="back" value="Back"  onclick="javascript&#058;history.back();" />
    <?php
        exit(); 
    }
    
    
    //The number of successful recipients is returned here 
    
    $swift->Send($message, $recipients, "xxx@gmail.com");   
    echo "Email Successfully sent to"; echo $total_intended_recipients; echo " email id(s)";
     ?>
As you see, i am using the emailaddress from db as the index value of the array which is passed to the send() function. Also the recipients are stored using Swift_RecipientList(). My problem is if i use the array as it is, i cant send a customized email message because for that i need to use multiple send functions and provide each with a different emailid that increases the burden and time multiple times. With that said, please guide me as to how can i use a single send() function and use Swift_RecipientList() to provide send() with a different email id every time. Otherwise my problem will be half solved bcos i really cant afford making multipe send() calls to increase the load.

Waiting in anticipation
SHivam

Re: Customizing email for each recipient

Posted: Mon Mar 10, 2008 9:17 pm
by Chris Corbyn
The batchSend() and BatchMailer approaches both call send() over and over. There's no load increase since the connect is persistent and the message is cached at many levels. Just re-use the same message but change elements of it on each iteration.

Re: Customizing email for each recipient

Posted: Thu Mar 13, 2008 6:20 pm
by gmorehoudh
Can Decorator be used to decorate headers?

Re: Customizing email for each recipient

Posted: Fri Mar 14, 2008 12:28 am
by Chris Corbyn
gmorehoudh wrote:Can Decorator be used to decorate headers?
Some headers yes (Subject and Attachment filename).

Re: Customizing email for each recipient

Posted: Fri Mar 14, 2008 2:59 pm
by gmorehoudh
Mmm, that makes things hard from a VERP point of view. :/

Re: Customizing email for each recipient

Posted: Fri Mar 14, 2008 9:06 pm
by Chris Corbyn
gmorehoudh wrote:Mmm, that makes things hard from a VERP point of view. :/
If you only want to change the return-path for each recipient then you can use a plugin whih does the replacement in a BeforeSendListener. Quite literally call $message->setReturnPath($newPath).

More on plugins here:

http://www.swiftmailer.org/wikidocs/ (See "Writing Plugins (Advanced)")

Re: Customizing email for each recipient

Posted: Mon Mar 17, 2008 11:42 am
by gmorehoudh
Thanks for the excellent tip. :)

Re: Customizing email for each recipient

Posted: Thu Mar 27, 2008 7:03 pm
by gmorehoudh
getTo() doesn't appear to be working when I actually tried to implement this.

Code: Select all

   public function beforeSendPerformed(Swift_Events_SendEvent $e) {
        $message = $e->getMessage();
        $to_list = $message->getTo();
        // if addressed to more than one person, we can't verp it
        if(count($to_list) > 1)
            die("Can't VERP multiple recipients.");
        else
            // !!! make actually do something
            print_r($to_list);
    }
An empty array is returned by getTo() even though the message is getting sent properly, so obviously the To field is properly assigned.

Re: Customizing email for each recipient

Posted: Thu Mar 27, 2008 10:20 pm
by Chris Corbyn

Code: Select all

$recipients = $e->getRecipients();
$to = $recipients->getTo();
I don't know why I designed it this way... in the forthcoming version you simply do $message->setTo() to provide recipients rather than having a separate list. The above works for v3 though.

The ability to use an iterator will be optional in v4 however:

Code: Select all

//Normal
$message->setTo(array($ofRecipients));
 
$mailer->send($message);
 
//Using an iterator (to read from MySQL for example)
$mailer->send($message, $myIterator);