Send email from the script is the most used functionality in the web application. The PHP mail() function is used to send emails from the PHP script. When you send an email using the mail() function in PHP, the mail is sent from your web server. Sometimes it may cause issues in sending an email and fails to deliver mail to the recipient. With SMTP you can overcome this issue, SMTP is the most recommended way to send email from the PHP script. When you send an email via SMTP, the email is sent from the mail server rather than the web hosting server.
The easiest way to send email in PHP with SMTP is to use the PHPMailer library. PHPMailer provides the ability to send an email via an SMTP server using PHP. Various configuration options of the PHPMailer library allow you to configure and customize the email sending functionality as per your needs. You can send a text or HTML email with single or multiple attachments using PHPMailer. In this tutorial, we will show you how to send HTML email with SMTP in PHP using PHPMailer.
In the example script, we will integrate PHPMailer in PHP and send SMTP mail using PHPMailer library. Use the following example code to send HTML email with attachment using PHP.
PHPMailer Library:
We will use PHPMailer to send emails via SMTP server. So, include the PHPMailer library files and initialize the PHPMailer object.
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Include PHPMailer library files
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
// Create an instance of PHPMailer class
$mail = new PHPMailer;
Note that: You don’t need to download the PHPMailer library separately. All the required PHPMailer library files are included in the source code and you can install PHPMailer without composer in PHP.
SMTP Configuration:
Specify the SMTP server host ($mail->Host
), username ($mail->Username
), password ($mail->Password
), encryption type ($mail->SMTPSecure
), and port ($mail->Port
) as per your SMTP server credentials.
// SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$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 isHTML()
to TRUE for sending email in HTML format.
// Sender info
$mail->setFrom('sender@example.com', 'SenderName');
$mail->addReplyTo('reply@example.com', 'SenderName');
// Add a recipient
$mail->addAddress('recipient@example.com');
// Add cc or bcc
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
// 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 Server 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. Mailer Error: '.$mail->ErrorInfo;
}else{
echo 'Message has been sent.';
}
Use the addAttachment() method of PHPMailer class to add an attachment to the email. You can attach multiple attachments to the email by adding addAttachment()
method multiple times.
// Add attachments
$mail->addAttachment('files/codexworld.pdf');
$mail->addAttachment('files/codexworld.docx');
$mail->addAttachment('images/codexworld.png', 'new-name.png'); //set new name
Add addAddress() method multiple times for sending email to multiple recipients.
// Add multiple recipients
$mail->addAddress('john.doe@gmail.com', 'John Doe');
$mail->addAddress('mark@example.com'); // name is optional
Send Email using PHPMailer in CodeIgniter
If you want to use Gmail SMTP to send email, you need to make some changes in Google account settings. Follow the below steps to use Gmail SMTP in PHPMailer library.
.
You are done! Now you can use Gmail SMTP to send email from the PHP script.
Specify your Gmail account credentials (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 = 'codexworld@gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Send Email from Localhost in PHP
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
This is nice guide for me,
i am looking for Contact form data sent on email from local
could you please arrange for us
Thank you
Naushad
I need help i am tried with this example but not getting correctly. I am getting the error “Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting“. please it’s quite argent.
This issue is occurred due to wrong SMTP credentials, check the credentials you mentioned in the script.
Hi, is there a limit google allows to send emails as such a day?
very nice
Thank you sir
Good article