Home > Uncategorized > Send Email via client-side Javascript

Send Email via client-side Javascript

logo

Typically, if you want to send an Email via Javascript, like in response to a feedback form, or other client-side user interaction. Then you end up having to write server-side code to send the email, and then client side code to make an Ajax call to the server, or force the user to move to another page.

Tired of writing this code over and over again for every web project I did, and increasingly every Phonegap App I wrote, I decided to write a general script that can send an email from Javascript, with no server side code required. – and no dependencies, like jQuery.

So, here’e the website http://www.smtpjs.com 

It works as follows, you add a script reference to

http://smtpjs.com/smtp.js

Then you send the email like so…

Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
"smtp.yourisp.com",
"username",
"password");

But Wait!!!, I can hear you scream… My username and password are visible in client side script. – So The trick to this, is that you can encrypt the credentials on SMTPJS.com and receive a security token, and then use this to send the email instead.

In which case you can send Email as follows

Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
{token: "63cb3a19-2684-44fa-b76f-debf422d8b00"});


Which is much more secure, and we’d recommend this even if your code is packaged inside a Phonegap/Cordova App.

Advertisements
Categories: Uncategorized
  1. Vasanth
    February 4, 2016 at 12:24 pm

    Can’t this option still be misused by using the same tokens and send spam via this email ?

    • February 12, 2016 at 12:42 pm

      @Vasanth, Each token is locked down by referral domain as well. There’s a honey-pot trap on the API, so that once a potential hacker tries to use it with the wrong domain, then the IP is caught and locked down.

  2. robwsi
    June 16, 2017 at 10:31 am

    how to use html in email body in smtp.js?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: