3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1

We will see how to send a simple email with the help of three different programming languages: Javascript, Ruby and Python
Before you start you need to create a Gmail account.
Do not forget to accept and allow the “Less secure apps” access in order use…


This content originally appeared on DEV Community and was authored by François

We will see how to send a simple email with the help of three different programming languages: Javascript, Ruby and Python
Before you start you need to create a Gmail account.
Do not forget to accept and allow the "Less secure apps" access in order use your scripts with your Gmail smtp connection.
I'll let you do this on your own, you don't need a tutorial for this
?

Javascript ?

  • For the first script, we are going to use the Nodemailer module:
yarn add nodemailer
  • Require or import the module into your index.js:
const nodemailer = require('nodemailer')
  • Initialize the mailer with our Gmail account info:
// Gmail account info
const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});
  • Create your email:
// Email info
const mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yopmail.com',
  subject: 'Sending email using Node.js',
  text: 'Easy peasy lemon squeezy'
};
  • Sending your email:
// Send email and retrieve server response
transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Here the final code:

const nodemailer = require('nodemailer')

// Gmail account info
const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

// Email info
const mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yopmail.com',
  subject: 'Sending email using Node.js',
  text: 'Easy peasy lemon squeezy'
};

// Send email ?  and retrieve server response
transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Javascript buddy ?

Javascript buddy

Table of contents

  • Javascript ? - Part 1
  • Ruby ? - Part 2 - coming next week...
  • Python ? - Part 3 - coming soon...


This content originally appeared on DEV Community and was authored by François


Print Share Comment Cite Upload Translate Updates
APA

François | Sciencx (2021-06-14T10:05:45+00:00) 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1. Retrieved from https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/

MLA
" » 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1." François | Sciencx - Monday June 14, 2021, https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/
HARVARD
François | Sciencx Monday June 14, 2021 » 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1., viewed ,<https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/>
VANCOUVER
François | Sciencx - » 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/
CHICAGO
" » 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1." François | Sciencx - Accessed . https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/
IEEE
" » 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1." François | Sciencx [Online]. Available: https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/. [Accessed: ]
rf:citation
» 3 ways to send emails with only few lines of code and Gmail – Javascript – Part 1 | François | Sciencx | https://www.scien.cx/2021/06/14/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.