2020-08-18 03:40:19 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
2020-10-01 05:01:39 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-08-18 03:40:19 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
encodeEmail,
|
|
|
|
extractEmail,
|
|
|
|
googleApiRequest,
|
|
|
|
googleApiRequestAllItems,
|
|
|
|
parseRawEmail,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
messageFields,
|
2020-10-01 05:01:39 -07:00
|
|
|
messageOperations,
|
2020-08-18 03:40:19 -07:00
|
|
|
} from './MessageDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
messageLabelFields,
|
2020-10-01 05:01:39 -07:00
|
|
|
messageLabelOperations,
|
2020-08-18 03:40:19 -07:00
|
|
|
} from './MessageLabelDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
labelFields,
|
2020-10-01 05:01:39 -07:00
|
|
|
labelOperations,
|
2020-08-18 03:40:19 -07:00
|
|
|
} from './LabelDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
draftFields,
|
2020-10-01 05:01:39 -07:00
|
|
|
draftOperations,
|
2020-08-18 03:40:19 -07:00
|
|
|
} from './DraftDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
isEmpty,
|
|
|
|
} from 'lodash';
|
|
|
|
|
|
|
|
export interface IEmail {
|
2021-07-30 07:26:57 -07:00
|
|
|
from?: string;
|
2020-08-18 03:40:19 -07:00
|
|
|
to?: string;
|
|
|
|
cc?: string;
|
|
|
|
bcc?: string;
|
|
|
|
inReplyTo?: string;
|
|
|
|
reference?: string;
|
|
|
|
subject: string;
|
|
|
|
body: string;
|
2021-01-07 02:58:28 -08:00
|
|
|
htmlBody?: string;
|
2020-08-18 03:40:19 -07:00
|
|
|
attachments?: IDataObject[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IAttachments {
|
|
|
|
type: string;
|
|
|
|
name: string;
|
|
|
|
content: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Gmail implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Gmail',
|
|
|
|
name: 'gmail',
|
2021-02-28 08:27:52 -08:00
|
|
|
icon: 'file:gmail.svg',
|
2020-08-18 03:40:19 -07:00
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Gmail API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Gmail',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
2021-01-10 11:49:47 -08:00
|
|
|
{
|
|
|
|
name: 'googleApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'serviceAccount',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-18 03:40:19 -07:00
|
|
|
{
|
|
|
|
name: 'gmailOAuth2',
|
|
|
|
required: true,
|
2021-01-10 11:49:47 -08:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'oAuth2',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2020-08-18 03:40:19 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
2021-01-10 11:49:47 -08:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Service Account',
|
|
|
|
value: 'serviceAccount',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'oAuth2',
|
|
|
|
},
|
2020-08-18 03:40:19 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Draft',
|
|
|
|
value: 'draft',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Label',
|
|
|
|
value: 'label',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Message',
|
|
|
|
value: 'message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Message Label',
|
|
|
|
value: 'messageLabel',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'draft',
|
|
|
|
description: 'The resource to operate on.',
|
|
|
|
},
|
|
|
|
//-------------------------------
|
|
|
|
// Draft Operations
|
|
|
|
//-------------------------------
|
|
|
|
...draftOperations,
|
|
|
|
...draftFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Label Operations
|
|
|
|
//-------------------------------
|
|
|
|
...labelOperations,
|
|
|
|
...labelFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Message Operations
|
|
|
|
//-------------------------------
|
|
|
|
...messageOperations,
|
|
|
|
...messageFields,
|
|
|
|
//-------------------------------
|
|
|
|
// MessageLabel Operations
|
|
|
|
//-------------------------------
|
|
|
|
...messageLabelOperations,
|
|
|
|
...messageLabelFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the labels to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getLabels(
|
2020-10-22 09:00:28 -07:00
|
|
|
this: ILoadOptionsFunctions,
|
2020-08-18 03:40:19 -07:00
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const labels = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'labels',
|
|
|
|
'GET',
|
2020-10-22 09:00:28 -07:00
|
|
|
'/gmail/v1/users/me/labels',
|
2020-08-18 03:40:19 -07:00
|
|
|
);
|
|
|
|
for (const label of labels) {
|
|
|
|
const labelName = label.name;
|
|
|
|
const labelId = label.id;
|
|
|
|
returnData.push({
|
|
|
|
name: labelName,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: labelId,
|
2020-08-18 03:40:19 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-08-18 03:40:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
|
|
|
|
let method = '';
|
|
|
|
let body: IDataObject = {};
|
|
|
|
let qs: IDataObject = {};
|
|
|
|
let endpoint = '';
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'label') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/labels/create
|
|
|
|
const labelName = this.getNodeParameter('name', i) as string;
|
|
|
|
const labelListVisibility = this.getNodeParameter('labelListVisibility', i) as string;
|
|
|
|
const messageListVisibility = this.getNodeParameter('messageListVisibility', i) as string;
|
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = '/gmail/v1/users/me/labels';
|
|
|
|
|
|
|
|
body = {
|
|
|
|
labelListVisibility,
|
|
|
|
messageListVisibility,
|
|
|
|
name: labelName,
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/labels/delete
|
|
|
|
const labelId = this.getNodeParameter('labelId', i) as string[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
method = 'DELETE';
|
|
|
|
endpoint = `/gmail/v1/users/me/labels/${labelId}`;
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
responseData = { success: true };
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/labels/get
|
|
|
|
const labelId = this.getNodeParameter('labelId', i);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
method = 'GET';
|
|
|
|
endpoint = `/gmail/v1/users/me/labels/${labelId}`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/gmail/v1/users/me/labels`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.labels;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
|
|
|
const limit = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'messageLabel') {
|
|
|
|
if (operation === 'remove') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/messages/modify
|
|
|
|
const messageID = this.getNodeParameter('messageId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${messageID}/modify`;
|
|
|
|
body = {
|
|
|
|
removeLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'add') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/modify
|
|
|
|
const messageID = this.getNodeParameter('messageId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${messageID}/modify`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = {
|
|
|
|
addLabelIds: labelIds,
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'message') {
|
|
|
|
if (operation === 'send') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/send
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let toStr = '';
|
|
|
|
let ccStr = '';
|
|
|
|
let bccStr = '';
|
|
|
|
let attachmentsList: IDataObject[] = [];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const toList = this.getNodeParameter('toList', i) as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
toList.forEach((email) => {
|
|
|
|
toStr += `<${email}>, `;
|
2020-08-18 03:40:19 -07:00
|
|
|
});
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.ccList) {
|
|
|
|
const ccList = additionalFields.ccList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
ccList.forEach((email) => {
|
|
|
|
ccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.bccList) {
|
|
|
|
const bccList = additionalFields.bccList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
bccList.forEach((email) => {
|
|
|
|
bccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.attachmentsUi) {
|
|
|
|
const attachmentsUi = additionalFields.attachmentsUi as IDataObject;
|
|
|
|
const attachmentsBinary = [];
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
|
|
|
if (attachmentsUi.hasOwnProperty('attachmentsBinary')
|
|
|
|
&& !isEmpty(attachmentsUi.attachmentsBinary)
|
|
|
|
&& items[i].binary) {
|
|
|
|
// @ts-ignore
|
|
|
|
for (const { property } of attachmentsUi.attachmentsBinary as IDataObject[]) {
|
|
|
|
for (const binaryProperty of (property as string).split(',')) {
|
|
|
|
if (items[i].binary![binaryProperty] !== undefined) {
|
|
|
|
const binaryData = items[i].binary![binaryProperty];
|
|
|
|
attachmentsBinary.push({
|
|
|
|
name: binaryData.fileName || 'unknown',
|
|
|
|
content: binaryData.data,
|
|
|
|
type: binaryData.mimeType,
|
|
|
|
});
|
|
|
|
}
|
2021-04-30 12:44:46 -07:00
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-04-30 12:44:46 -07:00
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
attachmentsList = attachmentsBinary;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const email: IEmail = {
|
2021-07-30 07:26:57 -07:00
|
|
|
from: additionalFields.senderName as string || '',
|
2021-07-19 23:58:54 -07:00
|
|
|
to: toStr,
|
|
|
|
cc: ccStr,
|
|
|
|
bcc: bccStr,
|
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
|
|
|
body: this.getNodeParameter('message', i) as string,
|
|
|
|
attachments: attachmentsList,
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (this.getNodeParameter('includeHtml', i, false) as boolean === true) {
|
|
|
|
email.htmlBody = this.getNodeParameter('htmlMessage', i) as string;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = '/gmail/v1/users/me/messages/send';
|
|
|
|
method = 'POST';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'reply') {
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = this.getNodeParameter('messageId', i) as string;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let toStr = '';
|
|
|
|
let ccStr = '';
|
|
|
|
let bccStr = '';
|
|
|
|
let attachmentsList: IDataObject[] = [];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const toList = this.getNodeParameter('toList', i) as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
toList.forEach((email) => {
|
|
|
|
toStr += `<${email}>, `;
|
2020-08-18 03:40:19 -07:00
|
|
|
});
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.ccList) {
|
|
|
|
const ccList = additionalFields.ccList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
ccList.forEach((email) => {
|
|
|
|
ccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.bccList) {
|
|
|
|
const bccList = additionalFields.bccList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
bccList.forEach((email) => {
|
|
|
|
bccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.attachmentsUi) {
|
|
|
|
const attachmentsUi = additionalFields.attachmentsUi as IDataObject;
|
|
|
|
const attachmentsBinary = [];
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
|
|
|
if (attachmentsUi.hasOwnProperty('attachmentsBinary')
|
|
|
|
&& !isEmpty(attachmentsUi.attachmentsBinary)
|
|
|
|
&& items[i].binary) {
|
|
|
|
// @ts-ignore
|
|
|
|
for (const { property } of attachmentsUi.attachmentsBinary as IDataObject[]) {
|
|
|
|
for (const binaryProperty of (property as string).split(',')) {
|
|
|
|
if (items[i].binary![binaryProperty] !== undefined) {
|
|
|
|
const binaryData = items[i].binary![binaryProperty];
|
|
|
|
attachmentsBinary.push({
|
|
|
|
name: binaryData.fileName || 'unknown',
|
|
|
|
content: binaryData.data,
|
|
|
|
type: binaryData.mimeType,
|
|
|
|
});
|
|
|
|
}
|
2021-04-30 12:44:46 -07:00
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-04-30 12:44:46 -07:00
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
attachmentsList = attachmentsBinary;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// if no recipient is defined then grab the one who sent the email
|
|
|
|
if (toStr === '') {
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${id}`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.format = 'metadata';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const { payload } = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const header of payload.headers as IDataObject[]) {
|
|
|
|
if (header.name === 'From') {
|
|
|
|
toStr = `<${extractEmail(header.value as string)}>,`;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const email: IEmail = {
|
2021-07-30 07:26:57 -07:00
|
|
|
from: additionalFields.senderName as string || '',
|
2021-07-19 23:58:54 -07:00
|
|
|
to: toStr,
|
|
|
|
cc: ccStr,
|
|
|
|
bcc: bccStr,
|
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
|
|
|
body: this.getNodeParameter('message', i) as string,
|
|
|
|
attachments: attachmentsList,
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (this.getNodeParameter('includeHtml', i, false) as boolean === true) {
|
|
|
|
email.htmlBody = this.getNodeParameter('htmlMessage', i) as string;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = '/gmail/v1/users/me/messages/send';
|
|
|
|
method = 'POST';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
email.inReplyTo = id;
|
|
|
|
email.reference = id;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
threadId: this.getNodeParameter('threadId', i) as string,
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/messages/get
|
|
|
|
method = 'GET';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const id = this.getNodeParameter('messageId', i);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const format = additionalFields.format || 'resolved';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (format === 'resolved') {
|
|
|
|
qs.format = 'raw';
|
2020-08-18 03:40:19 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.format = format;
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = `/gmail/v1/users/me/messages/${id}`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let nodeExecutionData: INodeExecutionData;
|
2020-08-18 03:40:19 -07:00
|
|
|
if (format === 'resolved') {
|
2021-07-19 23:58:54 -07:00
|
|
|
const dataPropertyNameDownload = additionalFields.dataPropertyAttachmentsPrefixName as string || 'attachment_';
|
|
|
|
|
|
|
|
nodeExecutionData = await parseRawEmail.call(this, responseData, dataPropertyNameDownload);
|
2020-08-18 03:40:19 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
nodeExecutionData = {
|
|
|
|
json: responseData,
|
|
|
|
};
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = nodeExecutionData;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
if (qs.labelIds) {
|
|
|
|
// tslint:disable-next-line: triple-equals
|
|
|
|
if (qs.labelIds == '') {
|
|
|
|
delete qs.labelIds;
|
|
|
|
} else {
|
|
|
|
qs.labelIds = qs.labelIds as string[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
2020-08-18 03:40:19 -07:00
|
|
|
this,
|
2021-07-19 23:58:54 -07:00
|
|
|
'messages',
|
2020-08-18 03:40:19 -07:00
|
|
|
'GET',
|
2021-07-19 23:58:54 -07:00
|
|
|
`/gmail/v1/users/me/messages`,
|
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-08-18 03:40:19 -07:00
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
qs.maxResults = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/gmail/v1/users/me/messages`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
responseData = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const format = additionalFields.format || 'resolved';
|
|
|
|
|
|
|
|
if (format !== 'ids') {
|
2020-08-18 03:40:19 -07:00
|
|
|
|
|
|
|
if (format === 'resolved') {
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < responseData.length; i++) {
|
|
|
|
responseData[i] = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/gmail/v1/users/me/messages/${responseData[i].id}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-10-15 01:41:47 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (format === 'resolved') {
|
|
|
|
const dataPropertyNameDownload = additionalFields.dataPropertyAttachmentsPrefixName as string || 'attachment_';
|
|
|
|
|
|
|
|
responseData[i] = await parseRawEmail.call(this, responseData[i], dataPropertyNameDownload);
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (format !== 'resolved') {
|
|
|
|
responseData = this.helpers.returnJsonArray(responseData);
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/delete
|
|
|
|
method = 'DELETE';
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = `/gmail/v1/users/me/messages/${id}`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'draft') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/create
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let toStr = '';
|
|
|
|
let ccStr = '';
|
|
|
|
let bccStr = '';
|
|
|
|
let attachmentsList: IDataObject[] = [];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.toList) {
|
|
|
|
const toList = additionalFields.toList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
toList.forEach((email) => {
|
|
|
|
toStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.ccList) {
|
|
|
|
const ccList = additionalFields.ccList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
ccList.forEach((email) => {
|
|
|
|
ccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.bccList) {
|
|
|
|
const bccList = additionalFields.bccList as IDataObject[];
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
bccList.forEach((email) => {
|
|
|
|
bccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.attachmentsUi) {
|
|
|
|
const attachmentsUi = additionalFields.attachmentsUi as IDataObject;
|
|
|
|
const attachmentsBinary = [];
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
|
|
|
if (attachmentsUi.hasOwnProperty('attachmentsBinary')
|
|
|
|
&& !isEmpty(attachmentsUi.attachmentsBinary)
|
|
|
|
&& items[i].binary) {
|
|
|
|
for (const { property } of attachmentsUi.attachmentsBinary as IDataObject[]) {
|
|
|
|
for (const binaryProperty of (property as string).split(',')) {
|
|
|
|
if (items[i].binary![binaryProperty] !== undefined) {
|
|
|
|
const binaryData = items[i].binary![binaryProperty];
|
|
|
|
attachmentsBinary.push({
|
|
|
|
name: binaryData.fileName || 'unknown',
|
|
|
|
content: binaryData.data,
|
|
|
|
type: binaryData.mimeType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 12:44:46 -07:00
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-04-30 12:44:46 -07:00
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
|
|
|
|
attachmentsList = attachmentsBinary;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const email: IEmail = {
|
|
|
|
to: toStr,
|
|
|
|
cc: ccStr,
|
|
|
|
bcc: bccStr,
|
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
|
|
|
body: this.getNodeParameter('message', i) as string,
|
|
|
|
attachments: attachmentsList,
|
|
|
|
};
|
2021-01-07 02:58:28 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (this.getNodeParameter('includeHtml', i, false) as boolean === true) {
|
|
|
|
email.htmlBody = this.getNodeParameter('htmlMessage', i) as string;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = '/gmail/v1/users/me/drafts';
|
|
|
|
method = 'POST';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = {
|
|
|
|
message: {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/get
|
|
|
|
method = 'GET';
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const format = additionalFields.format || 'resolved';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (format === 'resolved') {
|
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = `/gmail/v1/users/me/drafts/${id}`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const binaryData: IBinaryKeyData = {};
|
2020-10-15 01:41:47 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let nodeExecutionData: INodeExecutionData;
|
|
|
|
if (format === 'resolved') {
|
|
|
|
const dataPropertyNameDownload = additionalFields.dataPropertyAttachmentsPrefixName as string || 'attachment_';
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
nodeExecutionData = await parseRawEmail.call(this, responseData.message, dataPropertyNameDownload);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Add the draft-id
|
|
|
|
nodeExecutionData.json.messageId = nodeExecutionData.json.id;
|
|
|
|
nodeExecutionData.json.id = responseData.id;
|
|
|
|
} else {
|
|
|
|
nodeExecutionData = {
|
|
|
|
json: responseData,
|
|
|
|
binary: Object.keys(binaryData).length ? binaryData : undefined,
|
|
|
|
};
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = nodeExecutionData;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/delete
|
|
|
|
method = 'DELETE';
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = `/gmail/v1/users/me/drafts/${id}`;
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
Object.assign(qs, additionalFields);
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'drafts',
|
|
|
|
'GET',
|
|
|
|
`/gmail/v1/users/me/drafts`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-18 03:40:19 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.maxResults = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await googleApiRequest.call(
|
2020-08-18 03:40:19 -07:00
|
|
|
this,
|
|
|
|
'GET',
|
2021-07-19 23:58:54 -07:00
|
|
|
`/gmail/v1/users/me/drafts`,
|
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-08-18 03:40:19 -07:00
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.drafts;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (responseData === undefined) {
|
|
|
|
responseData = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const format = additionalFields.format || 'resolved';
|
|
|
|
|
|
|
|
if (format !== 'ids') {
|
2020-08-18 03:40:19 -07:00
|
|
|
if (format === 'resolved') {
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < responseData.length; i++) {
|
|
|
|
|
|
|
|
responseData[i] = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/gmail/v1/users/me/drafts/${responseData[i].id}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (format === 'resolved') {
|
|
|
|
const dataPropertyNameDownload = additionalFields.dataPropertyAttachmentsPrefixName as string || 'attachment_';
|
|
|
|
const id = responseData[i].id;
|
|
|
|
responseData[i] = await parseRawEmail.call(this, responseData[i].message, dataPropertyNameDownload);
|
2020-10-15 01:41:47 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Add the draft-id
|
|
|
|
responseData[i].json.messageId = responseData[i].json.id;
|
|
|
|
responseData[i].json.id = id;
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (format !== 'resolved') {
|
|
|
|
responseData = this.helpers.returnJsonArray(responseData);
|
|
|
|
}
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-08-18 03:40:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (['draft', 'message'].includes(resource) && ['get', 'getAll'].includes(operation)) {
|
|
|
|
//@ts-ignore
|
|
|
|
return this.prepareOutputData(returnData);
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|