Page 1 of 1

Is this code proper for newsletter/personalizing with ver. 3

Posted: Fri Feb 23, 2007 10:11 am
by jbh
Hey,

How do I improve the code/performance for php 4 version 3 compared to my OLD method of personalizing with a newsletter?
I just want to make sure I do this correctly. Is this type of code still allowed? (Note, I will use batchsend instead of 'send')

And I will gladly donate once this works, as I realize how much of a PITA it is to develop

Code: Select all

set_time_limit(0); ignore_user_abort();

flush(); ob_flush();


$sql="Select firstname,lastname,email,IP,joindate from pt_mailtemp";
$result=mysql_query($sql);




if (!$swift->hasFailed())

{



while($row=mysql_fetch_array($result))
{
$firstname=$row["firstname"];


 
$recipients=$row["email"];
$ip=$row["IP"];
$joindate=$row["joindate"];

$message=$_POST["message"];
$subject=$_POST["subject"];
$message=str_replace("[firstname]",$firstname,$message);
$subject=str_replace("[firstname]",$firstname,$subject);
$message=str_replace("[ip]",$ip,$message);
$message=str_replace("[joindate]",$joindate,$message);


$swift->send($recipients, '"John Smith<johnsmith@lblahblah.com>', $subject, $message);

}

$swift->close();

Posted: Fri Feb 23, 2007 1:22 pm
by Chris Corbyn
It will work with EasySwift yes. But I wouldn't advise that method. I'd say to use Swift itself, create *one* message object then loop over all your recipients and update the message object for each one.

Code: Select all

$message =& new Swift_Message();

while ($row = mysql_fetch_array($result)) 
{ 
    $firstname = $row["firstname"]; 
    $recipients = $row["email"]; 
    $ip = $row["IP"]; 
    $joindate = $row["joindate"]; 

    $body = $_POST["message"];
    $subject = $_POST["subject"];
    
    $replacements = array(
        "[firstname]" => $firstname,
        "[ip]" => $ip,
        "[joindate]" => $joindate
    );
    $message->setBody(str_replace(array_keys($replacements), array_values($replacements), $body));
    $message->setSubject(str_replace(array_keys($replacements), array_values($replacements), $subject));
    
    $swift->send($message, $recipients,  new Swift_Address("johnsmith@lblahblah.com", "John Smith")); 
}

Posted: Fri Feb 23, 2007 3:22 pm
by jbh
Interesting. And thank you.

3 last stupid questions so I won't have to bug ya again ;)

A.) Can the 'header' info be variables? (Which can be set by me in a config file, so I don't have to hard code it

B.) Should I be using batchsend and not send, though? Maybe I misunderstood the instruction manual. With your example, I assume NO as you realize my goal, but I just wanted to clarify the purpose/difference

C.) Are you god?

Thank you for everything.

Posted: Fri Feb 23, 2007 5:25 pm
by Chris Corbyn
jbh wrote:A.) Can the 'header' info be variables? (Which can be set by me in a config file, so I don't have to hard code it
Version 3 has set...() methods for the headers yes. You can either do $message->header->set(...), or for the common headers do $message->set...(....). The headers cache themselves.... don't worry about doing this. Tody I sent a 100MB attachment and used only 1MB of PHP's memory (in svn... update coming soon)... I can sit back and relax, so you can too ;)
jbh wrote:B.) Should I be using batchsend and not send, though? Maybe I misunderstood the instruction manual. With your example, I assume NO as you realize my goal, but I just wanted to clarify the purpose/difference
If you open up Swift.php and look at batchSend() you'll see it makes no difference. batchSend() exists as a placebo more than anything else :P I'm having a hard time convincing people the caching (the bits that speeds things up) as happening in the message object, not in the Swift class.
jbh wrote:C.) Are you god?
I am not.. but my housemate calls me Christopharian... I'm still not sure why :? :lol: