An Interest In:
Web News this Week
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
June 14, 2021 10:05 am GMT
Original Link: https://dev.to/fralps/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1-4i92
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 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 infoconst transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'yourpassword' }});
- Create your email:
// Email infoconst mailOptions = { from: '[email protected]', to: '[email protected]', subject: 'Sending email using Node.js', text: 'Easy peasy lemon squeezy'};
- Sending your email:
// Send email and retrieve server responsetransporter.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 infoconst transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'yourpassword' }});// Email infoconst mailOptions = { from: '[email protected]', to: '[email protected]', subject: 'Sending email using Node.js', text: 'Easy peasy lemon squeezy'};// Send email and retrieve server responsetransporter.sendMail(mailOptions, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); }});
Javascript buddy
Table of contents
- Javascript - Part 1
- Ruby - Part 2 - coming next week...
- Python - Part 3 - coming soon...
Original Link: https://dev.to/fralps/3-ways-to-send-emails-with-only-few-lines-of-code-and-gmail-javascript-part-1-4i92
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To