How to Send an Email With PHP?
Sending emails programmatically is a regular requirement in web development, whether for user registration confirmations, password resets, or contact form submissions. PHP, a prominent server-side scripting language, has numerous methods for sending emails. This article will teach you how to send HTML emails with SMTP in PHP using PHPMailer and using the built-in ‘mail()’.
Using PHP's Built-In Mail() Function
The built-in `mail()` method is the easiest way to send an email using PHP. This function is simple to use, but it has several drawbacks. It does not support attachments or more complicated email formats. Additionally, it uses the default sendmail binary on the server, which means it cannot use SMTP servers for sending emails.
Basic Usage
Here's a simple example of how to send an email with the ‘mail()’ function:
<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "Hello, this is a test email.";
$headers = "From: sender@example.com";
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Failed to send email.";
}
?>
In this example:
‘$to’: This is the recipient's email address.
‘$subject’: The email's subject line.
‘$message’: The body of the email. ‘$headers’: Additional headers, including the From address.
HTML Content
To send an HTML email, you must specify the Content-Type header:
<?php
$to = "recipient@example.com";
$subject = "HTML Email";
$message = "
<html>
<head>
<title>HTML Email</title>
</head>
<body>
<p>This is an HTML email.</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: sender@example.com" . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Failed to send email.";
}
?>
Limitations
The ‘mail()’ method has various disadvantages:
- Emails sent with ‘mail()’ may be flagged as spam due to a lack of DKIM signatures.
- There is no easy attachment support.
- Limited customization and control.
- It cannot use SMTP servers for sending emails.
Using PHPMailer
The easiest method to send emails via SMTP in PHP is by using the PHPMailer module. This library enables email sending through an SMTP server with PHP. PHPMailer offers numerous configuration options that let you adjust and customize the email-sending features to suit your needs. With PHPMailer, you can send either plain text or HTML emails, and you can include single or multiple attachments.
Installation
Composer is a package manager for PHP, and can be used to import the required class files for PHP Mailer.
composer require phpmailer/phpmailer
Basic Usage
Here is a basic example of sending an email using PHPMailer:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Recipient');
// Content
$mail->isHTML(true);
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email.';
$mail->send();
echo 'Email has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Adding Attachments
To include attachments, use the ‘addAttachment’ function:
$mail->addAttachment('/path/to/file.txt');
$mail->addAttachment('/path/to/image.jpg', 'new.jpg');
Sending to Multiple Recipients
$mail->addAddress('recipient1@example.com');
$mail->addAddress('recipient2@example.com');
Why Use HTML?
With PHPMailer, you can easily include two different versions of your message: plaintext and HTML. Plaintext is suited for older email clients which do not support HTML, whereas HTML offers you the flexibility to design and structure your emails more effectively.
HTML emails enable you to:
- Use rich text formatting, including bold and italic text.
- Include photos and links.
- Make visually stunning layouts.
- Improve the user experience with improved presentation.
Conclusion
Sending emails using PHP may be accomplished using a variety of approaches, ranging from the fundamental mail() function to more advanced solutions like PHPMailer. Mail() is simple to use, but it lacks functionality and stability. PHPMailer offers a strong solution for delivering HTML emails, attachments, and SMTP for increased dependability.
PHPMailer may help you improve the user experience and functionality of your PHP applications by providing dependable email sending features. For more thorough advice and advanced setups, please refer to The CodexWorld guide on sending emails using PHPMailer.