PHP Advent Calendar Day 1

Original post provide by Chris Shiflett

Welcome to the PHP Advent Calendar. If you are unfamiliar with the format of an Advent calendar, Wikipedia has a pretty good description. The PHP Advent Calendar is similar in spirit to the Perl Advent Calendar, a tradition the Perl community has sustained for several years.

Each day, starting today and ending on Christmas Day, a member of the PHP community will be sharing a PHP-related tip or trick. Today’s entry is provided by Sean Coates.

Sean Coates

Name
Sean Coates
Blog
blog.phpdoc.info
Biography
Sean Coates a PHP developer who works primarily on keeping things together over at php|a by developing their software and organizing their conferences. He was formerly the Editor-in-Chief of php|architect Magazine, is the co-host of the Pro::PHP Podcast and contributes to the PHP documentation team.
Location
Montréal, Canada

If you’ve ever developed a script that sends batch email to customers, you know that dreaded feeling of “what have I done?!” that hits seconds after you’ve launched the script, and the precise moment you remember that you forgot to turn the debug flag on, and hundreds of customers have been mailed your unfinished test template. It’s an amateur mistake, but it’s an easy one to make. With a little bit of clever configuration, you can mitigate the risk of stray email going to real customers from your development/staging environment.

When it comes to mail() (as well as many other things), PHP (on non-Windows, and by default) prefers to delegate the heavy lifting to another piece of software: sendmail (or a sendmail compatible command-line mail transport agent). By default, PHP will call your sendmail binary, and pass it the entire message, after composing it from the headers and body supplied by the developer.

One of the side-benefits to this system is the ability to override PHP’s default, and seamlessly hook in your own sendmail-workalike binary or script. By setting the sendmail_path directive in php.ini, you can easily override the actual sending of email, and instead log it for easy review.

Here’s an example from one of my development environments:

$ cat /path/to/php/ini | grep sendmail_path
sendmail_path=/usr/local/bin/logmail
$ cat /usr/local/bin/logmail
cat >> /tmp/logmail.log

This little bit of config code is extremely useful in a non-production environment. In the scenario above, you don’t have to worry about flipping any flags or accidentally reading the “real” customer database when you meant to read the “fake” repository that contains only your own email address. Disaster avoided.

Left alone, that log file will get pretty big over time, quickly becoming unmanageable. With a little additional hackery, and with the help of the common formail app, an alternative might look like this:

$ cat /path/to/php/ini | grep sendmail_path
sendmail_path=/usr/local/bin/trapmail
$ cat /usr/local/bin/trapmail
formail -R cc X-original-cc 
  -R to X-original-to 
  -R bcc X-original-bcc 
  -f -A"To: devteam@example.com" 
| /usr/sbin/sendmail -t -i

This script traps all mail that would normally go out (say, to a customer), and instead, delivers it to devteam@example.com (with the original fields renamed for debugging purposes).

Sure, you could override this in your own framework (if you have a centralized mail object/function, for example), but the true beauty of this method is that it works for all calls to the mail() function, even those in third-party libraries. You do, however, still need to watch out for direct SMTP calls.

Posted Sat, 01 Dec 2007 23:07:30 GMT in Chris Shiflett’s Blog

Previous Articles:
Three Days Left For Our IPod Promo!
The php|a Team writes: Like all good things, our hugely popular iPod training promotion is rapidly coming to an end—in fact, there are only three days left!You can still get a free iPod in time for the holidays! With our promo, you get great live training from the convenience of your home or office and up to two free iPod Touch players!For more information, visit our iPod training promo page.
Php|architect Has A New Site!
For a limited time, we're offering free shipping on all our books to the U.S. and Canada, and special prices for international orders. If you're interested, our published Marco Tabini has also published a post with more information on the technical side of the redesign on his blog.
Announcing November Issue Of Php|architect
If your current CRM solution struggles to meet the needs of every department in your organization, perhaps it’s time to look at something that will provide customizable solutions geared to those needs. See how Force.com, salesforce.com’s “Platform-as-a-Service”, comes into play.Phalanger — When PHP Meets .NET by Timothy Boronczyk Microsoft’s .NET is hyped as a language-agnostic framework, but as PHP developers we could say we’ve been left out in the cold.
PHP5 Mit IMAP Unter Mac OS X
yes checking for IMAP Kerberos support... no checking for IMAP SSL support... no checking for utf8_mime2text signature... new checking for U8T_CANONICAL... no configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen.
PayPal Groks Security?
Does intent matter? Questions aside, here's hoping this is a genuine attempt to do the right thing. Thanks, PayPal. To my fellow Americans, have a wonderful Thanksgiving holiday! To everyone else, have a nice rest of the week.
More Articles:
PHP 5.2.1 And PHP 4.4.5 Released
Further details about the PHP 5.2.1 release can be found in the release announcement for 5.2.1, the full list of changes is available in the ChangeLog for PHP 5. Details about the PHP 4.4.5 release can be found in the release announcement for 4.4.5, the full list of changes is available in the ChangeLog for PHP 4.
International PHP Conference: Call For Papers
You can find more information at http://phpconference.com/cfp. Please put your proposals to http://phpconference.com/input. There are two pre-conference days with a Management Day and Power Workshops and two main conference days. The language for talks is both English and German.
The Lockergnome Universe For October 2006
Linux Fanatics: Linux to power US Navy aircraft Political Geeks: More RIAA Foolishness Hardware Help: Design Prototype: Dali's Melting Clock Digitalized Technobabble: Emergency Signs Media Center: 1-888-DO-FRUCALL Game Invasion: Netamin Announces Ultimate Baseball Online 2006 Summer Classic Mobile Lifestyle: Motorola takes Linux into the mobile mainstream Search Engineer: What bloggers say about your company Tech News Watch: Quicker, Cleaner Computers Are In Sight Word: Addressing envelopes in Word 2003 Windows XP: Folder/file attributes will not change Antivirus Discussions: Windows Update Features Disabled, Why and by Who?
Paged Result Sets With PHP And Oracle
Learn how to implement paged result sets, and display large ones efficiently.
Calling Oracle Stored Procedures With PHP
A stored procedure workout with Oracle and PHP

Leave a Reply