PHP Mail Configuration

Here's how to configure PHP for sending mail.

In the previous lesson, we used mail() to send mail in PHP. That lesson assumes that your PHP installation is configured for sending mail. If your system isn't configured for sending mail, all is not lost — you can change the configuration.

The php.ini File

The php.ini file is where you configure your PHP installation. This is the file you need to edit in order to configure PHP to send mail.

You need to ensure that the php.ini file contains details of the mail server that should be used whenever your application sends mail.

To check/change your PHP mail configuration:

  1. Open your php.ini file (if you don't know where this is, see below)
  2. Search for the line that reads [mail function]
  3. Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
  4. Save/close the php.ini file
  5. Restart your web server

Here's an example of what the mail settings could look like when you first open the php.ini file:

If you're using a UNIX based system, you will need to change the line that reads ;sendmail_path =. You will also need to remove the semicolon from the start of this line (semicolons indicate that the line is a comment). For example, sendmail_path = /usr/sbin/sendmail.

If you're using a Windows system, you should change the line that reads SMTP = localhost to include your mail server (or your ISP's mail server). You could leave it at localhost if you're using your own local SMTP server. If you aren't using your own local SMTP server, you will need to enter a mail server that you have access to (such as your ISP's mail server). For example, SMTP = mail.earthlink.net.

You should also set a default "From" email address by changing the line that reads ;sendmail_from = [email protected]. For example, sendmail_from = [email protected].

Don't Know Your php.ini Location or sendmail_path Path?

If you don't know where your php.ini is, or what your sendmail_path setting should be, read on...

Your php.ini may be located here: /private/etc/php.ini. And there's a chance that your sendmail path will be at /usr/sbin/sendmail). Having said this, each PHP installation is different, so you should check using phpinfo().

Fortunately, this is easy to do.

phpinfo() is used to view your PHP configuration details. You can do this by creating a .php file with the following line on it: <?php phpinfo(); ?>. When you run this in your browser, you will see a full list of PHP configuration variables. Simply search for the lines that contain php.ini and sendmail_path to see the values you need to use.