Web Developement
Now Reading
Send an Email Using Javascript (No Server Side Code)
0

Send an Email Using Javascript (No Server Side Code)

by Adrian RandallJune 9, 2015

We’re having more and more need to send an email using Javascript so I thought I’d do a quick write up why we’d do this and how we do it.

Why we may need to send an email with only Javascript

There are two main reasons we have needed to do this and although there may be other solutions to our issue we’ve found this the fastest. The first reason is we’ve built a mobile application using Cordova and our client doesn’t want a server at all. The second reason is we don’t have control over the server so can’t install or setup a server side SMTP mail server or similar functionality.

How we do it

There are a couple of programs which can do this but the easiest way we’ve found is to use Mandrill (which is built by the guys at MailChimp) and make use of their API. By using this we can just send a JSON request to their endpoint without including any external Javascript libraries. No third party support is needed to get your code working so less things to go wrong.

The javascript code below can be copied directly into a javascript file and the sendEmail function will be called as soon as the page is loaded. The only variables you need to set are:

1. “Mandrill API Key” – To get this, sign up for a free account at Mandrill App (https://mandrillapp.com) and create a key.
2. “from_email” – The email address you want the email to come from
3. “to_email” – The email address you’re looking to send the email too

Set those parameters and load the page then you should receive an email at the to_email address.

Tips

The easiest way to use this is to remove the action from a html form and then set the onSubmit to “sendEmail()” and then collect the input parameters in the sendEmail function. You can then compile all the values from the contact form into a HTML email by just appending them to the $htmlEmailString like below:

 

 

 

About The Author
Adrian Randall
I'm a digital marketing specialist, love working on digital business and coding on just about anything. I'm the founder of Arcadian Digital and this site shares some of our knowledge and practices.

Leave a Response