In one of the previous articles, we have mentioned sending an email using C#, but now in this article, I have mentioned how to send email from Javascript using Smtpjs, although it is not recommended to use Javascript for sending emails, you should use Server-side languages like C#, Java or PHP to send emails in encrypted form.

So, here is the simple code to send email using Smtpjs in Javascript.

send-email-from-javascript-min.png

First, you will have to include Smtpjs reference file

<script src="https://smtpjs.com/v3/smtp.js">
</script>

and use the below JS code to send email using it

function sendEmail() {
 Email.send({
    Host : "smtp.yourisp.com", // smtp.gmail.com
    Username : "username", //example@gmail.com
    Password : "password", // password
    To : 'them@website.com', //receiver email id , send to multiple receiver by using "," as seperator
    From : "you@isp.com", // your email
    Subject : "Your Subject here",
    Body : "Email body"
 }).then(
   message => alert("Email sent successfully")
 );
}

Considering you have below HTML, you can send email on button Click

<form method="post">
	<input type="button" value="Send Email" onclick="sendEmail()"/>
</form>  

Now, as you can see from the above code, we need to do some configurations on our SMTP server to get this working.

Suppose, you are using gmail as SMTP to send mails, then make sure you have properly configured these below settings:

  • Allowed access in Gmail for less secure apps: To use Gmail SMTP you will need to enable less secure apps access in your Gmail settings, which is not recommended by google but again to use this service we will have to allow it, otherwise, you won't be able to send emails using JS.
  • Disabled 2-step factor authentication: Again, we would have to compromise security settings in order to use the above code, so in your settings, fisable 2-step authentication.

Once you have done the above changes, we are good to go.

Why do we need the above changes?

Basically, we are allowing third-party apps or code to use our Gmail credentials, without any issue.

If we will not disable the above settings, Gmail will block any SMTP handshake using a third-party application or code and will send us a notification about this also.

Sending attachments

You can also send attachments using Smtpjs, you just need to add another property "Attachments" in JS code, as shown below

function sendEmail() {
  Email.send({
    Host : "smtp.yourisp.com", // smtp.gmail.com
    Username : "username", //example@gmail.com
    Password : "password", // password
    To : 'them@website.com', //receiver email id , send to multiple receiver by using "," as seperator
    From : "you@isp.com", // your email
    Subject : "Your Subject here",
    Body : "Email body"
  Attachments : [
  	{
  		name : "smtpjs.png",
  		path:"https://example.com/2022/11/smtpjs.png"
  	}]
  }).then(
  	message => alert("mail sent successfully")
  );
}

Here is the complete HTML code, to send email using Javascript and SmtpJs

<!DOCTYPE html>
<html>
<head>
	<script src="https://smtpjs.com/v3/smtp.js"></script>  
	<script type="text/javascript">
		function sendEmail() {
			Email.send({
				  Host : "smtp.yourisp.com", // smtp.gmail.com
                                  Username : "username", //example@gmail.com
                                  Password : "password", // password
                                  To : 'them@website.com', //receiver email id , send to multiple receiver by using "," as seperator
                                From : "you@isp.com", // your email
				Subject : "Email_Subject",
				Body : "Email_body",
			})
			.then(function(message){
				alert("Email sent successfully")
			});
		}
	</script>
</head>
<body>  
	<form method="post">
		<input type="button" value="Send Email" onclick="sendEmail()"/>
	</form>  
</body>
</html>

As you can see from the above code, we will be showing passwords in our HTML/JS code, but this can be encrypted as there are some ways to do it.

Is there any other way to send an email using Javascript?

Short answer, yes, there are some other plugins, which allow you to send email using Javascript, like https://www.emailjs.com/ which you can check

But, I will recommend you to use Server-side languages to send an email, so you don't need to change any settings in your Gmail or any other ISP providers.

You may also like to read:

Convert html to word with images (Using Javascript OR Using jQuery plugin)

How to get user location using Javascript / HTML5 Geolocation?

Create a simple calculator using Javascript