4

I'm in a LAMP environment working on a system that sends out a lot of email notifications. I'd like a way to keep PHP from sending out actual emails in my development environment. Right now, I'm commenting out all of the mail() lines, but this is starting to cause confusion downstream with the QA people because they are removing the commented out lines and pushing them to the testers.

Any simple way disable sendmail in PHP without throwing an error?

random21
  • 579
  • 1
  • 5
  • 15

5 Answers5

3

You could also set up a small php script like described here to write the mails to var/log/mails:

#!/usr/bin/php
<?php
$input = file_get_contents('php://stdin');
preg_match('|^To: (.*)|', $input, $matches);
$filename = tempnam('/var/log/mails', $matches[1] . '.');
file_put_contents($filename, $input);

Put the script in /usr/local/bin/sendmail, make it executable and put the line

sendmail_path = /usr/local/bin/sendmail

in your php.ini

chiborg
  • 26,978
  • 14
  • 97
  • 115
2

another solution for your problem is to setup your local postfix to local delivery. every email will be send to your local email account!

https://serverfault.com/questions/137591/postifx-disable-local-delivery

Community
  • 1
  • 1
silly
  • 7,789
  • 2
  • 24
  • 37
2

Look at Fakemail or smtp4dev (last one only for windows) These tools allow you to not modify your code and test mails sending.

a.tereschenkov
  • 807
  • 4
  • 10
1

Why dont you just use a Mailerclass like:

 class MyMailer {
    private static $is_development_state = true;
    public static function mail(...) {
        if (self::$is_development_state) {
          ...
        }
    }
 }

I mean: Refactoring mail to MyMailer::mail can do every IDE for you ;)

androidavid
  • 1,258
  • 1
  • 11
  • 20
1

Also you could try to use the override_function method in PHP:

http://php.net/manual/en/function.override-function.php

It allows you to degrade the mail() function to a dummy function. That way nothing happens, but the code would still work in production if you comment the override_function for your production environment.

Sebastian Borggrewe
  • 1,260
  • 4
  • 13
  • 25