How to send and receive emails in php using PHPMAILER

PHP |

31st Jul, 2019

Click here to share

Have you learnt PHP and it gets to the point that you want to take your coding to the next level by implementing emailing with your application?

Being able to send an email with your application will certainly take your PHP coding to the next level.

In this tutorial, I’m going to teach you how to send an email with your PHP application provided you have internet connection on your local machine.

This tutorial was prompted as a result of what a lot of people pass through to enable email sending with a PHP script which I’m not excluded.

Before we dive into the tutorial proper, let me give you the list of things you must already have so that you can follow properly with the tutorial on how to send email using PHP.

In this tutorial, we are going to be using the most popular PHP mailing library known as phpmailer.

This tutorial covers all you need to know:

The installation steps, the class functions, how to use SMTP authentication with your domain only, how to handle errors, sample code snippet and a download link to download a zip file containing premade template.

            WHY USE PHPMAILER?

Below will highlight some of the major reasons why you should consider using phpmailer from today.

PHPMailer offers many functionalities over the PHP core function mail(), including SMTP SSL encryption and authentication, HTML messages, file attachments and more.

 

Besides that, Phpmailer is much easier to use than generic PHP mail() function and it provides a clean, readable object-oriented syntax.

Below is a brief comparison between Phpmailer and PHP ordinary mail() function.

Firstly, mail() relies on the server email subsystem to work. This means that if you want to change some settings like the SMTP server or the authentication parameters, you need to do it system-wide.

That is usually a quite difficult operation, and unless you have a dedicated server or you are using a local PHP development environment, you are probably not even allowed to do it.

Secondly PHP mail() function makes it  impossible to use different configurations at the same time, for example using multiple SMTP accounts and switching between them programmatically cannot be achieved when using PHP mail() function ,  but with few lines of code you will achieve that using this powerful PHP mailing library known as PHPMailer.

Next, on the list, PHP mail() function is fine for sending simple, plain text emails, but it’s very limiting if you need to do anything more than that. Adding attachments or sending HTML emails, for example, is very difficult with mail(), while with PHPMailer it’s just a matter of a single line of code.

 

Phpmailer also offers many other functionalities over the PHP core function mail(), including SMTP, SSL encryption and authentication, 

HTML messages, file attachments and more.

On top of that, PHPMailer is much easier to use than mail() and provides a clean, readable object-oriented syntax.

In conclusion, phpmailer is quite common more than other PHP mailing libraries one might talk about. But if you are already familiar with any of them such as Zend Mail, SwiftMailer and Zeta Components Mail it is still good that you stick with it.

On the other, if you want to start using one of them and you need to choose which one to go with, PHPMailer is probably the best choice because it’s the most used one.

Now that you have been convinced to choose phpmailer let's get started on how to implement that on our application without wasting any time.

STEPS BY STEPS PROCESS OF INSTALLING PHPMAILER

PHPMAILER earlier than version 6 (2018 current version)  used to provide a “PHPMailerAutoload.php” file. This is a dependency management PHP file which will load all the classes needed to make your application work. Before PHPMailer version 6, all you have to do was to include it in your file and you are good to go.

But starting from version 6, however, this file is no longer provided. Now, to make the PHPMailer class available in your script you have two options at your disposal which we are going to explore here with examples.

HOW TO INCLUDE PHPMAILER IN YOUR PROJECT

  • Use Composer to download PHPMailer and automatically create an autoloader file ~ Best option.

  • Manually download the PHPMailer source code and include the required files yourself ~ Alternative option.

 

 

The first option is the recommended one, because Composer takes care of all the download, downloads all the required dependencies, update any file to the latest version, and as well creates the autoloader.php for you to use in lieu of the former phpAutoloader.php This is especially useful if you need to install other libraries as well, like the one needed for XOAUTH2 Google authentication (This is removed from this post for brevity).

However, the second option may be useful if you don’t want to install Composer for some reason best known to you.

To install phpmailer you must have installed composer.

WHAT IS COMPOSER

Composer is a dependency manager for PHP. It helps you download, install and keep up to date PHP extensions and libraries.

Installing Composer requires just a couple of minutes:

HOW TO INSTALL COMPOSER ON WINDOWS

first, make sure you have a web development environment already installed such as XAMPP, WAMP, EasyPHP, etc because  Composer needs a PHP executable path to work;

Download the Composer installation file from here for  “Windows Installer” and run it;

Follow the installation instructions, and make sure you can select a PHP executable:

Select the path that you have your

php.exe 

In my case I choose

c:/xampp/php/php.exe

 yours should look something like that if you are using xampp. In case you are using another development environment on windows, you can still follow the steps while taking the time to read the on-screen instructions as you proceed forward.

Once the installation is complete, Exit the setup by clicking finish and now you will be able to use the Composer command-line tools to install PHPMailer and other PHP libraries that you will come across such as laravel, symphony, Yii, etc.

Now start your command line by pressing window key on your keyboard together with R win+R. When the pop up shows type cmd and that will immediately launch your command-line tool on your windows machine when you hit enter

Now, let's look into installing phpmailer the best way to enjoy the maximum junk using composer.

HOW TO INSTALL PHPMAILER ON WINDOWS

With your command-line tool open  cd(change direction ) to the desired folder, you wish to install phpmailer and execute the installation command.

Note: you must be connected to the internet to carry out this task.

In my case, I wanted to install phpmailer inside my docs of my xampp.

Step 1

Create a folder inside the htdocs of your xampp (if you are on xampp otherwise install to the root folder of your local development environment)

Step 2

Now that you have your folder ready cd( change the direction ) to the folder you just created in the root directory of your development environment by executing the following command in your terminal

(cd c:/xampp/htdocs/folder_name)

Step 3

Click enter and you will get something like this

(c:/xampp/htdocs/folder_name) and a blinking cursor at the end of it.

Step 4

Now type the following command to automatically download and install PHPMailer to your local development machine

composer require PHPMailer/PHPMailer 

Step 5

Wait for few minutes for the command to finish executing and when it’s done open the (folder_name) and viola you have gotten PHPMailer with the latest version installed on your local machine.

Step five brings you very close to sending your first email with PHP if you have not done that before.

BRIEF EXPLANATION OF WHAT JUST HAPPENED WITH THE COMPOSER AND THE COMMAND LINE TOOL

Composer generated an

autoload.php

file that you can use to include the installed libraries, in this case, PHPMailer.

This file is located under the “vendor” directory by default, although you can configure Composer to use a different directory name.

 

So, assuming your installation directory is

C:/xampp/htdocs/folder_name

 you need to include the

C:/xampp/htdocs/folder_name/vendor/autoload.php

file in your project.

Don’t worry I have done everything for you and packaged it in a special folder for you.

Now let’s get started in generating our custom script to send mail.

Note again: if you are on the local machine you may not be able to get any good result like what I explained, but you can still follow up and understand everything.  

EXAMPLE OF SENDMAIL USING PHPMAILER

Folder structure

Folder_name

{ composer.json, composer.lock, vendor{composer, phpmailer, autoload.php}

}

The above is type of our folder hierarchy.

To accomplish sending mail using the above files you need to create at least two files named as you so desire but for sake of brevity, I named mine mail.php and contact.php.

A file with the HTML for to be submitted is included in the contact.php and action is channelled to mail.php.

Download the file and follow up so that you will understand better.

Download phpmailer

Download mail & contact.php

Leave a Reply

RELATED POSTS


Total of 1 Comment
Emmanuel David Emmanuel David
Sending Email in php is very easy with this lesson