Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 14, 2021 01:56 am GMT

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

(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

Well anyway, in short (for details, checkout the above link)

  • install typescript
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)
tsc --init

(It'll create a .tsconfig file)

  • install ts-node and ts-node-dev
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

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

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: '[email protected]', // 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<[email protected]>', // 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: '[email protected],[email protected],[email protected]', // 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 emailmyTransport.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

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

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

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 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

Wanna know more about me? Come here!
SilvenLEAF.github.io


Original Link: https://dev.to/silvenleaf/easiest-way-to-send-emails-with-javascript-by-silvenleaf-252b

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To