Send Email via client-side Javascript
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.
Can’t this option still be misused by using the same tokens and send spam via this email ?
LikeLike
@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.
LikeLike
how to use html in email body in smtp.js?
LikeLike