# Javascript Gmail API

**URL:** <https://forum.wixstudio.com/t/javascript-gmail-api/33190>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [December 29, 2017, 4:55pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190 "2017-12-29T16:55:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![luigino](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/luigino/32/802_2.png) [@luigino](https://forum.wixstudio.com/u/luigino)\
**Post date:** [December 29, 2017, 4:55pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/1 "2017-12-29T16:55:56Z")

</div>

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

Take a look:

> **[Sending Emails with the Gmail JavaScript API — SitePoint](https://www.sitepoint.com/sending-emails-gmail-javascript-api/)**
>
> Jamie Shields shows how to use the Gmail JavaScript API to send email, and in so doing creates a customizable JavaScript app to help you manage your inbox.

> <https://stackoverflow.com/questions/36946490/sending-email-from-my-javascript-app-via-gmail-api-mail-appears-in-gmail-sent>

Best,  
-Luigi

---

<div class="post-metadata">

**Author:** ![encoso-ltd](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/encoso-ltd/32/2554_2.png) [@encoso-ltd](https://forum.wixstudio.com/u/encoso-ltd)\
**Post date:** [March 21, 2018, 12:56pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/2 "2018-03-21T12:56:01Z")

</div>

Hi,  
Any success with this?

Currently using sendGrid and its not playing nice.

P

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [March 24, 2018, 7:12pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/3 "2018-03-24T19:12:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mauro-vento-avyno](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mauro-vento-avyno/32/2432_2.png) [@mauro-vento-avyno](https://forum.wixstudio.com/u/mauro-vento-avyno)\
**Post date:** [April 3, 2018, 9:26am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/4 "2018-04-03T09:26:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![code-queen](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-queen/32/696_2.png) [@code-queen](https://forum.wixstudio.com/u/code-queen)\
**Post date:** [August 29, 2018, 3:32pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/5 "2018-08-29T15:32:41Z")

</div>

Nothing yet ☹

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.

☹

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [August 29, 2018, 7:56pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/6 "2018-08-29T19:56:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![plomteuxquentin](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/plomteuxquentin/32/9345_2.png) [@plomteuxquentin](https://forum.wixstudio.com/u/plomteuxquentin)\
**Post date:** [May 20, 2019, 2:18am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/7 "2019-05-20T02:18:06Z")

</div>

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](https://medium.com/@nickroach_50526/sending-emails-with-node-js-using-smtp-gmail-and-oauth2-316fe9c790a1) . It simply uses npm package [nodemailer](https://nodemailer.com/about/) 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**

```javascript
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)

```javascript
 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 🙂

---

<div class="post-metadata">

**Author:** ![mauro-vento-avyno](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mauro-vento-avyno/32/2432_2.png) [@mauro-vento-avyno](https://forum.wixstudio.com/u/mauro-vento-avyno)\
**Post date:** [May 20, 2019, 5:08am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/8 "2019-05-20T05:08:37Z")

</div>

:clap::clap:

---

<div class="post-metadata">

**Author:** ![logan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/logan/32/13253_2.png) [@logan](https://forum.wixstudio.com/u/logan)\
**Post date:** [July 10, 2019, 9:07am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/9 "2019-07-10T09:07:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![plomteuxquentin](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/plomteuxquentin/32/9345_2.png) [@plomteuxquentin](https://forum.wixstudio.com/u/plomteuxquentin)\
**Post date:** [July 10, 2019, 11:01am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/10 "2019-07-10T11:01:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![logan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/logan/32/13253_2.png) [@logan](https://forum.wixstudio.com/u/logan)\
**Post date:** [July 10, 2019, 7:00pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/11 "2019-07-10T19:00:12Z")

</div>

@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](https://www.wix.com/corvid/forum/community-discussion/connecting-google-email-serivce-to-website-troubles)

Thank you so much for your post and help!

---

<div class="post-metadata">

**Author:** ![prashanthbhaskaran](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/prashanthbhaskaran/32/10176_2.png) [@prashanthbhaskaran](https://forum.wixstudio.com/u/prashanthbhaskaran)\
**Post date:** [August 5, 2019, 3:47am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/12 "2019-08-05T03:47:19Z")

</div>

Thanks, that worked !!

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [August 20, 2019, 6:48pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/13 "2019-08-20T18:48:14Z")

</div>

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

```javascript
 (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?

---

<div class="post-metadata">

**Author:** ![plomteuxquentin](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/plomteuxquentin/32/9345_2.png) [@plomteuxquentin](https://forum.wixstudio.com/u/plomteuxquentin)\
**Post date:** [August 21, 2019, 12:59am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/14 "2019-08-21T00:59:42Z")

</div>

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

I’ll update the code and notify you once updated

---

<div class="post-metadata">

**Author:** ![logan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/logan/32/13253_2.png) [@logan](https://forum.wixstudio.com/u/logan)\
**Post date:** [August 22, 2019, 8:33pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/15 "2019-08-22T20:33:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![logan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/logan/32/13253_2.png) [@logan](https://forum.wixstudio.com/u/logan)\
**Post date:** [September 25, 2019, 2:29am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/16 "2019-09-25T02:29:19Z")

</div>

@plomteuxquentin any updates on this yet?

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [September 25, 2019, 6:24am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/17 "2019-09-25T06:24:20Z")

</div>

You can use the following instead:

```javascript
const accessToken = oAuth2Client.getAccessToken();

```

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [September 25, 2019, 9:11pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/18 "2019-09-25T21:11:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [September 26, 2019, 4:36am UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/19 "2019-09-26T04:36:11Z")

</div>

@giri-zano This is what I did to update the [Save Data to a Google Sheet Using NPM](https://www.wix.com/code/home/forum/wix-tips-and-updates/example-google-sheets-npm) example which had the same issue.

---

<div class="post-metadata">

**Author:** ![bengenhes](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bengenhes/32/10446_2.png) [@bengenhes](https://forum.wixstudio.com/u/bengenhes)\
**Post date:** [February 1, 2020, 3:34pm UTC](https://forum.wixstudio.com/t/javascript-gmail-api/33190/20 "2020-02-01T15:34:01Z")

</div>

Hi Quentin,  
I have a problem regarding the use of Gmail PAI for sending emails. This [link](https://www.wix.com/corvid/forum/community-discussion/how-to-use-gmail-api-for-sending-email) 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](https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission) 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.

[Next page](https://forum.wixstudio.com/t/javascript-gmail-api/33190.md?page=2)
