It’s Festive Season! Enjoy 30% Off

Use code FESTIVE30 at checkout

Code has been added to clipboard!

PHP Mail Function: Communicating With Your Users via Emails

Reading time 3 min
Published Aug 8, 2017
Updated Oct 3, 2019

The easiest way to interact with your users is to make PHP send emails, using special forms and the PHP mail function. The main idea of it is that a developer can create special PHP email forms for sending emails straight from the script.

Using PHP mail() you can, for example, send weekly newsletters, or configure your email box to receive error messages. Therefore, the function provides with not only means of communication but also a custom error log.

PHP mail Function: Main Tips

  • PHP mail function allows you to send emails directly by using PHP script.
  • This function is integrated into PHP's core, but requires a working email system installed to your system (which must be defined in the php.ini file)

Options for Runtime Configuration

The way PHP mail() function behaves is always affected by the settings defined inside php.ini. The table below provides a list of these runtime configurations that you can apply:

Name Default value Description Changeable
mail.add_x_header "0" This function is used for adding X-PHP-Originating-Script, purpose of which is to include UID of script followed by filename. Usable in PHP 5.3.0 and newer PHP_INI_PERDIR
mail.log NULL Path to file that will include all previous calls to the PHP mail function. This log will include full path to directory of script file, line number, recipient address and required headers. Usable in PHP 5.3.0 and newer PHP_INI_PERDIR
SMTP "localhost" For Windows only: The DNS name or IP address of the SMTP server PHP_INI_ALL
smtp_port "25" For Windows only: The SMTP port number. Usable in PHP 4.3.0 and newer PHP_INI_ALL
sendmail_from NULL For Windows only: Specify From address that will be used for sending mail using PHP mail() function PHP_INI_ALL
sendmail_path "/usr/sbin/sendmail -t -i" Specify location of program sendmail. This directive also works in Windows. Ignores SMTP, smtp_port and sendmail_from if set. PHP_INI_SYSTEM

Functions for Email Sending

In the table below, you can see the functions you can use when you want to PHP send email:

Function Description
ezmlm_hash() Calculate hash value required by EZMLM
mail() Allow sending emails directly from script

PHP mail Function: Summary

  • Using an inbuilt mail() function, you can send emails via your PHP script.
  • For this function to work properly, your system must have an email system installed, working, and defined in php.ini.