Simple Example
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
const nodemailer = require('nodemailer'); // These id's and secrets should come from .env file. const CLIENT_ID = 'YOUR CLIENT ID'; const CLEINT_SECRET = 'YOUR CLIENT SECRET'; async function sendMail() { try { const accessToken = await oAuth2Client.getAccessToken(); const transport = nodemailer.createTransport({ service: 'gmail', auth: { type: 'OAuth2', user: 'yours authorised email address', clientId: CLIENT_ID, clientSecret: CLEINT_SECRET, accessToken: accessToken, }, }); const mailOptions = { from: 'SENDER NAME <yours authorised email address@gmail.com>', to: 'to email address here', subject: 'Hello from gmail using API', text: 'Hello from gmail email using API', html: '<h1>Hello from gmail email using API</h1>', }; const result = await transport.sendMail(mailOptions); return result; } catch (error) { return error; } } sendMail() .then((result) => console.log('Email sent...', result)) .catch((error) => console.log(error.message)); |
Replace the client id
, client secret
and access token
as described in the video
Node.js Express App
npm init -y
npm i nodemailer express multer