I am attempting to set up a email from my gmail account on a website for once an order is placed. However, I still havnt been able to send a test email to one of my other emails.
Below is my code for the two backend files and the page code.
I am basing my code on a post by @Quentin Plomteux at this forum: https://www.wix.com/corvid/forum/community-discussion/javascript-gmail-api
I saw a few TYPOS in his code as well (I believed them to be typos) so those are commented at changed from his in my code below. Any suggestions or pointers would be great. I’m pretty sure I have the google cloud service platform set up correctly based on the tutorial Quentin included in his post.
The things I have blackout in the code are my information, as I assumed was supposed to be changed for my mailing service. Sorry I did not use the code containers; They messed up the code format for some reason.
emailService.jsw (in backend)
import nodemailer from ‘nodemailer’;
import { google } from “googleapis”;
import _ from ‘lodash’;
import {emailService as CONFIG_SERVICE} from ‘backend/config.js’;
import {auth2 as CONFIG_AUTH2} from ‘backend/config.js’;
//init OAuth2
const OAuth2 = google.auth.OAuth2;
const oauth2Client = new OAuth2(
CONFIG_AUTH2.clientId,
CONFIG_AUTH2.clientSecret,
CONFIG_AUTH2.playground
//In Quentin Plomteux origional post, this was spelled as “playgroud”
);
oauth2Client.setCredentials({
refresh_token: CONFIG_AUTH2.refreshToken
});
// initialize the service by setting up the transport options (with fresh access token);
let initService = oauth2Client.refreshAccessToken().then(tokens => {
const transportOptions = _.cloneDeep(CONFIG_SERVICE.transportOptions);
transportOptions.auth.clientId = CONFIG_AUTH2.clientId;
transportOptions.auth.clientSecret = CONFIG_AUTH2.clientSecret;
transportOptions.auth.refreshToken = CONFIG_AUTH2.refreshToken;
transportOptions.auth.accessToken = tokens.credentials.access_token;
return nodemailer.createTransport(transportOptions);
}). catch (reason => {console.error(‘refresh token error’, reason)});
export function sendApprovedAddress(to, subject, text) {
const data = { from : CONFIG_SERVICE.sender, to, subject,text};
return initService.then(transporter => {
return transporter.sendMail(data);
}).then((responde) => {
console.info('email successfully sent to ', to, responde);
return responde;
}). catch (reason => {
console.error('could not send email to ’ + to, reason);
throw reason;
});
}
config.js (in backend)
export const auth2 = {
clientId: “███████My API ID█████████.apps.googleusercontent.com”,
clientSecret: “███████My Client Secret██████████”,
refreshToken: “███████████ My Token ███████████████████████”,
playground: “https://developers.google.com/oauthplayground”
//In Quentin Plomteux origional post, this has a weird duplication issue.
};
export const emailService = {
transportOptions:{
service: “gmail”,
auth: {
type: “OAuth2”,
user: “█████My Email Address @ My company Gsuite address████”,
clientId: null ,
clientSecret: null ,
refreshToken: null ,
accessToken: null
}
},
emailSender: {
name: ‘█████ My Name ████’,
address: ‘█████My Email Address @ My company Gsuite address████’
}
}
Page Code (only a button action to test)
import {sendApprovedAddress} from ‘backend/emailService.jsw’;
$w.onReady( function () {
});
export function mail_click(event) {
sendApprovedAddress(“██ My test recipient email address ██”,“hello”,“this is a test”);
console.log(“hello”);
}
Any suggestions of error fixes would help tremendously!
Thank you so much everyone for your time and help, and especially thank you to @Quentin Plomteux.