CodingHamster
CodingHamster

Reputation: 362

How to display or save emails instead of actual sending them on localhost?

I'm working on a php project where all emails are sent via the mail() function. That's quite a problem to prepare and test those emails because mail() function fails on the localhost and I should constantly rewrite my code to print out the email before sending, check it and asume that on server it would be sent ok.

Is there a way to somehow manage such situations? I will be very happy if there is a way to save the messages on hard disk or send them only to the one specific email address, not to the real recipients, without or with slight modification of the code. Some useful software or advices are so appreciated.

Thanks!

P.S. On the localhost I'm using WAMP package as a webserver.

Upvotes: 1

Views: 2061

Answers (4)

CodingHamster
CodingHamster

Reputation: 362

Here is the solution I found to get around this problem. 1. Create the sendmail.php file somewhere in the wamp dir, e.g. d:\wamp\apps\sendmail.php. Here is it's source:

/* Path where emails will be stored */
define('DST', 'd:/wamp/tmp/sendmail/');
/* Extract the message from the stdin */
$message = '';
while(($line = fgets(STDIN)) !== false) {
    $message .= $line;
}
/* Save message to file */
file_put_contents(DST.date('Y-m-d_H-i-s_').md5($message).'.eml', $message);

2. Uncomment and edit the sendmail_path parameter in the php.ini to this:

sendmail_path = "D:\wamp\bin\php\php5.3.5\php.exe D:\wamp\apps\sendmail.php"

All messages that are sent via the mail() function will be captured and stored in the specified directory.

Upvotes: 7

Laurence
Laurence

Reputation: 60058

http://www.toolheap.com/test-mail-server-tool/

Test mail server tool for Windows is awesome! Every time you send an email on local host, it just pops that email up in your favorite email reader (I.e. outlook, postbox etc)

I use it exclusively to test all my web apps on WAMP!

No server changes needed - just download and install - then send an email and see it in action.

Oh - and it's free!

Upvotes: 9

xdazz
xdazz

Reputation: 160883

You could wrap the mail() function, like:

function my_send_mail(/*...*/) {
   if (is_localhost()) {
      // just save the mail to text.
   } else {
      // call mail() and send mail
   }
}

And use this function instead of use mail() directly.

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318568

Install fakemail. It acts as a local SMTP server and saves all mails in a folder.

Upvotes: 2

Related Questions