SENDING MAILS 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
· PHPMailer
· 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 follow this link to download it https://getcomposer.org/download/
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 https://www.bootstrapcdn.com/
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.
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 https://myaccount.google.com/lesssecureapps login then click on security then click to switch Less secure app access ON
Fixed the problem of could not be authenticated
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.
Here is a link to the source codes https://github.com/LarrySul/PHPmailer
Happy coding 😍😍😍😍😍