# Gmail Messages

Setting it up takes two steps: authorize the Gmail account, then write the queries that pick the messages. The connector stores what it finds in four tables: queries, messages, headers and parts.

## Authorization
[Create a new configuration](https://help.keboola.com/components/configurations/#create-a-configuration) of the **Gmail Messages** connector.
Then click **Authorize Account** to [authorize the configuration](https://help.keboola.com/components/authorization/). 
**Your inbox is accessed as read only.**

![Gmail - authorize account](https://help.keboola.com/components/extractors/communication/gmail/gmail-1.png)

Reading email messages is considered very sensitive by Google. This means that authorization may often be rejected and 
the app may show as blocked or not-verified. In such a case, we recommend that you use Custom OAuth Credentials. Generally using
Custom OAuth credentials is the preferred authorization option, although it is more complicated.

### Custom OAuth Credentials

1. Visit the [Google API Console](https://console.developers.google.com/).
2. Select an existing project or create a new one. Click **Enable APIs and Services**.
 
![Screenshot - Google API Console - Project](https://help.keboola.com/components/extractors/communication/gmail/google_console_enable.png)

3. Select the [**Gmail API**](https://console.cloud.google.com/apis/library/gmail.googleapis.com) and **Enable** it. You should 
then see it in the list of enabled APIs.
 
![Screenshot - Google API Console - API list](https://help.keboola.com/components/extractors/communication/gmail/google_console_apis.png)
    
4. Select the **Credentials** section from the menu on the left, click the **Create Credentials** button, and select **OAuth client ID**.
  
![Screenshot - Google API Console - Create Credentials](https://help.keboola.com/components/extractors/communication/gmail/google_console_credentials.png)
    
5. Choose **Web Application**. Into **Authorized redirect URIs** insert:
    - `https://oauth.keboola.com/authorize/keboola.ex-gmail/callback`
    - or `https://oauth.eu-central-1.keboola.com/authorize/keboola.ex-gmail/callback`
    - or `https://oauth.north-europe.azure.keboola.com/authorize/keboola.ex-gmail/callback`

    Depending on which stack you are using or planning to use.

![Screenshot - Google API Console - Fill Credentials](https://help.keboola.com/components/extractors/communication/gmail/google_console_detail.png)

6. Click **Create** and a pop-up window will display your new client ID and client secret credentials.
7. You can now use these credentials in the **Custom Authorization** tab when authorizing the Gmail Messages connector.
 
![Screenshot - Custom Authorization](https://help.keboola.com/components/extractors/communication/gmail/custom-credentials.png)

## Configuration
Fill in the form to fit your needs.

![Gmail - configure queries](https://help.keboola.com/components/extractors/communication/gmail/gmail-2.png)

- **Query** -- Query to filter your messages. To speed up the extraction, be as specific as possible. 
For more detailed information about querying, follow Google's [Advanced Search](https://support.google.com/mail/answer/7190?hl=en) help site.
- **Headers** (optional) -- Headers you want to download. If no headers are specified, all headers will be downloaded.

Don't forget to **Save** the configuration.

## Produced Tables
Data are always imported incrementally. The data source connector produces several tables that can be joined together.

### Queries
Queries and their messages; it is good to know which query a message came from.

| query | messageId |
| --- | --- |
| `from:some.address@example.com` | `9876cbd54bd215a6` |
| `from:another.address@example.com` | `1234abcd2ffdc1d6` |

### Messages
A base table of messages:

| id | threadId |
| --- | --- |
| `9876cbd54bd215a6` | `1234abcd2ffdc1d6` |
| `1234abcd2ffdc1d6` | `1234abcd2ffdc1d6` |

*Tip: You can group your messages to conversations with `GROUP BY threadId`*.

### Headers
All downloaded headers:

| messageId | name | value |
| --- | --- | --- |
| `1234abcd2ffdc1d6` | `From` | `News <some.address@example.com>` |
| `1234abcd2ffdc1d6` | `Subject` | `Trending News` |

### Parts
All downloaded message parts:

| messageId | partId | mimeType | bodySize | bodyData |
| --- | --- | --- | --- | --- |
| `1234abcd2ffdc1d6` | `0` | `text/plain` | `26` | `Lorem ipsum dolor sit amet` |
| `1234abcd2ffdc1d6` | `1` | `text/html` | `33` | `<p>Lorem ipsum dolor sit amet</p>` |

*Note: Only parts with `text/plain` and `text/html` mime types are downloaded.*
