How to send an email from JavaScript


I want my website to have the ability to send an email without refreshing the page. So I want to use Javascript.

<form action="javascript:sendMail();" name="pmForm" id="pmForm" method="post">
Enter Friend's Email:
<input name="pmSubject" id="pmSubject" type="text" maxlength="64" style="width:98%;" />
<input name="pmSubmit" type="submit" value="Invite" />

Here is how I want to call the function, but I'm not sure what to put into the javascript function. From the research I've done I found an example that uses the mailto method, but my understanding is that doesn't actually send directly from the site.

So my question is where can I find what to put inside the JavaScript function to send an email directly from the website.

function sendMail() {
    /* ...code here...    */

This question is tagged with javascript email

~ Asked on 2011-09-11 20:50:22

The Best Answer is


You can't send an email directly with javascript.

You can, however, open the user's mail client:'mailto:[email protected]');

There are also some parameters to pre-fill the subject and the body:'mailto:[email protected]?subject=subject&body=body');

Another solution would be to do an ajax call to your server, so that the server sends the email. Be careful not to allow anyone to send any email through your server.

~ Answered on 2011-09-11 20:52:08


Indirect via Your Server - Calling 3rd Party API - secure and recommended

Your server can call the 3rd Party API after proper authentication and authorization. The API Keys are not exposed to client.

node.js -

const mandrill = require('node-mandrill')('<your API Key>'); 

function sendEmail ( _name, _email, _subject, _message) {
    mandrill('/messages/send', {
        message: {
            to: [{email: _email , name: _name}],
            from_email: '[email protected]',
            subject: _subject,
            text: _message
    }, function(error, response){
        if (error) console.log( error );
        else console.log(response);

// define your own email api which points to your server. '/api/sendemail/', function(req, res){
    let _name =;
    let _email =;
    let _subject = req.body.subject;
    let _messsage = req.body.message;

    //implement your spam protection or checks. 

    sendEmail ( _name, _email, _subject, _message );


and then use use $.ajax on client to call your email API.

Directly From Client - Calling 3rd Party API - not recomended

Send an email using only JavaScript

in short:

  1. register for Mandrill to get an API key
  2. load jQuery
  3. use $.ajax to send an email

Like this -

function sendMail() {
      type: 'POST',
      url: '',
      data: {
        'key': 'YOUR API KEY HERE',
        'message': {
          'from_email': '[email protected]',
          'to': [
                'email': '[email protected]',
                'name': 'RECIPIENT NAME (OPTIONAL)',
                'type': 'to'
          'autotext': 'true',
          'subject': 'YOUR SUBJECT HERE!',
     }).done(function(response) {
       console.log(response); // if you're into that sorta thing

Note: Keep in mind that your API key is visible to anyone, so any malicious user may use your key to send out emails that can eat up your quota.

~ Answered on 2014-01-10 10:14:11

Most Viewed Questions: