How to send Email to user using PHP mail() and Simple Transmission Protocol (SMTP)

As we know php used to develop Static websites or Dynamic websites or Web applications.
The mail () function in php allows you to send emails directly from a script and it is a built in PHP.

Note: PHP mail() function will not work at the localhost.
If you have localhost go through the following link.

This is the syntax of mail function:-


every parameter has its value.

to:  Require the receivers of the email id.

Subject:  describing about message
Avoid using aggressive or salesy language, and loud formatting like all caps or multiple exclamation marks. These will most likely land your email in the spam folder without the reader ever opening it.
Get more instructions about subject :

Message:  The content of your code will come here.
Each line should be separated with a CRLF (\r\n ). Lines should not be larger than 70 characters.
In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

Header:   Requires additional headers, like But optional.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).
A Carbon Copy, or "Cc'd" message is an e-mail that is copied to one or more recipients. Both the main recipient (whose address is in the "To:" field) and the Cc'd recipients can see
Blind carbon copying is a useful way to let others see an e-mail you sent without the main recipient knowing.
In simple way you hide the other recipient from main recipient..

Parameters: Additional parameter to the sendmail program but optional.

Return Values :  Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
The return value only indicates that the message was delivered successfully to whatever intermediary your system is configured with (usually sendmail on linux systems).
If you need actual data on deliverability, you could use a third party mail service such as SendGrid or MailGun, etc, which use APIs to send and also can report back delivery status, and other data.

For Example:

Send an email with extra headers:

        $to = "";
        $subject = "happy learning!!";
        $txt = "thank you for visiting us";
        $headers = "From:" . "\r\n" .


Personal views:

You can use error_get_last() when mail() returns false.

$success = mail('', 'My Subject', $message);
if (!$success) {
$errorMessage = error_get_last()['message'];

Use latest Version of PHP.

For learning in detail :-

Did you like our works?

We are known for Website Development and Website Designing, along with Android iOS application development in Mumbai, India. Please write us what you think, we would like to hear it from you