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.
<?php
// 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.), https://api.drupal.org/api/devel/devel.mail.inc/7

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:
#!/Applications/MAMP/bin/php/php5.3.20/bin/php
<?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) {
  dpm($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: http://subharanjan.com/how-to-stop-outgoing-emails-being-sent-through-postfix-from-your-local-machine-in-mac-os-x/

Tags

Internal References

External References

Article Type

General