Linux · March 18, 2016 0

How to test php mail function on localhost

On localhost if you don’t have a mail server you can’t send mails using php mail function but fortunately you can test it either mail function receiving correct parameters and its output.

Step 1
Create a logs directory somewhere you want to create, for example
I am going to create a mail/logs directory in my projects directory

$ mkdir -p /projects/logs

Step 2
Edit your php.ini file and customize sendmail_path.
Search for sendmail_path and comment out if already exists and add following line

sendmail_path = /usr/bin/tee /projects/logs/mail.out

We are sending output of “mail” function to “tee” command which will write the output of mail function in mail.out file in the directory /projects/logs we created in first step.

Step 3
now call mail function

mail("test@example.com", "testing php mail", "Body of test php mail() function")

Check mail function output
Now check the file in /projects/logs/mail.out it will have all the mail information right there.

Thanks