Vue Send Email Guide

(Free And Without Backend)

Vue Send Email

We all know that Vue's primary purpose is to be running in browsers. Because of that, it's super powerful but also has quite a few limitations. For example, being client-side only, it can't do certain things that we would take for granted on the server-side. A good example would be sending emails. As all the client code is public, you wouldn't want to expose your mail server credentials to the whole world.

So the question is, how to send emails from Vue without a backend? Luckily, this guide answers it perfectly.

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

Create the email function inside of Vue 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. We'll be using vue hooks, but the example is straightforward to port to the class components. See the sample code.

1import { ref } from 'vue'
2
3const UseEmail = (endpointUrl) => {
4  const sumbmitted = ref(false);
5  const loading = ref(false);
6  const error = ref();
7
8  function sendEmail(data) {
9    loading.value = true;
10    sumbmitted.value = false;
11    error.value = undefined;
12
13
14    fetch(endpointUrl, {
15      method: "POST",
16      headers: {
17        Accept: "application/json",
18        "Content-Type": "application/json",
19      },
20      body: JSON.stringify(data),
21    })
22      .then((response) => {
23        // Endpoint thinks that it's likely a spam/bot request, you need to change "spam protection mode" to "never" in HeroTofu forms
24        if (response.status === 422) {
25          throw new Error("Are you robot?");
26        }
27
28        if (response.status !== 200) {
29          throw new Error(`${response.statusText} (${response.status})`);
30        }
31
32        return response.json();
33      })
34      .then(() => {
35        sumbmitted.value = true;
36        loading.value = false;
37      })
38      .catch((err) => {
39        error.value = err.toString();
40        loading.value = false;
41      });
42
43  }
44
45  return {
46    sumbmitted,
47    loading,
48    error,
49    sendEmail
50  }
51
52}
53
54export default UseEmail;

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

Add component into your app, 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.

1<template>
2  <div class="app">
3    <header class="app-header">
4      <img src="./assets/logo.png" alt="Logo" class="app-logo" />
5      <div style="margin: 1rem 0; font-size: 2rem">
6        <span v-if="sumbmitted"> Done, email was sent! </span>
7        <span v-if="error"> Unexpected error: {{ setError }} </span>
8        <span v-if="loading"> Email is being sent now... </span>
9      </div>
10      <div>
11        <button style="margin: 1rem 0" @click="sendExample">
12          Send test data
13        </button>
14      </div>
15    </header>
16  </div>
17</template>
18
19<script>
20import UseEmail from "./UseEmail";
21
22export default {
23  name: "App",
24
25  setup() {
26    const { loading, sumbmitted, error, sendEmail } = UseEmail(endpointUrl);
27    const sendExample = () => {
28      sendEmail({
29        example_user: "example@gmail.com",
30        example_data: new Date().toISOString(),
31      });
32    };
33
34    return { loading, sumbmitted, error, sendExample };
35  },
36};
37</script>

That's it!

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


Related questions

How do I send form data to an email in Vue?

It's doable via HeroTofu; check out the Vue Contact Form guide guide.

How do I create a contact form in Vue JS?

This guide has an example of the ready-to-use contact form.

How do I send an email in the Nuxt JS?

As it uses the Vue framework under the hood, the process is identical to this guide.

Can I use Nodemailer in Vue?

Unfortunately, no. Nodemailer is the backend-only library.

How do I send an email with Sendgrid and Vuejs?

For either of these, you would need to use the backend. This process could expose your credentials which can raise big security concerns. You don't need any email provider when using HeroTofu.

How do you use EmailJS?

EmailJS is a fantastic email service that also lets you send emails from the frontend. Yet, if you operate mainly with form-like data structures, HeroTofu would fit a bit better.

 
Vue Email Endpoint -- Start NowFree and Paid plans