Code has been added to clipboard!

PHP mail()

Reading time 2 min
Published Aug 14, 2017
Updated Sep 27, 2019

PHP mail(): Main Tips

  • Using PHP mail() lets you send emails using a script and communicate with your website visitors.
  • It's a great way to use data forms.

Correct Syntax: Parameters Explained

To avoid cases of PHP mail not working, always make sure you use the correct syntax in your PHP mail scripts:

mail(to, subject, message, headers, parameter);

Seems rather simple, doesn't it? See how PHP mail() works in a short PHP mail script example below:

Example
<?php
// The text
$text = "The first message";

// use wordwrap() if the lines are more than 70 characters
$text = wordwrap($text, 70);

//send the email
mail("test@test.com", "My subject", $text);
?>

Now, let's break it down and talk about each parameter that is used in the formula above:

Parameter Description
to This parameter is needed to specify the recipient of the email.
subject This parameter is needed to name the subject and can't have any newline characters.
message This parameter is used to write the message inside the email. A single line must not use more than 70 characters and ought to be split using \n to avoid PHP mail not working.
headers This parameter is optional and used to add new PHP mail headers (CC, BCC, From, etc.). All PHP mail headers must be separated using \r\n to avoid PHP mail not working.
parameters This parameter is optional and allows you to specify more additional parameters.

Note: If you want to make PHP send mail, you must use the header From as it is required. It can be set with this parameter or by editing the php.ini file.

Usage: Code Example

Let's see one more PHP mail example to make sure we got it right. In this code, you can notice that more mail headers are added:

Example
<?php
$to = "test@test.com";
$subject = "My subject";
$txt = "Test";
$headers = "From: admin@test.com" . "\r\n" .
"CC: notadmin@test.com";

mail($to, $subject, $txt, $headers);
?>
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()