PHP

How to Send an Email using PHPMailer

Description:

How to send an Email using PHPMailer– in this article, I am going to show you how to use a PHP mailer to send an email with a full code explanation.


PHPMailer:

Phpmailer is a PHP class for sending emails. It provides more functionality than the mail() function. It provides a package of functions for sending HTML emails, email attachments, and inline images. It also supports SMTP. It provides all features of SMTP-based email, eg., multiple recipients via to, cc, bcc, etc.

Phpmailer class can be downloaded from its website at https://github.com/PHPMailer/PHPMailer.

Its documents are also available on this site.

The following paragraphs show how phpmailer class can be used to send email using a Gmail account.

The following is required for using phpmailer class:

  • Phpmailer source code
  • Patch for phpmailer fo SSL(required by Gmail)
  • A Gmail account



PHPMailer Installation Methods:

First Method of PHPMailer Installation:

Download PhpMailer :

Download the phpmailer source code, from the link given above

PHPMailer

extract the zip file of the phpmailer source code. The source code will consist of the following Php files in the src folder:

  • Exception
  • OAuth
  • PHPMailer
  • POP3

Copy (or upload) the files to the same folder (directory) that contain the PHP document that will use these files.

Extract the download zip folder and copy the Exception, PHPMailer, POP3, SMTP files to your project folder which is saved in htdocs

PHPMailer

PHPMailer


Second Method of PHPMailer installation:

PHPMailer

I shared a link above to download the PHP mailer repository so I’ve opened up that link and here you can see a command composer require phpmailer/phpmailer this command is used for installing the PHP mailer package using the composer. it does not install using a Command prompt for this you must install composer. When I paste this link in my command prompt I got the issue as you can see in the below picture

PHPMailer

So for installing the phpmailer I download composer software and installed it. To check the composer whether it is installed or not simply run the composer command in the command prompt it will show you package is installed or not:

PHPMailer


Now install the phpmailer in your project folder using the below code

PHPMailer

As you can see the phpmailer is successfully installed.

When installing the phpmailer using composer the following folder and files will be created

PHPMailer

 

PhpMailer Program for sending an email:

Index file code

Now create a form handler for sending emails using phpmailer class.

The code for the form for sending emails is shown below:

Save the above code as index.php.


phpmailerTest file code:

Save the above code as a form handler with the name phpmailerTest.php.

Load the index.php file in a web browser

PHPMailer

Enter information as shown below:

PHPMailer

When send button is pressed a message will show as you can see in the below figure

PHPMailer

The email sent by the above form using the phpmailer class is received as shown below:

PHPMailer



Code explanation:

To use phpmailer, the phpmailer class is called and an instance of this class is created as shown below:

phpmailer provides several methods of sending emails. SMTP is one of them. Other methods are the mail() function, Sendmail and qmail.

the values submitted by the email form with the Post method are placed in PHP variables:

The function $mail-> isSMTP(); indicates that the email will be sent using SMTP.

A valid SMTP server is required for using the SMTP method. In this example, I will use the gmail server. The setting for the Gmail server is specified as shown below:

Next, specify the email account on the Gmail that is to e used for sending emails. The password for the email account is also specified.

For example, in my case my address on Gmail is kali07490@gmail.com. Suppose my password for this is xxxxx(use your own password). This information is specified as shown below:


Specify the email address and name of the person to whom the email is being sent:

User information is specified as shown below:

For those browsers which cannot display HTML tags, alternative information is specified as shown below:

Specify the message and the email and name of the sender as shown below:

Specify IsHTML(true) to enable HTML tags in the message, as shown below:

$mail->Send() is used to send the email.


It is a good practice to send the email and check whether the email has been sent successfully, as shown below:

Related Article:

Php Syntax with basic examples

Related Articles

One Comment

  1. Hello, I enjoy reading all of your post. I like to
    write a little comment to support you.

Leave a Reply

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

Back to top button