Sending Mail in PHP

Sending Mail in PHP
bluehost placeholder image

Sending mail is a very common thing to do for a dynamic application or website. You want to inform your users of critical events like a successful sign up, major updates or changes, a successful purchase, and any changes in status of their accounts. Sending mail in PHP is done with the mail() function, which both builds and sends the email from your server.

Here is the basic syntax for the function:

	
    mail(destination, subject, message, headers, parameters);
	

Here is the explanations for each parameter:

  • destination: The recipient's email address to send the email to.
  • subject: The subject line of the email.
  • message: The content of the email.
  • headers: Additional headers like "From", "Cc", and "Bcc".
  • parameters: Any additional parameters.

Sending Plain Text Emails

The simplest email to send is a plain text email. That is, the body of the email is just normal text. Here's an example of sending a plain text email:

	
    <?php
        $destination = "[email protected]";
        $subject = "Hello there, from Sabe";
        $message = "Hello, thanks for registering on our forum!";

        $from = "[email protected]";
        $headers = "From: $from";

        mail($destination, $subject, $message, $headers);
    ?>
	

That's all you need to do to send a plain text email. An important thing to note is that the mail() function will return a boolean indicating if sending the email was successful or not. That means you can run different code if it fails, like this:

	
    <?php
        $destination = "[email protected]";
        $subject = "Hello there, from Sabe";
        $message = "Hello, thanks for registering on our forum!";

        $from = "[email protected]";
        $headers = "From: $from";

        $success = mail($destination, $subject, $message, $headers);

        if ($success) {
            // sending was successful
        } else {
            // sending was not succcessful
        }
    ?>
	

Sending HTML Emails

Sending HTML emails requires a bit more work to ensure it renders properly. To accomplish this, we simply need to pass additional headers with our emails so that the user's email client knows to render the email as an HTML page.

	
    <?php
        $destination = "[email protected]";
        $subject = "Hello there, from Sabe";

        $from = "[email protected]";

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: ' . $from . "\r\n";

        $message = '<html>';
        $message .= '<body>';
        $message .= '<h1 style="color:#333;">Hello from Sabe.io!</h1>';
        $message .= '<p style="color:#088;">Thanks for registering on our forum!</p>';
        $message .= '</body>';
        $message .= '</html>';

        mail($destination, $subject, $message, $headers);
    ?>
	

Your HTML email would then look something like this:

An example of an HTML email.
An example of an HTML email.

Emails are a powerful way to connect with your users and keep them up-to-date with the most important information. Now you know how to send both plain text and HTML emails dynamically with your server using PHP!