Javascript Gmail API

Hi Everyone,
I found great tutorials about how to send emails with GMAIL API from Javascript.

Take a look:

Best,
-Luigi

2 Likes

Hi,
Any success with this?

Currently using sendGrid and its not playing nice.

P

Luigi, I managed to send emails thru gmail. Look here: https://girizano.wixsite.com/codecorner/home/

Hi, nobody could illustrate as send an email directly via oAuth authorization on Gmail avoiding 3d party solutions like SendGrid, EmailJsā€¦ ?
thanks in advance

Nothing yet :frowning:

Cannot find a solution to avoid using 3rd party in any of Google APIā€™s that I want to use.

SendGrid allows 100 free emails per day.
EmailJS only 200 per month (?!)
Zappier ā€¦ I donā€™t even want to think about it.

:frowning:

There is always the Gmail API that you can use, directly from Wix Code. YouĀ“ll just have to develop one. But itĀ“s not that easy. Have you found the documentation?

You asked for it, I did it!

Iā€™m reviving an old post here but someone else might need it!

I created a backend service based on this tutorial . It simply uses npm package nodemailer to send email via an smtp service. In this case it use Gmail to send free email but It could be used with any other smtp service!

Like if it helped!

emailService.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.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

donā€™t forget to upadte the auth2 credentials (explanations in tutorial)

 export const auth2 = {
    clientId: "client id",
    clientSecret: "client secret",
    refreshToken: "refresh token",
    playground: "https://developers.google.com/oauthplayground" 
};

export const emailService =  {
    transportOptions:{
        service: "gmail",
        auth: {
            type: "OAuth2",
            user: "USER TO CHANGE", 
            clientId:  null,
            clientSecret: null,
            refreshToken: null,
            accessToken: null
        }
    },
    emailSender: {
        name: 'Quentin from Leg me up',
        address: 'quentin@legmeup.be'
    }
}

Let me know if you have any suggestion or question :slight_smile:

:clap::clap:

Hi Quentin,
Iā€™m having some trouble. I used your code example from above, and followed the steps in the tutorial for setting up the cloud platform api, however, I linked the code to send from a button click. and I havnā€™t been able to send an email to myself. Any suggestions?

Hi @logan , Could you create another topic with your code in it so we donā€™t ā€œpolluteā€ this one? Also do you have any error in the logs?

@plomteuxquentin
Hi Quentin, I have created a new thread and deleted my code from here.
My Thread Link is: https://www.wix.com/corvid/forum/community-discussion/connecting-google-email-serivce-to-website-troubles

Thank you so much for your post and help!

Thanks, that worked !!

Works very well. I just found this in the browser console:

 (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. 

Looks like we will have to change it very soon. Have you done that already?

Hi Giri, I was not aware of the issue. Thx for sharing.

Iā€™ll update the code and notify you once updated

Damn, Just when I thought after getting it setup I wouldnā€™t have to do anything. Good catch though. Hopefully the new method is not difficult to implement.

@plomteuxquentin any updates on this yet?

You can use the following instead:

const accessToken = oAuth2Client.getAccessToken();

@yisrael-wix Did that work for you? I tried that before I wrote to Quentin (because I found that on their Forum and on Stackoverflow), but then I must have made some mistake, because I couldnĀ“t get it working.

@giri-zano This is what I did to update the Save Data to a Google Sheet Using NPM example which had the same issue.

Hi Quentin,
I have a problem regarding the use of Gmail PAI for sending emails. This link is the summery of what I have done till now. Since I am a newbie who is attempting a DIY, unfortunately, I didnā€™t get much of what Yisrael and GOS referred me to. My problem is that all things are very fragmented and there are many ā€œtutorialsā€, which are not directly about sending an email using the Wix platform.
I was very happy when I saw your post but still have some questions. In your post, it seems that you have made two files, ā€˜emailService.jswā€™ and ā€˜Config.jsā€™ that are enough for solving the problem. Are they equivalent of the two files ā€˜email.jswā€™ and ā€˜sendGrid.jsā€™ in this tutorial? Are your files the only ones we need for configuring all things? How about ā€˜in-page code?
I just need a blueprint, which gives components, i.e. files, and structures, i.e. a road-map.
I will be grateful if you help me go through this confusion.