Easiest Way to Send Emails With JavaScript by SilvenLEAF
Tags: javascript, typescript, node, silvenleaf
Published: 2021-10-14 01:56:41

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
```bash 
npm init -y
```
(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](https://dev.to/silvenleaf/typescript-setup-by-silvenleaf-1aen)**

Well anyway, in short (for details, checkout the above link)
- install typescript
```bash
npm i typescript 
```

- 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)
```bash
tsc --init
```
(It'll create a **.tsconfig** file)

- install **ts-node** and **ts-node-dev**
```bash 
npm i ts-node ts-node-dev
```

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
```bash 
npm i nodemailer @types/nodemailer
```
(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
```typescript
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!`);
  }
})
```
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
```bash
ts-node app.ts
```
(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?

![Failed to send](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y4qey6y8gmw7qhihp5u.png)

Because Google blocked that request! So in order to make it work, we need to allow it first to send emails from that email account. How? Google **"less secure apps"** and open the first link.
![Google Less Secure Apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71g2jait16s2ols1me5f.png)

Now you'll see something like this
![Less Secure Apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7yxu67f0h5cm01tco7t.png)
Make the toggle button on (Allow less secure apps: ON)

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


# What's NEXT?
#### 1. Improved AI BOT that can do anything
#### 2. Insane stuff with JavaScript/TypeScript
#### 3. Debugging TypeScript with VS Code Debugger
#### 4. How to automate anything
#### 5. Sequelize Hooks
#### 6. Automate creating DB Schemas
#### 7. How to create an Android APP with NO XP
(including apk generating)


# Got any doubt? 
Drop a comment or Feel free to reach out to me @SilveLEAF on Twitter or [Linkedin](https://www.linkedin.com/in/silvenleaf/)

Wanna know more about me? Come here! 
[SilvenLEAF.github.io](https://SilvenLEAF.github.io)