Page 1 of 1
Personalizing Email Messages, using tokens in a form
Posted: Tue Dec 05, 2006 8:59 pm
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
Posted: Wed Dec 06, 2006 12:21 am
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")));
}
Posted: Wed Dec 06, 2006 3:36 am
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.
Posted: Wed Dec 06, 2006 7:07 am
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

Posted: Wed Dec 06, 2006 4:33 pm
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
Posted: Mon Dec 11, 2006 6:00 pm
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();
}
Posted: Mon Dec 11, 2006 6:31 pm
by Chris Corbyn
No AntiFlood will work fine with a loop. Just load the plugin before the loop

Posted: Mon Dec 11, 2006 7:50 pm
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
Posted: Mon Dec 11, 2006 8:59 pm
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
Posted: Tue Dec 12, 2006 12:42 am
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

Posted: Mon Dec 18, 2006 12:35 pm
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();