PHP sending email using mail() function

PHP built-in mail function is a convenient way to send emails. In this post, we’ll learn to send emails and also configure headers for email.

Syntax

<?php
    mail(string $to_email_id, string $email_subject, string $email_message, array or string $email_headers, array or string $email_additional_parameters);
?>

The mail function returns a boolean value. If mail is sent than returns TRUE else FALSE.

Parameters Description

  • $to_email_id : (mandatory) specifies receiver email id.
  • $email_subject : (mandatory) This will be displayed as the email subject.
  • $email_message : (mandatory) This is the main content of the email.
  • $email_headers : (optional) Accepts data type of array or string. You extra information related to CC, BCC or phpversion.
    • If you are using headers as a string then each header value must be separated by \r\n.
    • If you are passing headers as array than the array key will be considered as header name and array keys value will be considered as a header value.
  • $email_additional_parameters : (optional) This parameter will be used for configuring additional flags.

The mail function returns a boolean value. If TRUE then email is successfully sent.

Using mail() function to send email as plain text

<?php

$to_email_id = "to_email@example.com";
$email_subject = "This is Email Subject";
$email_message = "This is Email Message";
$email_headers = [
    'From' => 'from_email@gmail.com',
    'Reply-To' => 'from_email@gmail.com',
    'X-Mailer' => 'PHP/' . phpversion()
];

$response = mail($to_email_id, $email_subject, $email_message, $email_headers);

var_dump($response); // output : bool(true)

PHP using mail function to send email as plain text

You’ll also see the spam alert message by Gmail like this below.
PHP send email | Gmail spam alert message

To not let google identify your emails as spam. You must specify all required header and also configure STMP settings for sending an email.

Send email as HTML

<?php

    $to_email_id = "to_email@gmail.com";
    $email_subject = "This is Email Subject";
    
    $email_message = '
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </head>
        <body>
            <h2>This is Test Email sent as HTML document.</h2>
        </body>
        </html>
    ';
    
    $email_headers = [
        'From' => 'from_email@gmail.com',
        'Reply-To' => 'from_email@gmail.com',
        'X-Mailer' => 'PHP/' . phpversion(),
        'MIME-Version' => '1.0',
        'Content-type' => 'text/html',
        'charset' => 'iso-8859-1',
    ];
    
    $response = mail($to_email_id, $email_subject, $email_message, $email_headers);
    
    var_dump($response); // output : bool(true)

?>

PHP send email as HTML format

Note

The Reply-To header is used to defined which email the conversion must be sent when end-user reply’s.

The X-Mailer header specifies the email program which triggers an email. Also, this is an optional header key.

The phpversion() method will display PHP version number like 7.2.27.

Error Handling of emails

<?php
    $to_email_id = "to_email@gmail.com";
    $email_subject = "This is Email Subject";

    $email_message = 'Email Message Content.';

    $email_headers = [
        'From' => 'from_email@gmail.com',
        'Reply-To' => 'from_email@gmail.com',
        'X-Mailer' => 'PHP/' . phpversion(),
        'MIME-Version' => '1.0',
        'Content-type' => 'text/html',
        'charset' => 'iso-8859-1',
    ];

    $response = mail($to_email_id, [$email_subject], $email_message, $email_headers);

    if(!$response){
        echo "Error Type : ".error_get_last()["type"]."<br>";
        echo "Error Type : ".error_get_last()["message"]."<br>";
        echo "Error File : ".error_get_last()["file"]."<br>";
        echo "Error Line : ".error_get_last()["line"]."<br>";
    }else{
        echo "Email successfully sent";
    }

Caution

The error_get_last() function will only display error message related to invalid parameter passing. Hence it is not a very effective way to check errors triggered by email.

Related Posts