jQuery Send Email Guide

(Free And Without Backend)

jQuery Send Email

jQuery is a client-side library that is designed to run in browsers. Because of that, it has some limitations. One of them is that you can't do things like sending emails via frontend.

This guide will show you how to overcome that.

P.S. If you want to know how to send form data (such as contact form fields), you can read the HTML Contact Form guide.

Create the email function inside of jQuery App

You'll send the email on that function. We’ll need to get two arguments for it to work: endpoint URL and "data." Then, a simple POST request and returning the state with the response of that request. See the sample code.

function sendEmail(endpointUrl, data, onSuccess, onError) {
  $.ajax({
      type: "POST",
      url: endpointUrl
      data: JSON.stringify(data),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: onSuccess,
      error: function(xhr, status) {
        if (typeof this.statusCode[xhr.status] !== 'undefined') {
          return false;
        }

        onError(err);
      },
      statusCode: {
        // Endpoint thinks that it's likely a spam/bot request, you need to change "spam protection mode" to "never" in HeroTofu forms
        422: function(response) {
          alert("Cannot send request, are you robot?");
        },
      }
    });
}

Signup for the free HeroTofu account

To create an account, go to herotofu.com . It is capable of handling all the complex email sending work. You can take the 14-day free trial and then leave it in the free forever plan. It's usually more than enough for small websites.

The registration is easy. After filling in the primary fields, you should receive an email confirmation.

Herotofu signup

Create a new backend, set your email, spam protection

Go to app.herotofu.com/forms to make your first form after you have confirmed your email address. In this case, the form will function as our API backend/endpoint. Attach your preferred email address where you'd like to receive your data submits. Be sure to select spam protection as "never." You'll be sending API requests yourself, so they shouldn't ever be detected as spam by HeroTofu.

Remember to copy the endpoint URL once you hit submit.

Herotofu Forms List

Call the created function, pass some data

The last step is implementation. Call your created function with the endpoint URL and actual data. HeroTofu will forward that data to your email.

var onSuccess = function(response) {
  alert("Success!");
  console.log(response);
};

var onError = function(err) {
  alert("Error!");
  console.error(err);
};

// TODO: replace the endpoint url with your own
sendEmail("https://herotofu.com/start", {
  example_user: "user@example.com",
  example_data: new Date().toISOString(),
}, onSuccess, onError);

// The same code as in previous snippet...
function sendEmail(endpointUrl, data, onSuccess, onError) {
  $.ajax({
      type: "POST",
      url: endpointUrl
      data: JSON.stringify(data),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: onSuccess,
      error: function(xhr, status) {
        if (typeof this.statusCode[xhr.status] !== 'undefined') {
          return false;
        }

        onError(err);
      },
      statusCode: {
        // Endpoint thinks that it's likely a spam/bot request, you need to change "spam protection mode" to "never" in HeroTofu forms
        422: function(response) {
          alert("Cannot send request, are you robot?");
        },
      }
    });
}

That's it!

You can now send emails from your jQuery application. All without the burden of the server/backend code.