PHP Mail Configuration
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. This lesson explains how to configure PHP for sending mail.
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:
- Open your
php.inifile (if you don't know where this is, see below)
- Search for the line that reads
- Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
- Save/close the
- Restart your web server
Here's an example of what the mail settings could look like when you first open the
[mail function] ; For Win32 only. SMTP = localhost smtp_port = 25 ; For Win32 only. ;sendmail_from = firstname.lastname@example.org ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path =
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@example.com. For example,
sendmail_from = firstname.lastname@example.org.
Don't Know Your
php.ini Location or
If you don't know where your
php.ini is, or what your
sendmail_path setting should be, read on...
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
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
sendmail_path to see the values you need to use.