Prevent local machine from sending emails

To prevent emails from accidentally getting sent from a local development environment, I use a two part scheme. First, for Drupal, I turn on the devel module and then add the following to my settings.php:

Saves to temporary://devel-mails dir by default. Can be changed using 'devel_debug_mail_directory' variable. Filename pattern controlled by 'devel_debug_mail_file_format' variable.
// Don't send email, instead, put email in to /tmp/devel_mails
$conf['mail_system'] = array(
  'default-system' => 'DevelMailLog',

Here's details on using Devel's email log (MailSystemInterface for logging mails to the filesystem.),

Then, in my php.ini, I add this line (and comment out any other sendmail_path lines):

sendmail_path = /Users/jmahoney/smtp_out/smtp_catcher.php
What smtp_catcher.php does is saves all emails, in a format your email client can read, to a folder. This makes sure that no matter what method is used, email cannot get sent from my local machine from any php project. Here's the contents of smtp_catcher.php:
# create a filename for the emlx file
list($ms, $time) = explode(' ', microtime());
$filename = dirname(__FILE__).'/'.date('Y-m-d h.i.s,', $time).substr($ms,2,3).'.eml';
# write the email contents to the file
$email_contents = fopen('php://stdin', 'r');
$fstat = fstat($email_contents);
file_put_contents($filename, $fstat['size']."\n");
file_put_contents($filename, $email_contents, FILE_APPEND);
# Uncomment out the next line to open up the emlx file in the default email program. You may have to associate .emlx files with your email program to get this to work.
#exec('open '.escapeshellarg($filename));

Debug messages with hook_mail_alter()

 * Implements hook_mail_alter().
function mymodule_mail_alter(&$message) {
The reroute email module may also be handy if you want to route all email to a particular address.

Weird MacOS problem?

A coworker found another article today (Feb 9 2017). It's a Mac only fix and I haven't tried it yet:


Internal References

External References

Article Type