Mailing is a very important functionality in any web application, I’ll share how to send mail using PHP mail() function.PHP mail()
function is used to send mail from the web server. You can send mail as plain text or with HTML element.
You can add HTML content and CSS styles with the message body of the mail. I am sharing a separate article for sending HTML content emails using PHP.
What’s mail() function in php
PHP mail()
function has features to send mail to multiple recipients using php email headers. You can add in mail header like From
, Cc
, and Bcc
using php mail headers.
You can also check other tutorial of php mail,
- How To Send Email From Localhost Using PHP
- How to Send Mail in PHP
- How to Send Nice HTML Email with PHP
The syntax:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Where mail()
function Parameters are:
- to: This is used for the Receiver’s email id or receiver’s email ids of the mail.
- subject: Subject of the email to be sent.
- message: Message to be sent.
- additional_headers: This will use to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).
Return Type: This method returns TRUE if mail was sent successfully and FALSE on Failure.
Runtime Configuration of PHP mail
The behavior of the php mail functions is affected by settings in php.ini
, You can set the below configuration into the php.ini
file.
Name | Default | Description | Changeable |
---|---|---|---|
mail.add_x_header | “0” | Add X-PHP-Originating-Script that will include UID of the script followed by the filename. For PHP 5.3.0 and above | PHP_INI_PERDIR |
mail.log | NULL | The path to a log file that will log all mail() calls. Log include full path of script, line number, To address and headers. For PHP 5.3.0 and above | PHP_INI_PERDIR |
SMTP | “localhost” | Windows only: The DNS name or IP address of the SMTP server | PHP_INI_ALL |
smtp_port | “25” | Windows only: The SMTP port number. For PHP 4.3.0 and above | PHP_INI_ALL |
sendmail_from | NULL | Windows only: Specifies the “from” address to be used when sending mail from mail() | PHP_INI_ALL |
sendmail_path | “/usr/sbin/sendmail -t -i” | Specifies where the sendmail program can be found. This directive works also under Windows. If set, SMTP, smtp_port and sendmail_from are ignored | PHP_INI_SYSTEM |
Example 1: Sending mail Using PHP
<?php $to = "[email protected]"; $sub = "Sample Mail"; $msg="Hello Geek! This is a sample email."; if (mail($to,$sub,$msg)) echo "Your Mail is sent successfully."; else echo "Your Mail is not sent. Try Again."; ?>
Output:
Your Mail is sent successfully.
Example 2: Sending Mail Using PHP with Extra Parameters:
<?php $to = "[email protected]"; $subject = "parxx"; $body = "Body of your message here you can use HTML too. e.g. <b> Bold "; $headers = "From: Peterrn"; $headers .= "Reply-To: [email protected]"; $headers .= "Return-Path: [email protected]"; $headers .= "X-Mailer: PHP5n"; $headers .= ‘MIME-Version: 1.0′ . "n"; $headers .= ‘Content-type: text/html; charset=iso-8859-1′ . "rn"; if(mail($to,$subject,$body,$headers)) echo "Your Mail is sent successfully."; else echo "Your Mail is not sent. Try Again."; ?>
Output:
Your Mail is sent successfully.