Send mail using phpmailer tutorial

Hey technoz, previously we’ve seen how to send an email directly from php script in which we have used php mail() function. But now, we will use phpmailer – a third party, open source library which makes this process quite easier. Sending Email using phpmailer library has various advantages over traditional php mail() function as follows:

  • It is open source library i.e. Free to use
  • Guaranteed delivery (in just 3-5 seconds)
  • Very easy to integrate
  • No need to learn new API
  • Works efficiently across all php versions
  • Object oriented approach
  • Most CMS like WordPress, Drupal, 1CRM, SugarCRM, Yii, Joomla! uses phpmailer

Considering all above these benefits, most developers prefer to use this library. This tutorial will guide you how to integrate it in our php website.

1. Download PHPMailer

We will first need to download the phpmailer library. Please go through this link to download.You will get a .rar file. Extract it, rename it as phpmailer, and put the folder in your working directory.

2. Design the mail template

We will now design the email template. create file template.html and paste the following code in it.

After running the above file, you will get an idea how our mail will look like. However, there are some php variables in the file which will be printed as it is currently. See the screenshot of page below.

Send mail using phpmailer library instead php mail() function- Technopoints

3. Creating form

Now, we have to create the form which takes name, email and recipient’s message as an input. Following php file is index.php

style the form by making file index.css as shown below.

Let’s see our creation below.

Send mail using phpmailer library instead php mail() function, send email using phpmailer library- Technopoints

4. Sending Email using phpmailer library

Lets create the actual code responsible for sending an email using phpmailer library. code the following file sendmail.php

The above file receives variables $name, $email and $msgbody which are used in the email. To send the email with the above code, we are putting the whole code of template.html file in variable $message which is the actual code to be sent via email. Remember to update username and password credentials with yours in above files.

That’s it! Our code is now able to send email using phpmailer library. We will see the acknowledgement after successful mail delivery as shown in image below.


Send mail using phpmailer library instead php mail() function- Technopoints

This code is free to include in your projects. Thanks for reading this tutorial. Feel free to comment below if you have any queries.

If you want to directly download and apply the code including phpmailer library, click on the link below.

You can also see android tutorial to implement a cool style snackbar in app.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.