My blog sends me an email after a comment is posted. I should be able to send an email from WordPress, too.
I checked the WordPress Plugin Repository for a solution, but did not find anything suitable. I decided to write a plugin using WordPress’s mail function: wp_mail. wp_mail uses PHPMailer to send mail.
I have written mailers in C and PHP for *nix systems. Linux, MacOs and similar systems include sendmail. Sendmail is hard to configure, easy to use.
PHP’s mail function also uses sendmail.
Sending Mail with Microsoft Windows
Sendmail is not included with Microsoft Windows.
I had to learn how to send on a Microsoft system, because 12% of the Web is powered by Microsoft servers.
Thankfully, PHPMailer can send mail on Microsoft systems. Their explanation:
The PHP mail function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn’t include a local mail server; PHPMailer’s integrated SMTP implementation allows email sending on Windows platforms without a local mail server.
Introducing Quick Mail
My plugin enables a WordPress blogger to send an email with an attachment from the WordPress Administration area.
I named this plugin “Quick Mail” instead of “Quick Email,” because I included a Spanish translation. Quick Mail is shorter in Spanish.
Quick Mail adds an option to the WordPress Tools menu because Email is a Tool and I did not want to see another choice on my crowded menu.
WordPress only requires an email address and a “nickname” to register. If Quick Mail finds an incomplete user profile, it displays an error message with a link to edit the profile.
How It Works
wp_mail sends mail from “firstname.lastname@example.org.” I changed this to send mail with the user’s name and address. It looks like a regular email, and users can receive replies.
The first rule of Web programming is never trust user input. The nice people at WordPress added the sanitize_text_field function to protect WordPress administrators from untrusted users. This function:
Checks for invalid UTF-8, Convert single < characters to entity, strip all tags, remove line breaks, tabs and extra white space, strip octets.
“strip all tags” means that HTML is removed. sanitize_text_field removes HTML with wp_strip_all_tags. Users can include a link in messages as plain text. HTML will be removed.
After a message is sent, the message, subject and recipient are saved, and reloaded into the data entry form. Users can see what they sent, in case their text was filtered. This also makes it easy to send the message to another person.
Attaching a File to a Quick Mail Message
file_uploads is set with a PHP boolean, a “truth value.” A truth value is true or false. However, PHP accepts multiple values. Quick Mail recognizes “1”, “true”, and “on” as true. I tested with PHP 5.4 and PHP 5.5. True values were converted to “1” – but I test for all three values.
If uploads are disabled, Quick Mail hides the file input. “Uploads are disabled” is displayed once, after Quick Mail is loaded.
One file can be attached to an email message. I could have allowed HTML and multiple uploads, but this is Quick Mail — not a multi-function mailer.
Saving File Uploads
PHP uses another setting to find a place to store uploads temporarily, before they are processed. upload_tmp_dir is not set in a default PHP distribution.
If upload_tmp_dir is missing, Quick Mail checks for a /tmp directory.
/tmp is a standard writable directory on most Unix-like operating systems.
If /tmp is not found, Quick Mail looks for “TEMP” or “TMP” environment variables. Microsoft Windows uses TEMP and/or TMP to store the location of a directory for temporary files.
“Missing temporary directory” is displayed, if none of these values are found.
How to Install Quick Mail
- Download the Quick Mail plugin as a .zip file from Github.
- Create a wp-content/plugins/quick-mail directory.
- Unpack the archive into the new directory.
Git users can clone the Github repository to copy the files.
Translating Quick Mail
Quick Mail includes a Portable Object Template (.pot file) for translators to add languages.
Want More Features?
It would be nice to save recipients. I would like to use HTML5 Web Storage to save the last ten recipients.
Quick Mail on the WordPress Plugin Repository
I submitted Quick Mail to the WordPress Plugin Repository. I will update this article with a link after Quick Mail is accepted, or modify the code, if needed.
I hope you enjoy using Quick Mail!
Update February 20, 2015
Quick Mail was updated today, and tested with WordPress 4.1.1.