Sending system messages as HTML instead of text

While phpList allows you to send beautifully formatted HTML messages, its own system messages (confirmation request, unsubscription notification, and so on) are sent in plain-old-boring-text.

This hack allows you to enter HTML in your system messages and have them delivered in multipart HTML-and-text.

Code changes

The following pages detail all the code changes required for this hack. A patch file is included with this book, as well as a readme file that explains how to apply it.

The following pages detail the code changes that are required, step-by-step.

Creating the plain-text part of the message by stripping out the HTML

You'll customize your system message to include HTML code. Unlike the message composition interface, phpList doesn't give us the option to define separate HTML and text parts to our system messages, so we'll have to create the text version by stripping out all of the HTML code, but still preserving any hyperlinks.

Open admin/lib.php and look for the following line (around line #271):

$destinationemail = '';

Directly below this line, add the following:

// START HACK - Allow HTML in system messages
$html_message = $message;
$message = preg_replace('/(<script.*/script>)|(<style.*/style>)/i', '', $message);
$message = preg_replace('/<as*.*?hrefs*=s*("|')([^"']*?)("|').*?>(.*?)</a>/i', "$4:
$2
", $message);
$message = strip_tags($message);
// END HACK - Allow HTML in system messages

This code starts off with the assumption that the message that it gets from the database includes all the HTML code required. As you'll also want to generate a plain-text version of this message (for multipart MIME messages) it copies the HTML message into a new variable for the plain text message.

Then, it removes any JavaScript or CSS styles which may contain URLs. Next, it converts any HTML links into plain text links and then it strips out all remaining HTML.

Sending both the HTML and text versions of the message

Now that we have two separate messages—one HTML and one plain-text—we'll modify the code that actually sends the message ensuring that it sends both parts.

Open admin/lib.php and look for the following lines (around line #283):

if (!ereg("dev",VERSION)) {
$mail = new PHPlistMailer('systemmessage',$to);
$destinationemail = $to;
$mail->add_text($message);
} else {
# send mails to one place when running a test version
$message = "To: $to
".$message;
if ($GLOBALS["developer_email"]) {
# fake occasional failure
if (mt_rand(0,50) == 1) {
return 0;
} else {
$mail = new PHPlistMailer('systemmessage',$GLOBALS["developer_email"]);
$mail->add_text($message);
$destinationemail = $GLOBALS["developer_email"];
}
} else {
print "Error: Running CVS version, but developer_email not set";
}
}

This conditional implements slightly different message-sending behavior, depending on whether phpList's version indicates that it's a development instance. Our addition is the same in both cases, so identify both occurrences of the following line in this stanza:

$mail->add_text($message);

And below each instance, add the following:

$mail->add_html($html_message); // HACK - Allow HTML in system messages

This simply uses already-existing features of the PHPlistMailer class to add an HTML part to the message.

Stripping slashes from messages stored in the database

phpList stores the custom system messages in the database in a "quoted" format. That is, any apostrophes will be preceded with backslashes. This is a common (and sensible) security practice for protection against SQL injection attacks. However, because phpList doesn't anticipate that these custom messages will contain HTML, it doesn't strip these slashes off the content before sending them. We'll have to manually do that in the following section, so that our HTML messages aren't sent full of slashes.

Stripping slashes from a subscribe message for normal subscriptions

Open admin/subscribelib2.php and look for the following line (around line #312):

$subscribemessage = ereg_replace('[LISTS]', $lists, getUserConfig("subscribemessage:$id",$userid));

Directly below this line, add the following:

$subscribemessage = stripslashes($subscribemessage); // HACK - Allow HTML in system messages

Stripping slashes from member data change notifications

Open admin/subscribelib2.php and look for the following line (around line #595):

$message = ereg_replace('[USERDATA]', $datachange, $message);

Directly below this line, add the following:

$message = stripslashes($message); // HACK - Allow HTML in system messages

Stripping slashes from the confirmation message

Open index.php and look for the following line (around line #555):

$confirmationmessage = ereg_replace('[LISTS]', $lists, getUserConfig("confirmationmessage:$id",$userdata["id"]));

Directly below this line, add the following;

$confirmationmessage = stripslashes($confirmationmessage); // HACK - Allow HTML in system messages

Stripping slashes from the unsubscribe message

Open index.php and look for the following line (around line #656):

$unsubscribemessage = ereg_replace("[LISTS]", $lists,getUserConfig("unsubscribemessage",$userid));

Directly below this line, add the following:

$unsubscribemessage = stripslashes($unsubscribemessage); // HACK - Allow HTML in system messages

Stripping slashes from the personal location message

Open index.php and look for the following line (around line #130):

sendMail ($uid[1],getConfig("personallocation_subject"),getUserConfig("personallocation_message",$uid[2]),system_messageheaders(),$GLOBALS["envelop
e"]);

Replace this line with the following:

// START HACK - Allow HTML in system messages
$personallocmessage = getUserConfig("personallocation_message",$uid[2]);
$personallocmessage = stripslashes($personallocmessage);
sendMail ($uid[1],getConfig("personallocation_subject"),$personallocmessage,system_messageheaders(),$GLOBALS["envelope"]);
// END HACK - Allow HTML in system messages

Stripping slashes from the subscribe message for user imports

Open admin/import1.php and look for the following line (around line #246):

$subscribemessage = ereg_replace('[LISTS]', $listoflists, getUserConfig("subscribemessage",$userid));

Directly below this line, add the following:

$subscribemessage = stripslashes($subscribemessage); // HACK - Allow HTML in system messages

Open admin/import3.php and look for the following line (around line #461):

$subscribemessage = ereg_replace('[LISTS]', $listoflists, getUserConfig("subscribemessage",$userid));

Directly below this line, add the following:

$subscribemessage = stripslashes($subscribemessage); // HACK - Allow HTML in system messages

Open admin/commonlib/pages/importcsv.php and look for the following line (around line #834):

$subscribemessage = ereg_replace('[LISTS]', $listoflists, getUserConfig("subscribemessage", $userid));

Directly below this line, add the following:

$subscribemessage = stripslashes($subscribemessage); // HACK - Allow HTML in system messages

Stripping slashes from the subscribe message when resending confirmations

Open admin/reconcileusers.php and look for the following stanza (around line #36):

if ($userdata["subscribepage"]) {
$subscribemessage = ereg_replace('[LISTS]', $lists, getUserConfig("subscribemessage:".$userdata["subscribepage"],$id));
$subject = getConfig("subscribesubject:".$userdata["subscribepage"]);
} else {
$subscribemessage = ereg_replace('[LISTS]', $lists, getUserConfig("subscribemessage",$id));
$subject = getConfig("subscribesubject");
}

Directly below this stanza, add the following:

$subscribemessage = stripslashes($subscribemessage); // HACK - Allow HTML in system messages

Web interface changes

Having made all the required code changes, we'll now customize the contents of the system messages in the web interface.

Standard system messages

The standard system messages are configured under the Configure page on the web interface.

You should now modify the following:

  • Message users receive when they subscribe
  • Message users receive when they unsubscribe
  • Message users receive after confirming their e-mail address
  • Message that is sent when users change their information
  • Part of the message that is sent to their new e-mail address when users change their information and the e-mail address has changed
  • Part of the message that is sent to their old e-mail address when users change their information and the e-mail address has changed
  • Message to send when they request their personal location

This illustration will use the confirmation message (Message users receive after confirming their e-mail address).

Customize your message to include whatever HTML you require:

Standard system messages

The user will receive this message in HTML:

Standard system messages

Subscription and confirmation messages on the pre-existing subscribe page

While you've made changes to the subscribe and confirmation messages on the Configure page, be aware that these changes only apply to any subsequently created subscribe pages.

If you already have subscribe pages set up, you'll also need to customize the subscribe and confirmation messages which were inherited by these pages on creation:

Subscription and confirmation messages on the pre-existing subscribe page

Tips

The following are a few tips to help you avoid common stumbling blocks in sending system messages by HTML.

Be sure to customize all system messages

Once this hack is enabled, all system messages will be sent as HTML. If a default phpList message is sent as HTML, it will lose all its line breaks and hence look worse than the original. At the very least, you should convert word-for-word, each plain text message into HTML.

Composing HTML messages

A useful way to convert a plain text system message to HTML is to copy the text contents and paste it into a new message. Then customize the message using the WYSIWYG editor and click the Source button to view the generated HTML code:

Composing HTML messagessystem messages, sending as HTMLtips

How to deal with placeholders producing URLs

You'll note that some messages include placeholders like [SUBSCRIBEURL], which will be converted into a user-specific URL at the time of sending. Because you want these links to still be clickable when sent via HTML, be sure to check that the<A HREF> link contains the correct source.

For example, if the text version of a message looks like this:

If this is correct, please click the following link to confirm your subscription.
Without this confirmation, you will not receive any newsletters.
[CONFIRMATIONURL]

In this example, the corresponding HTML version should look like this:

<p>If this is correct, please click the following link to confirm your subscription.</p>
<p>Without this confirmation, you will not receive any newsletters.</p>
<p><A HREF="[CONFIRMATIONURL]">Click here</A></p>

Keep CSS styling inline

This tip applies not just to system messages as HTML, but also to any HTML messages sent through phpList.

Many online e-mail providers (for example, Gmail) will attempt to strip out any potentially harmful or incompatible style information from your message before displaying it. If you (as you would on a regular website) apply all your styling before the<body> tag, it's likely it'll all be stripped out.

A more reliable, but more time-consuming, approach is to apply all your styling inline.

Say you want a particular paragraph to have a gray background with a 1 px solid border on all sides. Instead of declaring a custom style greyborderbox in the<head> section of the HTML, and then applying that to your paragraph, you should present your paragraph like this:

<p style="border:1px solid; background: grey;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pretium, dolor in eleifend tempor, lacus erat ultricies sapien, in ultrices lorem ligula quis eros. Etiam at velit convallis nulla tristique elementum quis at lacus. Pellentesque ultricies facilisis justo eu egestas.</p>

This will ensure maximum compatibility with web mail providers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.37.10