Personalizing Email Messages, using tokens in a form

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
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Personalizing Email Messages, using tokens in a form

Post by jbh »

Hey,

I love this script. And I am playing with it now. I noticed the batch email demo works perfectly. However, I do have one question.

Imagine sending a message to your list, using that kind of demo:
http://www.swiftmailer.org/docs/tutorials/batch-mailing

but you submit the subject/message through a form

Now, what I can do is make the subject/message a database record and pull them as wel

Can I just create a var, such as

Code: Select all

$subject=$row["subject"]; and so on for $message
And if so, where would I use str_replace to replace tokens, such as [firstname] (I put that in the form box, before submission, so it displays first name]
?

I assume this is possible with your script?

I'll keep testing, but I just wanted to ask, just in case somebody has done this before me.

Note: I realize it might have to do with the template.php file's class, but I am not sure HOW to implement it.

Thank you very much

Joel
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's not possible to replace the headers in version 2, and therefore not possible to replace the subject. The best alternative is to just call send() in a loop.

Version 3 solves this by dealing with caching when you use a loop. That will need to wait around a week or so however.

In version 3 you'll be able to do this at little expense:

Code: Select all

while ($resultset->next())
{
    $message->setSubject("Hi " . $resultset->get("username"));
    $swift->send($message, new Swift_Address($resultset->get("address")));
}
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Post by jbh »

So, in the body message, there can't be variables?
Such as first name? I figured one could, if we could add images to the message. But I'm not sure.

That's all I wanted to do (subject was a bad example)

Just wanted to make sure. If not until vers 3, I can wait a week

But I just wanted to clarify. Thank you for this great script and for your time.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah you can do it in the body just fine :)

Search this form for "Swift template plugin" with the "All Terms" radio button checked. You'll find a few threads containing what you need :)
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Post by jbh »

I did, but I'm still very new at this.

I'll keep experimenting, but I know many new users would take off with one demo. And this would fly as the best solution. I love this script.

Thank you
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Post by jbh »

If I can't personalize, does it affect the flood prevention if I do a
loop with the following code being called throughout each turn?

Code: Select all

if (!$swift->hasFailed())

{


$swift->send($recipients, '"Joel Holtzman <joel@losttrafficsaver.com>', 'Subject', $message);

$swift->close();

}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No AntiFlood will work fine with a loop. Just load the plugin before the loop ;)
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Post by jbh »

lol

Yeah, I'll just do my cheesey while loop ,declare var for firstname, such as
$firstname=$row["firstname"];

and in that loop do the code I posted above.

Thank goodness. (If I am mistaken, laugh at me now. Otherwise, I will test it now) ;0

Thank you so much
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Post by jbh »

This won't work

I can send the message to unique email addys ,but I can't use a while loop to change the $message var to show unique first name, even though I can get a unique first name in each loop

Code: Select all

if (!$swift->hasFailed())

{



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

 
$recipients=$row["email"];


$message=str_replace("[firstname]",$firstname,$message);

$swift->send($recipients, '"Joel Holtzman <joel@losttrafficsaver.com>', 'Subject', $message);

}

$swift->close();

}
It just cannot turn the [firstname] token in my message and replace it with the unique value for $firstname for each loop, even though
I can email a unique email

Weird...I'd pay to have this fixed. I'm losing $ trying to get it to work

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

Post by Chris Corbyn »

You replace $message with a new string the first time and therefore the [firstname] but disappears from the string. You'll need to copy $message to $copied_message before you make a replacement ;)
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Post by jbh »

This is what I have, that is working. Feel free to laugh at it ;0
(Note: the flood function, db connect and the include files for swiftmailer are left out. Below starts the main work to personalize

It seems to work. It takes a while to load, but even if I abort, it works in the background, of course. I assume even with 500/1000
in the list it would work fine. Just fyi for anybody who would want to do this. And feel free to criticize it if there is a better way with swift mailer 2+

Thanks

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();
Post Reply