How to Send an Email with Attachment Using PHPMailer and Gmail

PHPMailer is a code library to send (transport) emails safely and easily via PHP code from a web server (mail user agent to the mail submission server). Sending emails directly by PHP code requires a high-level familiarity with Simple Mail Transfer Protocol (SMTP) standard protocol and related issues (such as Carriage return) and vulnerabilities about Email Injection for spamming.

Features of PHPMailer

Some of the features of the PHPMailer we would tap into are further listed below:

  • Plain text, HTML and multipart batched files
  • SSL and TSL (Secure Sockets Layer and Transport Layer Security) but in this tutorial we would be making use of TSL
  • SMTP, POP3
  • Debugging system
  • PHP send mail and mail methods

Terminologies

· PHP

PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

· POP3

In computing, the Post Office Protocol (POP) is an application-layer Internet standard protocol used by e-mail clients to retrieve e-mail from a mail server.

· SMTP

The Simple Mail Transfer Protocol (SMTP) is a communication protocol for electronic mail transmission. User-level email clients typically use SMTP only for sending messages to a mail server for relaying, typically submit outgoing email to the mail server.

· SSL & TLS

Transport Layer Security (TLS), and its now-deprecated predecessor, Secure Sockets Layer (SSL) are cryptographic protocols designed to provide communications security over a computer network. The TLS protocol aims primarily to provide privacy and data integrity between two or more communicating computer applications.

Resources

· Composer – Dependency Manager for PHP

· PHPMailer – Email Sending Library for PHP

· Bootstrap & jQuery CDN

· Active Gmail account

Setup

Composer

To get started with this tutorial we need to install Composer on our machine and a way to get to do this is by running PHP Composer Install or download it.

PHPMailer

Once we have Composer installed on our local machine, we can proceed to install PHPMailer via the description below:

Bootstrap & jQuery CDN

To make use of modern CSS design patterns we adopt the use of Bootstrap and jQuery in this tutorial, hence we will be connecting to their various CDN links available here.

Let’s Start Coding

First, we need to create a file and save as a .php extension. This would enable files processed across the server and thus enable mail delivery. Then we should proceed to structure our tags, connect to our CDN to activate Bootstrap styles and also add a bit of JavaScript to validate input fields, this allows us ensure that fields will not be left empty.

Here we have been able to structure our hypertext markup tag, Next is adding to our script tag to enable us add form validation and form submisson

Next we can put it all together to enable us have a well structured code

Next, we then need to configure our PHPMailer to send mails with attachment and also our Gmail to receive mails via SMTP.

Screenshot

After mail has been sent, the PHPMailer would return an error in this format.

To resolve this problem click here login then click on security then click to switch “Allow less secure apps: ON“.

Fixed the problem of could not be authenticated and boom email get sent to user address.

Email delivery to my Google account shows mail was successfully sent ! Yippee!

Conclusion

What inspired me to write this was due to the fact that I found it challenging while trying to send mails with attachment, but on successful completion of the task I was happy and decided to help so many others who might face this same challenge in the future.

The complete source code is here.

Happy coding!

CategoriesTags

Related Posts