26

I need to test a function that uses PHP's mail()
How can I do this without uploading the script to a server and test it online?
What's even more I am developing with no Internet connection at all.
I am on Mac OSX running localhost from XAMPP.

Volomike
  • 23,743
  • 21
  • 113
  • 209
FFish
  • 10,964
  • 34
  • 95
  • 136

7 Answers7

54

You don't have to install an MTA on your computer to test PHP's mail() function. On Unix based systems (Linux, *BSD, OS X, etc.) you can set sendmail_path to something like tee mail.out > /dev/null. This will put the emails (including the headers) in a file called mail.out.

Here is an example of how it would work:

daniel@daniel-laptop:~$ cat | php -d sendmail_path='tee mail.out > /dev/null'
<?php
mail('test@example.com', 'the subject', 'the body');
?>
daniel@daniel-laptop:~$ cat mail.out
To: test@example.com
Subject: the subject
X-PHP-Originating-Script: 1000:-


the body

You can set sendmail_path in your php.ini file. If you want to append emails to the file instead of overwriting each time, you can use tee -a instead of just tee.

Daniel Egeberg
  • 8,359
  • 31
  • 44
11

To test sending email from apache do the following

create a folder to store the email.

/home/username/Documents/TestEmails

Give permission to apache. From the Documents folder, run

sudo chgrp -R www-data TestEmails

Modify the php.ini file, mine is located at

/etc/php5/apache2/php.ini

set sendmail_path

sendmail_path ='cat > /home/username/Documents/TestEmails/mail.txt'

Restart apace2

sudo service apache2 restart
Xavier John
  • 8,474
  • 3
  • 37
  • 51
  • What if I'm using the MAMP server software for developing on localhost? – Nathan Jan 04 '14 at 09:26
  • Best answer which worked for me is the one below (http://stackoverflow.com/a/3176517/1130803), as it uses `tee` instead of `cat` which gives an `Invalid argument` error in OSX. If using MAMP, I guess the same advice applies, only changing the path to where your php.ini is. – Meetai.com Apr 26 '15 at 02:33
  • This is working for me, but just from CLI scripts; but not from a web service. Is there something else I need to do aside of restarting apache? – orlybg Aug 31 '15 at 17:20
  • If you use `cat >> /path/mail.txt` instead of `cat > /path/mail.txt`, it will append the mail rather than overwriting it. This can be useful if, say, you are debugging where you send a mail to a call center as well as the customer, for example. – Volomike Jan 27 '18 at 22:17
5

A nice and simple solution for testing:

http://blogs.bigfish.tv/adam/2009/12/03/setup-a-testing-mail-server-using-php-on-mac-os-x/ Updated link: https://github.com/ifunk/smtp-catcher

biziclop
  • 14,466
  • 3
  • 49
  • 65
  • There is a small Windows section at the end of the page. On other Unix systems I think you could solve it similarly to the Mac case. – biziclop Sep 06 '12 at 06:13
1

Hmm. I haven't tried this, but in php.ini you can set "sendmail_path" ... so in theory you could write your own shell script that simply wrote the input into text files, and change your php.ini to use that? Then simply run tests and check text files!

James
  • 3,265
  • 4
  • 22
  • 28
1

If you're on Windows/using something like WAMP/UWAMP/XAMPP and need to test mail then Papercut is well worth a look:

https://github.com/ChangemakerStudios/Papercut

You can leave your SMTP settings in php.ini as the defaults (localhost/25) and it just works. It looks like an email client and shows all the parts of/details of a message in separate sections.

trapper_hag
  • 780
  • 9
  • 14
0

Setup a pop3 server in local Machine. Many available for free. and send mails within your local domain using sendmail.

By default its not required to set the sendmail path in Linux. at least I never needed it. just use the mail() function and hit mails on local domain

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • That would be a SMTP server buddy pop3 is to actually receive emails not send mails out. – Prix Jul 04 '10 at 17:23
  • LOL Ya But where would the sendmail send the mails ??????????? he have no internet connection. Okay ?????? Thats the reasonhe needs a POP3 Server to run a local test. >> we are not talking about SMTP server. its sendmail – Neel Basu Jul 04 '10 at 17:25
  • having access to the machine as he does with only the mail server he can verify it without having to go thru a pop3 connection. – Prix Jul 04 '10 at 17:27
0

Building on the answer @Daniel-Egeberg provided, this is what worked for me on Ubuntu 18.04:

I opened /etc/php/7.2/apache2/php.ini and set:

sendmail_path='tee /path/to/file/mail.out'

restarted:

sudo service apache2 restart

then created /path/to/file/mail.out and changed permissions for it:

chmod 666 /path/to/file/mail.out

chimeraha
  • 421
  • 5
  • 7