Forum Moderators: coopster
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?
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 ;)
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.