Hi I know there is a post about this already, but I couldn’t find any definitive answers. I was hoping if I shared my code someone could help. I followed @Logan Eldridge ( https://www.wix.com/corvid/forum/community-discussion/connecting-google-email-service-to-website-troubles )and @plomteuxquentin ( https://www.wix.com/corvid/forum/community-discussion/javascript-gmail-api )
emailSer
vice.jsw
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
);
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
export const auth2 = {
clientId: " -my actual client id- ",
clientSecret: " -my actual secret key- “,
refreshToken: " -my regreshToken from oauthplayground is here -”,
playground: “OAuth 2.0 Playground”
};
export const emailService = {
transportOptions:{
service: “gmail”,
auth: {
type: “OAuth2”,
user: " -my actual email address- ",
clientId: null ,
clientSecret: null ,
refreshToken: null ,
accessToken: null
}
},
emailSender: {
name: ’ -my name used on gmail account- ',
address: ’ -my-Email address: same as in User above- ’
}
}
page code
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import {sendApprovedAddress} from ‘backend/emailService.jsw’;
$w.onReady( function () {
});
export function button22_click(event) {
//Add your code for this event here:
sendApprovedAddress(" -i’ve tried different emails here- ",“hello”,“this is a test”);
console.log(“hello”);
}
These are there errors i've seen.
Microsoft Edge:
Unhandled promise rejection TypeError: Cannot read property 'sendMail' of undefined
wix console in preview:
emailService.jsw
Line 39
(node:1) [google-auth-library:DEP007] DeprecationWarning: The refreshAccessToken
method has been deprecated, and will be removed in the 3.0 release of google-auth-library. Please use the getRequestHeaders
method instead.
could not send email to
{…}
emailService.jsw
Line 45
Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 Check Gmail through other email platforms - Gmail Help q13sm3793401pjc.4 - gsmtp
Google Chrome:
Please help me figure this out. I know the authors of the other posts seem to have figured it out but I guess I just don’t understand.