2022-01-29, 13:42

Feb 27, 2012 — Notify Users module, ver. 0.5 released — user can choose between sending single message (...) »

2013-01-28, 08:32

19 студзеня 2013 у Гродне трагічна загінуў паэт, журналіст і мой сябар (...) »

2009-10-14, 07:19

The site Litaratura.org is devoted to Belarusian literary works and translations. (...) »

2009-01-12, 20:46

Late November 2008 marked the 20th anniversary of the official founding of the Belarusian (...) »

Registered User Area Log into This Site
Show Credits... Credits
~ / Public Domain Software / Stuff that MODx Contributors Forget About $_
Stuff that MODx Contributors Forget About
Click To View A Full Size Image...
2008-08-24, 10:06

This text explains — taking my favorite MODx CMS system as an example — how to make sending emails suitable even for production use. The inspiration for writing the text was patching the Jot Snippet which I use in almost each MODx-driven project, including this site.

There is a big difference between running a site in the development environment and on a real hosting server. No matter what kind of environment you use — Windows XAMPP or a genuine Linux machine — the difference will be no less noticeable. Relocating the project from the environment where it was built to an environment where it should work for years abounds with painful surprises.

Undoubtedly, the electronic mail is the most popular Internet service and each contemporary site should be able to send emails. Apart from sending regular messages from one human to another, sites also use emails for internal notifications of many kinds. Inability to send emails by the site always means the loss of an important part of its functionality (imagine that for some reasons the PayPal service is not able to send emails ;-).

The Jot Snippet written by Armand Pondman manages comments for articles. There is no point to write about its numerous advantages — it simply allows to arrange the system of comments for your site within several minutes. However, one thing in the snippet’s implementation was not considered enough by the author. The snippet notifies of a new comment by sending emails to users and moderators through a call to the PHP function mail(). What’s wrong? Nothing, as long as you play with the site in the environment it was created in.

If you try to relocate the site to a real hosting server it might occur that notifications do not work. Hosting providers very often turn off the PHP mail() function for security reasons. Therefore it should not be considered as a reliable mean for arranging the communication.

The MODx itself provides for a reliable communication system, but unfortunately not every contributor takes advantage of it. This system is a PHPMailer Class located in the manager/includes/controls folder of the system tree. The PHPMailer Class is a standalone product that you may include into any project, even if you are writing your site from the scratch.

Attachments for this article include the patched Jot Snippet where calls to the PHP mail() function were rewritten in the PHPMailer style. You can download files and correct your MODx installation by simply overwriting the main Jot-class file. Another MODx standard snippet, Weblogin written by Raymond Irving, uses direct calls of the PHP mail() function as well. Its patch is also attached at the bottom of the text. (It is worth to notice that non-standard Weblogin’s enhanced counterpart, WebLoginPE written by Scotty Delicious, sends emails in the „right way”, using PHPMailer Class.)

Here are some remarks on comparison between the call of the mail() function and its equivalent code written in the PHPMailer style, so you will be able to patch any other code on your own:

Comparison between mail() and PHPMailer

# example call of PHP mail() function
mail($to, $subject, $message, 'From: ' . $from);
# ---------------------------------------------
# equivalent code in the terms of PHPMailer class
require_once('manager/includes/controls/class.phpmailer.php'); /* 1 */
$mail = new PHPMailer(); /* 2 */
# $mail->IsSMTP(); /* 3 - use SMPT */
# $mail->IsMail(); /* 4 - use PHP mail() */
$mail->IsHTML(false); /* 5 - set plain text as mail format */
$mail->From = $from; /* 6 */
$mail->FromName = $from_name; /* 7 */
$mail->Subject = $subject; /* 8 */
$mail->Body = $message; /* 9 */
$mail->AddAddress($to); /* 10 */
$mail->send(); /* 11 - send message */

Yes, sending emails through the PHPMailer Class looks like a bit sophisticated process, but it works regardless of the configuration of the hosting server. In step 1 the class definition is loaded; in step 2 the class instance (object) is created; in steps 3 and 4 (commented) the method of sending emails might be selected; in step 5 the mail format is chosen; in steps 6, 7, 8, 9, and 10 the message is composed, and finally in step 11 it is sent.

Why steps 3 and 4 are commented? Because it is a better idea to set up one and default method of sending emails for the entire site in the class definition instead of selecting it while sending each single message. When you open the class.phpmailer.php file in an editor, then you will see that the class definition begins with a series of interface variables. One of these variables, $Mailer, allows you to define the method of sending emails: mail (PHP function), sendmail (sendmail, qmail, or other compatible program), smtp (SMTP protocol with user authentication). In the last case, you also need to provide (through subsequent variables) an SMTP username and password.

Sending emails through the PHPMailer Class solves all communication problems. During the development of the site in our private environment we can set up the PHPMailer Class to use the PHP mail() function for sending emails and later we can change it with a single and precise „surgical cut” for a method supported by the target hosting server.

Complete list of changes in attached Jot class files

— notification emails are sent using PHPMailer Class (all versions),
— small improvement to preserveUrl() function (all versions),
— improved notifyAuthor behavior: author is notified regardless the fact whether he/she belongs to manager or web users (1.1.3, 1.1.4).

Download This Attachment... Patched copy of the Weblogin Snippet, version 1.0 (older: prior to MODx
Last modified: 2009-06-28, 00:00 — Size: 5,97 KB
Download This Attachment... Patched copy of the Weblogin Snippet, version 1.0 (newer: MODx, MODx 1.0.0)
Last modified: 2009-07-13, 00:00 — Size: 6,03 KB
Download This Attachment... Patched copy of the Jot Class, version 1.1.2
Last modified: 2009-06-28, 00:00 — Size: 8,38 KB
Download This Attachment... Patched copy of the Jot Class, version 1.1.3
Last modified: 2009-06-28, 00:00 — Size: 8,82 KB
Download This Attachment... Patched copy of the Jot Class, version 1.1.4
Last modified: 2009-06-28, 00:00 — Size: 8,79 KB