Skip to content

php_ref_mail

PHP’s built-in mail() function provides a basic mechanism for sending emails directly from a PHP script.

It’s often used for simple notifications, contact form submissions, or password reset emails.

Important Limitations: The mail() function relies heavily on the server’s local mail transfer agent (MTA) configuration (like Sendmail or Postfix). It lacks features essential for robust email sending in modern applications, such as:

  • Easy SMTP authentication (username/password).
  • Simple creation of HTML emails with attachments.
  • Built-in handling for different character encodings.
  • Error handling and feedback (it often returns true even if the email fails to deliver later).
  • Support for modern email standards and security practices (DKIM, SPF alignment).

Recommendation: For anything beyond the most basic email needs, it is strongly recommended to use a dedicated PHP email library like PHPMailer or Symfony Mailer. These libraries provide a much more reliable, flexible, and feature-rich way to handle email sending, including SMTP support, HTML emails, attachments, and better error reporting.

For the mail() function to work, the server running PHP must have a correctly configured mail transfer agent (MTA) installed and operational (e.g., Sendmail, Postfix, Exim). PHP needs to know how to interface with this MTA.

The specific program and settings used are typically defined in the php.ini configuration file.

The mail() function is part of the PHP core. No separate PHP extension installation is needed. However, the underlying server-level mail system must be installed and configured correctly.

The behavior of the mail() function is influenced by settings in php.ini. Key directives include:

Directive Default Description Changeable
------------------ ------------- ---------------------------------------------------------------------- --------------
; For Unix-like systems using sendmail/postfix etc.
sendmail_path (varies) Path to the sendmail program (e.g., "/usr/sbin/sendmail -t -i"). PHP_INI_SYSTEM
e.g., "/usr/sbin/sendmail -t -i"
; For Windows systems (or Unix using SMTP relay)
SMTP localhost Hostname or IP address of the SMTP server to use. PHP_INI_ALL
smtp_port 25 Port number for the SMTP server. PHP_INI_ALL
sendmail_from (null) Sets the "From:" address used in mail sent from Windows. PHP_INI_ALL
; General mail settings
mail.add_x_header On Add 'X-PHP-Originating-Script' header showing script filename. PHP_INI_PERDIR
mail.log (null) Path to a log file to record all mail() calls (useful for debugging). PHP_INI_SYSTEM or PHP_INI_PERDIR
mail.force_extra_parameters (null) Force addition of specified parameters to sendmail command. PHP_INI_SYSTEM

Note: PHP_INI_SYSTEM means the setting can only be changed in php.ini or httpd.conf. PHP_INI_PERDIR allows changes in php.ini, .htaccess, httpd.conf or .user.ini. PHP_INI_ALL allows changes anywhere, including via ini_set().

Correctly configuring these settings, especially sendmail_path (Unix) or SMTP/smtp_port (Windows/SMTP Relay), is crucial for mail() to function.

FunctionDescription
mail()Sends an email. Takes parameters like recipient, subject, message, headers, and additional parameters.
ezmlm_hash()Calculates the hash value required by EZMLM mailing list manager (rarely used).