Send Email using JavaScript in VUE application without any use of server language
Abstract:
In real world sending Email from web application is an important feature. There are lot of methods available to implement email feature. It is important we choose efficient method as per our codebase. Send Email feature require server call, it forces us to use server language like Node, PHP.
In this article we will focus on send email feature using JavaScript without any server language. We will use SMTP.js JavaScript Library to perform server responsibilities. We will implement email feature in Vue application.
I will expect reader has basic understanding of JavaScript and VUE.js
For sending Email we will need SMTP server. You can use your choice of SMTP server for example Gmail, EmailJS, Elastic Email. SMTP.JS accept security token of SMTP server to connect with SMTP Server. I have used elastic mail server.
How to add SMTP.js library to VUE app?
To use SMTP.js we need to import smtp.js library into our Contact form component. You can go to https://smtpjs.com/ and download smtp.js library to your local. You will need to create smtp.js file or Pick file name of your choice in your local project where you will store downloaded smtp.js library.
Create contact form in VUE.js to submit email. In below screen shots you can find template code and style.
How to import smtp.js library into your Vue component?
There is named export “Email” used in local smtp.js file. We will use import declaration inside contact component to import smtp.js.
We will bind form fields using v-model. We have data() property declare which will hold this form field values. Now we need to build method sendEmail() under methods hook for our contact component. This method will fire when we click on submit button. Under secureToken you will need to put your smtp service token. Refer below screen shot.
Now all set, you can fill out contact form and send Email . You will receive email.
You will see confirmation box like below on your webpage if email sent is successful
Now you can check receiver mailbox to verify.
if you like my article please don’t forget to clap. It will encourage me to publish more which solve real world problems. Thanks!