On a little bit more thinking I came up with this little mod that seems to work quite well--something really useful and missing in PHPlist so far.
This mod allows you to include one type of text for use when a certain attribute exists for a certain user, and an alternative text to use if that attribute doesn't exist.
So you can do things like:
Dear Jane, [First Name Jane in database]
Dear Harold, [First Name Harold in database]
Greetings! [for the person with no First Name in the database]
The syntax works like this:
- Code: Select all
{CONDITIONAL|[FIRST NAME]|Dear [FIRST NAME],|Greetings!}
This includes "Dear [FIRST NAME]," if [FIRST NAME] exists or "Greetings!" otherwise. Note use of the {} to set off the entire conditional statement and the | character to separate the attribute, the text to use if the attribute exists, and the text to use if it does not exist.
Sample PHPlist message:
- Code: Select all
{CONDITIONAL|[FIRST NAME]|Dear [FIRST NAME],|Good morning, O anonymous one,}
I am so pleased to be writing you today, {CONDITIONAL|[FIRST NAME]|[FIRST NAME]|O Thou Person Whose Name I Do Not Know}.
Your last name is {CONDITIONAL|[LAST NAME]|[LAST NAME]|something I do not know}.
Your middle name is {CONDITIONAL|[MIDDLE NAME]|[MIDDLE NAME]|a complete mystery}.
{CONDITIONAL|[ADDRESS1]||
We do not know your address and it is very important that you email us with that information today!
If we don't know your address we can't send you the $1 million check that is waiting for you here at our office. because we can scarcely email it to [EMAIL]!!!!!!!
}
[IMPORTANT! Note that the above example will work only if you have attributes named FIRST NAME, LAST NAME, MIDDLE NAME, and ADDRESS1 in your PHPlist installation. Those examples are attributes I have in my PHPlist system, but your attribute names will most likely be different.]As in the last example, you can completely omit certain text if a certain attribute is present, or omit it if the attribute is absent.
You can also include attributes within any part of the text (the text to include if that attribute is present, or the text to include if it is absent).
In short, you can tailor your message much more the way you can when doing mail merge in a word processor.
To make this mod find sendemaillib.php. Make a backup of it.
About line 346 of sendemaillib.php find these lines,
- Code: Select all
$destinationemail = '';
if (is_array($user_att_values)) {
foreach ($user_att_values as $att_name => $att_value) {
Replace those lines with this:
- Code: Select all
$destinationemail = '';
//bhugh, do the "CONDITIONAL" substitution thing for messages, 9-30-2007
//Works like this, in email body:
//{CONDITIONAL Dear [FIRST NAME]|Greetings!} includes "Dear [FIRST NAME]," if [FIRST NAME] exists
//or "Greetings!" otherwise
//Works for any attribute name, upper case, in brackets [LAST NAME], [ZIP], etc.
//upcase the keys here because the format within email messages is the [UPPER CASE]
$user_att_values_upper=array_change_key_case($user_att_values, CASE_UPPER);
//do conditional substitution for HTML message:
if (preg_match_all ('/\{CONDITIONAL\|\[(.*?)\]\|(.*?)\|(.*?)\}/si',
$htmlmessage,$matches, PREG_SET_ORDER))
{
foreach ($matches as $regs) {
$conditional=""; # if the attribute isn't found or there are no attributes we will still just erase the matching conditional statement
if (is_array($user_att_values_upper)) {
if ( array_key_exists( $regs[1], $user_att_values_upper )) {
$att_value=$user_att_values_upper["$regs[1]"];
if ($att_value<>"") {
//note: no need to replace [ATTRIBUTE] placeholders here, it will be done later
$conditional=$regs[2];
} else {
$conditional=$regs[3];
}
}
}
//now substitute the conditional expression into the message (or erase it if no matching attributes found)
$htmlmessage = str_replace($regs[0] , $conditional, $htmlmessage);
}
}
//now do conditional substitution for TEXT message:
if (preg_match_all ('/\{CONDITIONAL\|\[(.*?)\]\|(.*?)\|(.*?)\}/si',
$textmessage,$matches, PREG_SET_ORDER))
{
foreach ($matches as $regs) {
$conditional=""; # if the attribute isn't found or there are no attributes we will still just erase the matching conditional statement
if (is_array($user_att_values_upper)) {
if ( array_key_exists( $regs[1], $user_att_values_upper )) {
$att_value=$user_att_values_upper["$regs[1]"];
if ($att_value</note> $att_value) {
This works with PHPlist ver 2.10.2 but probably also all the other 2.10.x and 2.11.x versions.
I haven't tested it very extensively yet.
[Note--this is the 2nd version of the code. The 2nd version improved performance substantially over the 1st version, added a couple helpful nuances, and changed the syntax a bit.]