generated from n8n-io/n8n-nodes-starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: "create email draft" operation
- Loading branch information
1 parent
f53c098
commit 81e673f
Showing
3 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
nodes/Imap/operations/email/functions/EmailCreateDraft.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { AppendResonseObject, ImapFlow } from "imapflow"; | ||
import { IExecuteFunctions, INodeExecutionData, NodeApiError } from "n8n-workflow"; | ||
import { IResourceOperationDef } from "../../../utils/CommonDefinitions"; | ||
import { getMailboxPathFromNodeParameter, parameterSelectMailbox } from '../../../utils/SearchFieldParameters'; | ||
|
||
|
||
function unicodeToBase64(str: string) { | ||
return Buffer.from(str).toString('base64'); | ||
} | ||
|
||
|
||
const PARAM_NAME_DESTINATION_MAILBOX = 'destinationMailbox'; | ||
|
||
export const createDraftOperation: IResourceOperationDef = { | ||
operation: { | ||
name: 'Create Draft', | ||
value: 'createDraft', | ||
}, | ||
parameters: [ | ||
{ | ||
...parameterSelectMailbox, | ||
description: 'Select the mailbox', | ||
name: PARAM_NAME_DESTINATION_MAILBOX, | ||
}, | ||
// select input format (RFC822 or fields) | ||
{ | ||
displayName: 'Input Format', | ||
name: 'inputFormat', | ||
type: 'options', | ||
options: [ | ||
{ | ||
name: 'Fields', | ||
value: 'fields', | ||
}, | ||
{ | ||
name: 'RFC822 Formatted Email', | ||
value: 'rfc822', | ||
}, | ||
], | ||
default: 'fields', | ||
description: 'Select the input format of the email content', | ||
}, | ||
// required parameters for fields input format | ||
{ | ||
displayName: 'Subject', | ||
name: 'subject', | ||
type: 'string', | ||
default: '', | ||
description: 'The subject of the email', | ||
displayOptions: { | ||
show: { | ||
inputFormat: [ | ||
'fields', | ||
], | ||
}, | ||
}, | ||
}, | ||
{ | ||
displayName: 'From', | ||
name: 'from', | ||
type: 'string', | ||
default: '', | ||
description: 'The email address of the sender', | ||
displayOptions: { | ||
show: { | ||
inputFormat: [ | ||
'fields', | ||
], | ||
}, | ||
}, | ||
}, | ||
{ | ||
displayName: 'To', | ||
name: 'to', | ||
type: 'string', | ||
default: '', | ||
description: 'The email address of the recipient', | ||
displayOptions: { | ||
show: { | ||
inputFormat: [ | ||
'fields', | ||
], | ||
}, | ||
}, | ||
}, | ||
{ | ||
displayName: 'Text', | ||
name: 'text', | ||
type: 'string', | ||
default: '', | ||
description: 'The text of the email', | ||
displayOptions: { | ||
show: { | ||
inputFormat: [ | ||
'fields', | ||
], | ||
}, | ||
}, | ||
}, | ||
// rfc822 input format | ||
{ | ||
displayName: 'RFC822 Formatted Email', | ||
name: 'rfc822', | ||
type: 'string', | ||
default: '', | ||
required: true, | ||
typeOptions: { | ||
rows: 10, | ||
}, | ||
displayOptions: { | ||
show: { | ||
inputFormat: [ | ||
'rfc822', | ||
], | ||
}, | ||
}, | ||
}, | ||
], | ||
|
||
async executeImapAction(context: IExecuteFunctions, itemIndex: number, client: ImapFlow): Promise<INodeExecutionData[] | null> { | ||
var returnData: INodeExecutionData[] = []; | ||
|
||
const destinationMailboxPath = getMailboxPathFromNodeParameter(context, itemIndex, PARAM_NAME_DESTINATION_MAILBOX); | ||
|
||
const inputFormat = context.getNodeParameter('inputFormat', itemIndex) as string; | ||
|
||
// compose rfc822 content | ||
let rfc822Content = ''; | ||
if (inputFormat === 'rfc822') { | ||
rfc822Content = context.getNodeParameter('rfc822', itemIndex) as string; | ||
} else { | ||
const subject = context.getNodeParameter('subject', itemIndex) as string; | ||
const from = context.getNodeParameter('from', itemIndex) as string; | ||
const to = context.getNodeParameter('to', itemIndex) as string; | ||
const text = context.getNodeParameter('text', itemIndex) as string; | ||
|
||
let headers = []; | ||
|
||
if (subject) { | ||
headers.push(`Subject: =?UTF-8?B?${unicodeToBase64(subject)}?=`) | ||
} | ||
if (from) { | ||
headers.push(`From: =?UTF-8?B?${unicodeToBase64(from)}?=`) | ||
} | ||
if (to) { | ||
headers.push(`To: =?UTF-8?B?${unicodeToBase64(to)}?=`) | ||
} | ||
if (text) { | ||
headers.push(`Content-Type: text/plain; charset=utf-8`) | ||
headers.push(`Content-Transfer-Encoding: base64`) | ||
} | ||
|
||
rfc822Content = headers.join('\r\n') + '\r\n\r\n'; | ||
if (text) { | ||
rfc822Content += unicodeToBase64(text); | ||
} | ||
} | ||
|
||
await client.mailboxOpen(destinationMailboxPath, { readOnly: false }); | ||
|
||
const resp : AppendResonseObject = await client.append( | ||
destinationMailboxPath, | ||
rfc822Content, | ||
["\\Draft"], | ||
); | ||
|
||
|
||
if (!resp) { | ||
throw new NodeApiError(context.getNode(), {}, { | ||
message: "Unable to create draft, unknown error", | ||
}); | ||
} | ||
|
||
var item_json = JSON.parse(JSON.stringify(resp)); | ||
|
||
returnData.push({ | ||
json: item_json, | ||
}); | ||
|
||
return returnData; | ||
}, | ||
}; |