Forum Moderators: coopster

Message Too Old, No Replies

Creating Automated Emails with Merge Fields

Thanks in advance for all help!

         

realestatesteve

3:04 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



Hi everyone!
I've built my first web application, a contact manager, and am now wanting to add a feature that I can't get my head around for some reason. (It's probably simple, heh)

I basically want to create a way for users to create email templates with merge fields, schedule them for contacts, and then setup those emails to automatically send to a contact in their database in the morning on the day that email is scheduled to be sent.

I've learned how to send emails, and that's working.

But first things first...Merge fields?

In the email template, the user enters this - {firstname} - and the server will see that the email needs to have the first name of the contact that the email is being sent to.

How in the world do I accomplish this?!

I'm imagining it happens after the user would submit the form, and then before I get $_POST['message'] ready to be added to the echo that will send the form, pull the data you need into that field.

But how would I get the script to work without having the user write complicated variables like <?php echo $firstname ?> inside of their email?

cameraman

4:34 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, realestatesteve.

There's at least a couple of ways you could do it. The first is to use explode [us2.php.net] on your { } delimiters. What you would do is first explode on { and then explode on } for each of those elements to isolate your field names.

Another way is to use a regular expression [php.net]. This way looks more complicated because the functionality is all in one line of script. The disadvantage [for me] is decyphering the regular expression in the future if I ever have a need to - I put together the expression below but in 6 months I'll have no idea what it does - I'd have to look at the surrounding script to figure it out, then it would take me several minutes of head-scratching to figure out how the expression works. The snippet below assumes you have your field values from your database as an associative array called $data_array.

if(preg_match_all [php.net]('#(?<!\\)\{([^\}]+)\}#',$mailbody,$fields)) {
foreach($fields[1] as $field)
$mailbody = str_replace("\{$field\}",$data_array[$field],$mailbody);
$mailbody = str_replace('\{','{',$mailbody);
}

Looking at that complete mess that is the first argument to preg_match_all, let's skip over the first part. This part: \{([^\}]+)
looks for a starting { followed by anything that isn't a } and the plus sign means keep on looking (as opposed to stopping after one character). The parentheses that surround it mean to capture that - it gets placed into $fields, which becomes a multidimensional array. The backslashes in front of the braces are needed because braces have special meaning in regular expressions.

The first part: ?<!\\
is a look-behind assertion that says not to include 'braced' elements that start with a backslash - that allows your users to use braces in their emails if they need to. For example, if $mailbody contains:
Hello {firstname}, just a reminder that I will be by at {time} to discuss the plan \{my manager will attend as well}.

You'll wind up with firstname and time as elements in the $fields array, but not that last phrase. The second str_replace then removes the \ characters that will be left in the mail body.

You could use a second regular expression along with preg_replace [php.net] to do the replacing, but to me there's not any real advantage and in 6 months the two expressions would be too much for my brain to deal with ;)

realestatesteve

4:49 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



Holy crap, Cameraman. You are awesome! I got referred here to get help when I get stuck, and it looks like I really got lucky finding someone to help me like yourself.

Thanks again; let me go give this a shot! :)

[edited by: eelixduppy at 6:51 pm (utc) on Dec. 17, 2008]

realestatesteve

4:55 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



So of the two methods you provided, you would give the explode method a shot first? Seems way easier to implement and as you say; may be better for the long run.

cameraman

6:59 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been leaning more toward regular expressions because I figure it's the only way I'll increase my comfort level.

I don't think you'll have any trouble with the expression I posted, but there's not much worse than having a section of code that you have no idea how it works and no hope of modifying for future requirements. If the explanation makes sense then you could paste the whole thing into your script as a comment so you can reread it in the future if necessary. If not, definitely go with the explode. Ultimately, go with what you're going to be more comfortable working with and maintaining.

realestatesteve

7:10 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



I've gotten it to work! I decided to use * before and after the field name to save the double explode trouble with {}. I don't think it'll be an issue.

I've gotten it to work like I want it - thank you so much!