Table of Contents
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:
PHPmail(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<?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<?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 successful
}
?>
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<?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.
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!
- Getting Started with TypeScript
- How to Install Node on Windows, macOS and Linux
- Managing PHP Dependencies with Composer
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- How to deploy a .NET app using Docker
- How to build a Discord bot using TypeScript
- How to deploy a PHP app using Docker
- How to deploy an Express app using Docker
- Getting User Location using JavaScript's Geolocation API
- Getting Started with React
- Setting Up a Local Web Server using Node.js