Below are the steps
1. Google Cloud Platform Setup
Go to Google Cloud and create a new project.
Search for “APIs & Services”
Click on “Credentials” > Click “+ Create credentials” > “OAuth client ID”
Type: Web Application
Name: “Enter Your Name of Client”
Authorized redirect URIs: https://developers.google.com/oauthplayground
Copy both the Client ID and Client Secret in a note.
2. Google OAuth Playground
Go to Oauth Playground > Click on Setting icon on the right > Enable Use your own Oauth credentials > Enter Oauth Client ID & Oatuh Client Secret that you get from the above step > Close
In Select & Authorize APIs, Type https://mail.google.com > Authorize APIs > Login with the account that you want to send from.
Click Exchange authorization code for tokens > Copy Refresh Token
Now, you should have 3 things in your note:
Client ID
Client Secret
Oauth2 Refresh Token
Important: No need to configure Gmail account
While many tutorials told you to turn ON “Less secure app access” to allow Nodemailer to have access to send emails from your account.
Now below is the coding part
In this example we are going to use aws lambda function to test our function but it shouldn't be different from any other use case except the way you are going to call your function.
The first thing to do is create a NPM project, So go to ur terminal and run :
npm init
Now we need to install nodemailer :
npm install nodemailer
Create a Javascript file let's call it index.js and copy and paste this code :
var nodemailer = require('nodemailer');
exports.handler = (event, context, callback) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: 'mail@gmail.com',
clientId: '****************************.apps.googleusercontent.com',
clientSecret: '*************************',
refreshToken: '***************************************************************************************',
accessToken: '**************************************************************************************************',
expires: 3599
}});
console.log(`Processing event ${JSON.stringify(event)}`);
// create reusable transporter object using the default SMTP transport
const requestBody = JSON.parse(event.body);
console.log('sending from ', process.env.MAILUSER);
console.log('message sender : ', JSON.stringify(requestBody.email));
// send mail to me with the message info
let info1 = transporter.sendMail({
from: `sender@gmail.com`, // sender address
to: "receiver@gmail.com", // list of receivers
subject: "Insert a Subject here", // Subject line
text: `message text here`, // plain text body
html: `<p>Html text message here </p>` // html body
});
var responseBody = {
"result":"message sent successfully"
};
var response = {
"statusCode": 200,
"headers": {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST'
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
callback(null, response);
}
reference:
http://blog.bessam.engineer/How-to-use-nodemailer-with-GMail-and-OAuth
No comments:
Post a Comment