Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/.example.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ DB_USER=
DB_PASSWORD=
DB_PORT=

HOST='http://localhost:3001'
HOST='http://localhost:3001'

CREDENTIALS=
TOKEN=
102 changes: 102 additions & 0 deletions backend/app/services/emailSender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const fs = require('fs').promises;
const path = require('path');
const process = require('process');
const {authenticate} = require('@google-cloud/local-auth');
const {google} = require('googleapis');
var messagedata = require('./messagedata.json');
const dotenv = require('dotenv');
dotenv.config({ path: '../../.env'});
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly','https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.send',];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.

const gmail = google.gmail('v1');
/**
* Reads previously authorized credentials from the save file.
*
* @return {Promise<OAuth2Client|null>}
*/
async function loadSavedCredentialsIfExist() {
try {
const credentials = JSON.parse(process.env.TOKEN);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}
/**
* Serializes credentials to a file comptible with GoogleAUth.fromJSON.
*
* @param {OAuth2Client} client
* @return {Promise<void>}
*/
async function saveCredentials(client) {
const keys = JSON.parse(process.env.CREDENTIALS);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}

async function authorize() {
let client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
client = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client);
}
return client;
}
async function sendMail(auth) {
google.options({auth});
const gmail = google.gmail({version: 'v1', auth});
const messageParts = [
`From: ${messagedata.from} <${messagedata.fromMail}>`,
`To: ${messagedata.to} <${messagedata.toMail}>`,
'Content-Type: text/html; charset=utf-8',
'MIME-Version: 1.0',
`Subject: ${messagedata.subject}`,
'',
'Testing Testing\n',
'<b>Testing !</b>',
];
const message = messageParts.join('\n');

// The body needs to be base64url encoded.
const encodedMessage = Buffer.from(message)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');

const res = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedMessage,
},
});
console.log(res.data);
return res.data;
}
/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/

authorize().then(sendMail).catch(console.error);
module.exports = sendMail;
7 changes: 7 additions & 0 deletions backend/app/services/messagedata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"from":"From",
"fromMail":"From@gmail.com",
"to" : "Toaddress",
"toMail":"Toaddress@gmail.com",
"subject" : "Testingggg"
}
Loading