Send emails from the script is the best feature and also the most common functionality in the web application. We use the PHP mail() function to send Emails from the PHP script. When you are sending an email using mail() function in PHP, it will send the mail from your web server. Sometimes there is trouble while sending an email and fails to send the mail to the recipient. Therefore, with the help of SMTP, you can solve this issue. SMTP is the best way to send Emails from the PHP script. When you send an email with SMTP, Email is sent from the mail server rather than the web hosting server.
The simplest way to send email in PHP with SMTP is to use the PHPMailer library. PHPMailer provides the ability to send an email via SMTP server using PHP. Various configuration options of the PHPMailer library permits you to configure and modify the Email sending functionality according to your requirements. You can share a text or HTML email with single or multiple attachments using PHPMailer. In this tutorial, we will explain to you how to send HTML email with SMTP in PHP using PHPMailer.
In the sample script, we will unify PHPMailer in PHP and send SMTP mail using the PHPMailer library. Therefore, use the following example code to send HTML email with attachment using PHP.
Send HTML Email via SMTP Server
PHPMailer Library:
We will the PHPMailer to send Emails via the SMTP server. So, attach the PHPMailer library files and initialize the PHPMailer object.
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // Include PHPMailer library files require 'PHPMailer/Exception.php'; require 'PHPMailer/PHPMailer.php'; require 'PHPMailer/SMTP.php'; $mail = new PHPMailer;
Note: There is no need to download the PHPMailer library separately. Initially, include all the necessary PHPMailer library files in the source code. So, you can install PHPMailer without the composer in PHP.
SMTP Configuration:
Specify the SMTP server host ($mail->Host), username ($mail->Username), password ($mail->Password), and port ($mail->Port) as per your SMTP server credentials.
// SMTP configuration $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = '******'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;
Configure Email:
Specify some basic email settings (like sender email & name, recipient email, subject, message, etc.). Set is HTML() to TRUE for sending email as HTML format.
<?php $mail->setFrom('[email protected]', 'CodexWorld'); $mail->addReplyTo('[email protected]', 'CodexWorld'); // Add a recipient $mail->addAddress('[email protected]'); // Add cc or bcc $mail->addCC('[email protected]'); $mail->addBCC('[email protected]'); // Email subject $mail->Subject = 'Send Email via SMTP using PHPMailer'; // Set email format to HTML $mail->isHTML(true); // Email body content $mailContent = ' <h2>Send HTML Email using SMTP in PHP</h2> <p>It is a test email by CodexWorld, sent via SMTP server with PHPMailer using PHP.</p> <p>Read the tutorial and download this script from <a href="https://www.codexworld.com/">CodexWorld</a>.</p>'; $mail->Body = $mailContent; // Send email if(!$mail->send()){ echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; }else{ echo 'Message has been sent'; }
SEE ALSO: One Time Temporary Download Link with Expiration in PHP
Send HTML Email with Attachments
You can use the addAttachment() method of PHPMailer class to attach files to your Email. You can connect multiple attachments to the Email by adding addAttachment() method multiple times.
// Add attachments $mail->addAttachment('files/allsweb.pdf'); $mail->addAttachment('files/allsweb.docx'); $mail->addAttachment('images/allsweb.png', 'new-name.png'); //set new name
Send Email to Multiple Recipients
Add addAddress() method multiple times for sending Emails to the various recipients.
// Add multiple recipients $mail->addAddress('[email protected]', 'John Doe'); $mail->addAddress('[email protected]'); // name is optional
Send Email using Gmail SMTP
You can use Gmail SMTP to send Emails. So, to use it, you need to make some changes in Google account settings. Therefore follow the steps below to use Gmail SMTP in PHPMailer library.
- Go to the My Account page in your google account.
- Click the Signing into Google link from the Sign-in & security section.
- Go to the Password & sign-in method section and turn Off the 2-Step Verification
- Scroll down the Connected apps & sites section and turn On Allow less secure apps.
Congratulations! You did it! Now you can use Gmail SMTP to send Emails from the PHP script.
Specify your Gmail account identifications (email address and password), SMTP host and port to send Email using Gmail SMTP.
// SMTP configuration $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = '********'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;
Also, read our previous blog- How to Backup MySQL Database using PHP