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.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()