AllsWeb Blog
No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
Support
Knowledgebase
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison
No Result
View All Result
AllsWeb White Logo
No Result
View All Result
Home PHP and MySQL

Send Email via SMTP Server in PHP using PHPMailer

SMTP Server

Lakshika Mathur by Lakshika Mathur
December 31, 2019
Reading Time: 4 mins read
0
Send Email via SMTP Server in PHP using PHPMailer

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.

RELATED POSTS

What is Application Programming Interface (APIs)?

Like Dislike Rating System with jQuery, Ajax, and PHP

Star Rating System with jQuery, Ajax, PHP, and MySQL

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.

  1. Go to the My Account page in your google account.
  2. Click the Signing into Google link from the Sign-in & security section.
  3. Go to the Password & sign-in method section and turn Off the 2-Step Verification
  4. 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

 

Tags: PHPSMTP Server
ShareTweetSendShareSharePinScan
Lakshika Mathur

Lakshika Mathur

Related Posts

What is Application Programming Interface (APIs), Types, and Importance.
PHP and MySQL

What is Application Programming Interface (APIs)?

January 29, 2022
61
Like Dislike Rating System with jQuery, Ajax, and PHP
jQuery & AJAX

Like Dislike Rating System with jQuery, Ajax, and PHP

January 6, 2020
739
Star Rating System with jQuery, Ajax, PHP, and MySQL
jQuery & AJAX

Star Rating System with jQuery, Ajax, PHP, and MySQL

January 6, 2020
162
How to Force Download File in PHP
PHP and MySQL

How to Force Download File in PHP

January 2, 2020
82
How to Connect to the Remote MySQL Database using PHP
PHP and MySQL

How to Connect to the Remote MySQL Database using PHP

January 1, 2020
28
How to Generate QR Code with PHP using Google Chart API
PHP and MySQL

How to Generate QR Code with PHP using Google Chart API

January 1, 2020
83
Next Post
File Type (extension) Validation with JavaScript

File Type (extension) Validation with JavaScript

How to Generate QR Code with PHP using Google Chart API

How to Generate QR Code with PHP using Google Chart API

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Comparison (3)
  • HTML & CSS (9)
  • Interesting Facts (1)
  • JavaScript (27)
    • jQuery & AJAX (18)
  • PHP and MySQL (48)
  • Security (10)
  • SEO (2)
  • Trademark (2)
  • Tutorials (5)
  • Uncategorized (1)
  • Web Hosting (19)
    • VPS Server (5)
  • WordPress (8)

Recent Posts

  • Is the Trademark valuable to your Brand or domain?
  • Ideas For Ten Fantastic Online Business From Home
  • Some best free WordPress Themes for Affiliate Marketing Websites
  • Home
  • Posts
  • Privacy Policy
  • Terms and Conditions

Built and Maintained With ♥ by AllsWeb Team

No Result
View All Result
  • Home
  • Main Home
  • PHP and MySQL
  • JavaScript
    • jQuery & AJAX
  • WordPress
  • SEO
  • Web Hosting
  • Comparison

Built and Maintained With ♥ by AllsWeb Team

Go to mobile version