in

Easiest Way to Send Emails With JavaScript by SilvenLEAF


Ahoy there Sweetlings! Let’s send some emails! But in a SUPER FUN Way!! For a Sweeter fun, let’s add TypeScript as well! Even if you don’t know it, don’t worry, we will be going from Level 0!! As long as you know the basics for JavaScript, hop on!



Step 0: Begin the Project

Create a folder and open it in your favorite editor (mine VS Code). Then type this command on your project terminal

npm init -y
Enter fullscreen mode

Exit fullscreen mode

(It’ll create a package.json file to track all the packages that you’d download and so on)



Bonus Step: Adding TypeScript

For those who are a bit lost on how to set up the environment and run the TypeScript files, check this one out TypeScript SETUP by SilvenLEAF

In short (for details, checkout the above link)

npm i typescript 
Enter fullscreen mode

Exit fullscreen mode

  • init our tsconfig
    (make sure you already have typescript globally installed, if not type “npm i -g typescript”. And don’t get it confused with the previous normal “npm i typescript” command)
tsc --init
Enter fullscreen mode

Exit fullscreen mode

(It’ll create a .tsconfig file)

  • install ts-node and ts-node-dev
npm i ts-node ts-node-dev
Enter fullscreen mode

Exit fullscreen mode

Now let’s create an app.ts file and send some freaking emails!



Step 1: Sending Emails

First install the required packages with this command

npm i nodemailer @types/nodemailer
Enter fullscreen mode

Exit fullscreen mode

(By the way, “npm i X” is the short version for “npm install X”)

Now let’s send some freaking emails! Inside the app.ts file, write these

import nodemailer from 'nodemailer';


// let's create the transport (it's the postman/delivery-man who will send your emails)
const myTransport = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: 'YOUR_GMAIL_ACCOUNT@gmail.com', // your gmail account which you'll use to send the emails
    pass: 'YOUR_GMAIL_PASSWORD', // the password for your gmail account
  }
});






// defining the content of the email (I mean, what will be on the email)
const mailOptions = {
  from: 'SilvenLEAF<YOUR_GMAIL_ACCOUNT@gmail.com>', // from where the email is going, you can type anything or any name here, it'll be displayed as the sender to the person who receives it
  to: 'user1@gmail.com,user2@crazy.com,user3@anything.com', // the email address(es) where you want to send the emails to. If it's more than one person/email, seperate them with a comma, like here how I seperated the 3 users with a comma

  subject: 'Sending Some Freaking Email', // your email subject (optional but better to have it)
  text: `Hello there my sweetling! Let's send some freaking emails!`, // your email body in plain text format (optional) 

  // your email body in html format (optional)
  // if you want to send a customly and amazingly designed html body
  // instead of a boring plain text, then use this "html" property
  // instead of "text" property
  html: `<h1 style="color: red;text-align:center">Hello there my sweetling!</h1>
         <p style="text-align:center">Let's send some <span style="color: red">freaking</span> emails!</p>`,
}






// sending the email
myTransport.sendMail(mailOptions, (err) => {
  if (err) {
    console.log(`Email is failed to send!`);
    console.error(err);
  } else {
    console.log(`Email is successfully sent!`);
  }
})
Enter fullscreen mode

Exit fullscreen mode

Yohoooo! We just created the email sender file. Now, let’s run it. Type this following command in your terminal to run this typescript file

ts-node app.ts
Enter fullscreen mode

Exit fullscreen mode

(It’s the TypeScript version of node app.js)

Yahoooo! We just send a freaking email to some freaking users!! Yay! But really? You must have seen a crazy error like this right?

Because Googled blocked that request! So in order to make it work, we need to allow it to send emails from that email account first. How? First google “less secure apps” and open the first link.
Google Less Secure Apps

Now you’ll see something like this
Less Secure Apps
Make the toggle button on (Allow less secure apps: ON)

Now run that email sender file once again! And HURRAH!!! You just send a freaking email!! Congrats Sweetling!



Source: https://dev.to/silvenleaf/easiest-way-to-send-emails-with-javascript-by-silvenleaf-32lp

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

Kubernetes Best Practices Validation in Azure Pipelines with Datree

React Hook used to make API calls