Sending Emails with EmailJS and Gmail Made Easy: A Step-by-Step Tutorial

--

Photo by Solen Feyissa on Unsplash

Step 1

Sign up for EmailJS Before you can start using EmailJS, you’ll need to sign up for an account. You can do this by going to the EmailJS website and clicking on the “Sign Up” button.

Step 2

Set up your Gmail account To use Gmail with EmailJS, you’ll need to set up a few things first. Specifically, you’ll need to enable “Less secure apps” and “IMAP access” in your Gmail account.

To enable "Less secure apps", follow these steps:

  1. Log in to your Gmail account.
  2. Go to the "Security" section of your Google Account.
  3. Scroll down to the "Less secure app access" section.
  4. Turn on the toggle to allow less secure apps.

To enable "IMAP access", follow these steps:

  1. Log in to your Gmail account.
  2. Go to the "Settings" section of your Gmail account.
  3. Click on the "Forwarding and POP/IMAP" tab.
  4. Under "IMAP access", select "Enable IMAP".

Step 3: Install EmailJS

Once you’ve signed up for EmailJS and set up your Gmail account, you’ll need to install the EmailJS SDK in your project. You can do this using npm:

npm install emailjs-com - save

Step 4: Set up your email service

To send emails with EmailJS, you’ll need to set up your email service. This involves creating an email template and specifying the service parameters.

Here’s an example email template:

<html>
<body>
<h3>Subject: {{subject}}</h3>
<p>{{message}}</p>
</body>
</html>

You can customize this template as needed, but be sure to include the double curly braces ({{}}) around the variables that you'll be passing in from your JavaScript code.

To specify the service parameters, you can use the EmailJS dashboard. Here’s how:

  1. Log in to your EmailJS account.
  2. Click on the “Email Services” tab.
  3. Click on the “Create new service” button.
  4. Select “Gmail” as your email service.
  5. Enter a name for your service.
  6. Enter your Gmail username and password.
  7. Click on the “Create” button.

Step 5:

Send an email with EmailJS With your email service set up, you can now send emails using EmailJS in your JavaScript code. Here’s an example:

const emailjs = require('emailjs-com');
const templateParams = {
to_name: 'John Doe',
from_name: 'Jane Doe',
subject: 'Hello from EmailJS',
message: 'This is a test email sent using EmailJS!'
};

emailjs.send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams, 'YOUR_USER_ID')
.then((response) => {
console.log('SUCCESS!', response.status, response.text);
}, (err) => {
console.log('FAILED...', err);
});

Be sure to replace the placeholders in the emailjs.send() function with your own service ID, template ID, and user ID. You can find these values in the EmailJS dashboard under your email service.

That’s it! With these steps, you should be able to send emails using EmailJS with Gmail and JavaScript.

--

--

The Modern Developer Academy - by Alex Madrazo
The Modern Developer Academy - by Alex Madrazo

Written by The Modern Developer Academy - by Alex Madrazo

Guatemalan software enthusiast in Madrid, leading high-performing engineering teams. Passionate about tech, entrepreneurship, and economics.

Responses (2)