Home arrow PHP arrow Page 4 - Managing Email

Preparing PHP - PHP

If you want to write PHP scripts that handle email, there are a few things you need to understand first. This article, the first of two parts, introduces you to these concepts. It is excerpted from chapter 8 of the Zend PHP Certification Study Guide, written by Zend Technologies (Sams; ISBN: 0672327090).

  1. Managing Email
  2. MX Records
  3. Other Emerging Technologies
  4. Preparing PHP
  5. Sending Email
By: Sams Publishing
Rating: starstarstarstarstar / 6
November 09, 2006

print this article



Before you can send email from your PHP script, you must first ensure that your copy of PHP has been correctly configured.

If You Are Using PHP on UNIX

To send email from a PHP script running on UNIX, you must have a sendmail-compatible MTA installed on the same server that your PHP script runs on.

On UNIX systems, PHP sends email by running the command-line program

sendmail. sendmail is the de facto standard MTA for UNIX systems.

If you are using an alternative to sendmail, it must provide a sendmail wrapper. A sendmail wrapper is a drop-in replacement for the sendmail command-line program. It must accept the -t and -i command-line switches at the very least.

When PHP is compiled, the configure script searches for the sendmail command in


If configure cannot find the sendmail command, then sendmail support will be permanently disabled. The following PHP functions will either be missing entirely, or will always return an error:

  • mail()ówill be missing

  • ezmlm_hash()ówill be missing

  • mb_send_mail()ówill always return false

When this happens, you must install a sendmail wrapper, and then recompile PHP.

Once PHP is compiled with sendmail support enabled, whenever your script sends email, PHP will use the sendmail command discovered by the configure script. If you ever need to override this default, set the sendmail_path in php.ini to point to the sendmail command that you want PHP to use.

sendmail_path = '/usr/local/bin/sendmail'
If You Are Using PHP on Windows or Netware

Although not documented in the PHP Manual, if you set the sendmail_path setting in your php.ini file, PHP will attempt to send email via the sendmail_wrapperóbehaving exactly as if PHP were running on UNIX. This can catch you out, so remember to look for it when troubleshooting email problems.

If you do not have a sendmail wrapper available, PHP on Windows talks to the mail transport agent (MTA) directly via the SMTP network protocol. PHP needs to be configured to tell it where to find your MTA:

  • The SMTP setting in php.ini must be set to the hostname (or IP address) of the email server that your MTA is running on. The default value is localhost. You will probably want to change that.

  • The smtp_port setting in php.ini must be set to the port that your MTA is listening on. The default value is 25. You probably will not need to change that.

Note - It isn't documented in the PHP Manual, but PHP on Novell Netware uses the same code for email support as PHP on Windows.

Caution - It is currently not possible to get PHP on UNIX to talk directly to the MTA via SMTP.

PHP on Windows does not support SASL. If your MTA is configured to require authentication, you will need to change the security on your MTA to enable PHP to send emails through successfully.

On UNIX, the MTA will automatically say that the email is from whichever user your PHP script is running as.

This can't be done on Windows because PHP is connecting to the MTA over the network via SMTP. Instead, PHP will work out who the email is from by looking in these places in this order:

  • the from: header line passed to the mail() function

  • the sendmail_from setting in the php.ini file

PHP will display an error and refuse to send the email if it cannot determine who the email is from.

If you compile your own version of PHP on Windows, and you are going to use it to send emails, it's important that you build PHP with the Perl-compatible regular expression (PCRE) library included. PHP on Windows relies on the PCRE functions to make sure that headers have the correct end of line character. If you build PHP on Windows without PCRE support, you might find that MTAs refuse to accept your emails for delivery.

With PHP correctly configured, you can now send email from your PHP scripts.

>>> More PHP Articles          >>> More By Sams Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: