2019-10-26 09:58:04 -07:00
import {
IExecuteSingleFunctions ,
} from 'n8n-core' ;
import {
IDataObject ,
INodeTypeDescription ,
INodeExecutionData ,
INodeType ,
2019-11-04 12:29:36 -08:00
ILoadOptionsFunctions ,
INodePropertyOptions ,
2019-10-26 09:58:04 -07:00
} from 'n8n-workflow' ;
2019-11-04 12:29:36 -08:00
import {
mandrillApiRequest ,
getToEmailArray ,
getGoogleAnalyticsDomainsArray ,
2019-10-28 14:23:25 -07:00
getTags ,
validateJSON
} from './GenericFunctions' ;
2019-10-26 09:58:04 -07:00
2019-10-28 14:23:25 -07:00
import * as moment from 'moment' ;
2019-11-04 09:29:20 -08:00
import * as _ from 'lodash' ;
interface Attachments {
type : string ;
name : string ;
content : string ;
}
interface Message {
html? : string ;
text? : string ;
subject? : string ;
from_name? : string ;
from_email : string ;
to : string [ ] ;
important? : boolean ;
track_opens? : boolean ;
track_clicks? : boolean ;
auto_text? : boolean ;
auto_html? : boolean ;
inline_css? : boolean ;
url_strip_qs? : boolean ;
preserve_recipients? : boolean ;
view_content_link? : boolean ;
async ? : boolean ;
subaccount? : string ;
google_analytics_campaign? : string ;
google_analytics_domains? : string [ ] ;
tags? : string [ ] ;
ip_pool? : string ;
bcc_address? : string ;
tracking_domain? : string ;
signing_domain? : string ;
return_path_domain? : string ;
headers? : IDataObject ;
metadata? : IDataObject ;
global_merge_vars ? : [ IDataObject ] ;
attachments? : Attachments [ ] ;
}
interface Body {
template_content : [ ] ;
template_name? : string ;
message : Message ;
send_at? : string ;
}
interface Options {
html : string ;
fromName? : string ;
text : string ;
subject? : string ;
important : boolean ;
trackOpens : boolean ;
trackClicks : boolean ;
autoText : boolean ;
autoHtml : boolean ;
inlineCss : boolean ;
urlStripQs : boolean ;
preserveRecipients : boolean ;
viewContentLink : boolean ;
async : boolean ;
subaccount : string ;
googleAnalyticsCampaign : string ;
googleAnalyticsDomains? : string ;
tags : string ;
ipPool : string ;
bccAddress : string ;
trackingDomain : string ;
signingDomain : string ;
returnPathDomain : string ;
sendAt : string ;
}
2019-10-26 09:58:04 -07:00
2019-10-28 14:23:25 -07:00
export class Mandrill implements INodeType {
2019-11-04 12:29:36 -08:00
//https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template
2019-10-26 09:58:04 -07:00
description : INodeTypeDescription = {
displayName : 'Mandrill' ,
name : 'mandrill' ,
icon : 'file:mandrill.png' ,
group : [ 'output' ] ,
version : 1 ,
2019-11-03 05:39:03 -08:00
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2019-10-28 14:23:25 -07:00
description : 'Consume mandrill API' ,
2019-10-26 09:58:04 -07:00
defaults : {
name : 'Mandrill' ,
color : '#c02428' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'mandrillApi' ,
required : true ,
}
] ,
properties : [
2019-10-28 14:23:25 -07:00
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
options : [
{
2019-11-04 12:29:36 -08:00
name : 'Message' ,
value : 'message' ,
description : 'Send a message.' ,
2019-10-28 14:23:25 -07:00
} ,
] ,
2019-11-04 12:29:36 -08:00
default : 'message' ,
description : 'Resource to consume.' ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-04 12:29:36 -08:00
{
2019-10-26 09:58:04 -07:00
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
resource : [
2019-11-04 12:29:36 -08:00
'message' ,
2019-10-28 14:23:25 -07:00
] ,
} ,
} ,
2019-10-26 09:58:04 -07:00
options : [
{
name : 'Send template' ,
value : 'sendTemplate' ,
2019-11-04 12:29:36 -08:00
description : 'Send message based on template.' ,
} ,
{
name : 'Send HTML' ,
2019-10-26 09:58:04 -07:00
value : 'sendHtml' ,
2019-11-04 12:29:36 -08:00
description : 'Send message based on HTML.' ,
2019-10-26 09:58:04 -07:00
} ,
] ,
default : 'sendTemplate' ,
description : 'The operation to perform.' ,
} ,
2019-11-04 12:29:36 -08:00
{
displayName : 'Template' ,
name : 'template' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getTemplates' ,
} ,
displayOptions : {
2019-10-26 09:58:04 -07:00
show : {
operation : [
'sendTemplate' ,
] ,
} ,
} ,
2019-11-04 12:29:36 -08:00
default : '' ,
2019-10-26 09:58:04 -07:00
options : [ ] ,
required : true ,
description : 'The template you want to send' ,
2019-11-04 12:29:36 -08:00
} ,
2019-10-26 09:58:04 -07:00
{
displayName : 'From Email' ,
name : 'fromEmail' ,
type : 'string' ,
default : '' ,
required : true ,
placeholder : 'Admin <example@yourdomain.com>' ,
description : 'Email address of the sender optional with name.' ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
operation : [
2019-11-04 12:29:36 -08:00
'sendHtml' ,
'sendTemplate' ,
2019-10-28 14:23:25 -07:00
] ,
} ,
} ,
2019-10-26 09:58:04 -07:00
} ,
{
displayName : 'To Email' ,
name : 'toEmail' ,
type : 'string' ,
default : '' ,
required : true ,
placeholder : 'info@example.com' ,
description : 'Email address of the recipient. Multiple ones can be separated by comma.' ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
operation : [
2019-11-04 12:29:36 -08:00
'sendHtml' ,
'sendTemplate' ,
2019-10-28 14:23:25 -07:00
] ,
} ,
} ,
2019-10-26 09:58:04 -07:00
} ,
2019-11-03 12:50:37 -08:00
{
displayName : 'JSON Parameters' ,
2019-11-03 05:39:03 -08:00
name : 'jsonParameters' ,
type : 'boolean' ,
default : false ,
description : '' ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
operation : [
2019-11-04 12:29:36 -08:00
'sendHtml' ,
'sendTemplate' ,
2019-11-03 12:50:37 -08:00
]
} ,
} ,
} ,
2019-11-03 05:39:03 -08:00
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
2019-11-03 12:50:37 -08:00
placeholder : 'Add Option' ,
2019-11-03 05:39:03 -08:00
default : { } ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
2019-11-04 12:29:36 -08:00
operation : [
'sendHtml' ,
'sendTemplate' ,
] ,
} ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
options : [
{
displayName : 'Subject' ,
name : 'subject' ,
type : 'string' ,
default : '' ,
placeholder : 'My subject line' ,
description : 'Subject line of the email.' ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
{
displayName : 'From name' ,
name : 'fromName' ,
type : 'string' ,
default : '' ,
placeholder : 'John Doe' ,
2019-11-04 09:29:20 -08:00
description : 'Optional from name to be used.' ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
{
displayName : 'HTML' ,
name : 'html' ,
type : 'string' ,
default : '' ,
typeOptions : {
rows : 5 ,
} ,
options : [ ] ,
2019-11-04 12:29:36 -08:00
description : 'The html you want to send.' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Text' ,
name : 'text' ,
type : 'string' ,
default : '' ,
typeOptions : {
rows : 5 ,
} ,
options : [ ] ,
2019-11-04 12:29:36 -08:00
description : 'Example text content.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'BCC Address' ,
2019-11-03 05:39:03 -08:00
name : 'bccAddress' ,
type : 'string' ,
default : '' ,
placeholder : 'message.bcc_address@example.com' ,
2019-11-04 12:29:36 -08:00
description : ` An optional address to receive an exact copy of each recipient's email. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Tracking Domain' ,
2019-11-03 05:39:03 -08:00
name : 'trackingDomain' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 12:29:36 -08:00
description : ` A custom domain to use for tracking opens and clicks instead of mandrillapp.com . ` ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Signing Domain' ,
2019-11-03 05:39:03 -08:00
name : 'signingDomain' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 12:29:36 -08:00
description : ` A custom domain to use for SPF/DKIM signing instead of<br />mandrill(for "via" or "on behalf of" in email clients). ` ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Return Path Domain' ,
2019-11-03 05:39:03 -08:00
name : 'returnPathDomain' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 12:29:36 -08:00
description : ` A custom domain to use for the messages's return-path. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Important' ,
name : 'important' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not this message is important, and should be<br />delivered ahead of non-important messages.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Track Opens' ,
2019-11-03 05:39:03 -08:00
name : 'trackOpens' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to turn on open tracking for the message.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Track Clicks' ,
2019-11-03 05:39:03 -08:00
name : 'trackClicks' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to turn on click tracking for the message.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Auto Text' ,
2019-11-03 05:39:03 -08:00
name : 'autoText' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to automatically generate a text<br />part for messages that are not given text.' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Auto HTML' ,
name : 'autoHtml' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to automatically generate an HTML<br />part for messages that are not given HTML.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Inline CSS' ,
2019-11-03 05:39:03 -08:00
name : 'inlineCss' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to automatically inline all CSS styles provided in<br />the message HTML - only for HTML documents less than 256KB in size.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Url Strip Qs' ,
2019-11-03 05:39:03 -08:00
name : 'urlStripQs' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to strip the query string from<br />URLs when aggregating tracked URL data.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Preserve Recipients' ,
2019-11-03 05:39:03 -08:00
name : 'preserveRecipients' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Whether or not to expose all recipients in to "To" header for each email.' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'View Content Link' ,
2019-11-03 05:39:03 -08:00
name : 'viewContentLink' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : 'Set to false to remove content logging for sensitive emails.' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Async' ,
name : 'async' ,
type : 'boolean' ,
default : false ,
2019-11-04 12:29:36 -08:00
description : ` Enable a background sending mode that is optimized for bulk sending.<br />In async mode, messages/send will immediately return a status of "queued"<br />for every recipient. To handle rejections when sending in async mode, set up<br />a webhook for the 'reject' event. Defaults to false for messages with<br />no more than 10 recipients; messages with more than 10 recipients are always sent<br />asynchronously, regardless of the value of async. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Subaccount' ,
name : 'subAccount' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 09:29:20 -08:00
description : 'The unique id of a subaccount for this message - must already exist or will fail with an error' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Google Analytics Campaign' ,
2019-11-03 05:39:03 -08:00
name : 'googleAnalyticsCampaign' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 09:29:20 -08:00
description : ` Optional string indicating the value to set for the utm_campaign tracking parameter. If this isn't provided the email's from address will be used instead. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Google Analytics Domains' ,
2019-11-03 05:39:03 -08:00
name : 'googleAnalyticsDomains' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 09:29:20 -08:00
description : ` An array of strings separated by , indicating for which any matching URLs will automatically have Google Analytics parameters appended to their query string automatically. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Tags' ,
name : 'tags' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 09:29:20 -08:00
description : ` An array of string separated by , to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an underscore are reserved for internal use and will cause errors. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Ip Pool' ,
2019-11-03 05:39:03 -08:00
name : 'ipPool' ,
type : 'string' ,
default : '' ,
placeholder : '' ,
2019-11-04 09:29:20 -08:00
description : ` The name of the dedicated ip pool that should be used to send the message. If you do not have any dedicated IPs, this parameter has no effect. If you specify a pool that does not exist, your default pool will be used instead. ` ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Sent At' ,
2019-11-03 05:39:03 -08:00
name : 'sendAt' ,
type : 'dateTime' ,
default : '' ,
placeholder : '' ,
2019-11-04 12:29:36 -08:00
description : ` When this message should be sent as a UTC timestamp in<br />YYYY-MM-DD HH:MM:SS format. If you specify a time in the past, the message<br />will be sent immediately. An additional fee applies for scheduled email,<br />and this feature is only available to accounts with a positive balance. ` ,
2019-11-03 05:39:03 -08:00
} ,
]
} ,
2019-10-28 14:23:25 -07:00
{
2019-11-03 12:50:37 -08:00
displayName : 'Merge Vars' ,
2019-11-03 05:39:03 -08:00
name : 'mergeVarsJson' ,
2019-10-28 14:23:25 -07:00
type : 'json' ,
typeOptions : {
2019-11-03 05:39:03 -08:00
alwaysOpenEditWindow : true ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : ` [
{
{ "name" : "name" , "content" : "content" }
}
] ` ,
2019-11-03 05:39:03 -08:00
displayOptions : {
show : {
jsonParameters : [
2019-11-04 12:29:36 -08:00
true ,
] ,
} ,
2019-11-03 05:39:03 -08:00
} ,
2019-11-04 09:29:20 -08:00
description : 'Global merge variables' ,
2019-11-03 05:39:03 -08:00
} ,
{
2019-11-03 12:50:37 -08:00
displayName : 'Merge Vars' ,
2019-11-03 05:39:03 -08:00
name : 'mergeVarsUi' ,
2019-11-03 12:50:37 -08:00
placeholder : 'Add Merge Vars' ,
2019-11-03 05:39:03 -08:00
type : 'fixedCollection' ,
default : '' ,
typeOptions : {
2019-11-04 12:29:36 -08:00
multipleValues : true ,
2019-11-03 05:39:03 -08:00
} ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
2019-11-03 05:39:03 -08:00
jsonParameters : [
2019-11-04 12:29:36 -08:00
false ,
] ,
} ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
description : 'Per-recipient merge variables' ,
options : [
{
2019-11-04 09:29:20 -08:00
name : 'mergeVarsValues' ,
2019-11-03 05:39:03 -08:00
displayName : 'Vars' ,
values : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : '' ,
} ,
{
2019-11-04 09:29:20 -08:00
displayName : 'Content' ,
name : 'content' ,
2019-11-03 05:39:03 -08:00
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
} ,
] ,
} ,
] ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Metadata' ,
name : 'metadataUi' ,
2019-11-03 12:50:37 -08:00
placeholder : 'Add Metadata' ,
2019-11-03 05:39:03 -08:00
type : 'fixedCollection' ,
default : '' ,
typeOptions : {
2019-11-04 12:29:36 -08:00
multipleValues : true ,
2019-11-03 05:39:03 -08:00
} ,
displayOptions : {
show : {
jsonParameters : [
2019-11-04 12:29:36 -08:00
false ,
] ,
} ,
2019-11-03 05:39:03 -08:00
} ,
2019-11-04 12:29:36 -08:00
description : 'Metadata an associative array of user metadata. Mandrill will store<br />this metadata and make it available for retrieval. In addition, you can select<br />up to 10 metadata fields to index and make searchable using the Mandrill search api.' ,
2019-11-03 05:39:03 -08:00
options : [
{
2019-11-04 09:29:20 -08:00
name : 'metadataValues' ,
2019-11-03 05:39:03 -08:00
displayName : 'Metadata' ,
values : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
2019-11-04 12:29:36 -08:00
default : 'Name of the metadata key to add.' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
description : 'Value to set for the metadata key.' ,
} ,
] ,
} ,
] ,
2019-10-28 14:23:25 -07:00
} ,
{
displayName : 'Metadata' ,
2019-11-03 12:50:37 -08:00
name : 'metadataJson' ,
2019-11-03 05:39:03 -08:00
type : 'json' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
displayOptions : {
show : {
jsonParameters : [
2019-11-04 12:29:36 -08:00
true ,
] ,
} ,
2019-11-03 05:39:03 -08:00
} ,
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : ` {
"website" : "www.example.com"
} ` ,
description : 'Metadata an associative array of user metadata. Mandrill will store this metadata<br />and make it available for retrieval. In addition, you can select up to<br />10 metadata fields to index and make searchable using the Mandrill search api.' ,
2019-10-28 14:23:25 -07:00
} ,
{
displayName : 'Attachments' ,
name : 'attachments' ,
2019-11-03 05:39:03 -08:00
type : 'json' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
displayOptions : {
show : {
jsonParameters : [
2019-11-04 12:29:36 -08:00
true ,
] ,
} ,
2019-11-03 05:39:03 -08:00
} ,
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : ` [
{
"type" : "text/plain" ( the MIME type of the attachment ) ,
"name" : "myfile.txt" ( the file name of the attachment ) ,
"content" : "ZXhhbXBsZSBmaWxl" ( the content of the attachment as a base64 - encoded string )
}
] ` ,
2019-11-04 09:29:20 -08:00
description : 'An array of supported attachments to add to the message' ,
2019-10-28 14:23:25 -07:00
} ,
{
2019-11-03 05:39:03 -08:00
displayName : 'Attachments' ,
name : 'attachmentsUi' ,
2019-11-03 12:50:37 -08:00
placeholder : 'Add Attachments' ,
2019-11-03 05:39:03 -08:00
type : 'fixedCollection' ,
typeOptions : {
2019-11-04 12:29:36 -08:00
multipleValues : true ,
2019-10-28 14:23:25 -07:00
} ,
displayOptions : {
show : {
2019-11-03 05:39:03 -08:00
jsonParameters : [
2019-11-04 12:29:36 -08:00
false ,
] ,
} ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
options : [
{
2019-11-04 09:29:20 -08:00
name : 'attachmentsValues' ,
displayName : 'Attachments Values' ,
2019-11-03 05:39:03 -08:00
values : [
{
displayName : 'Type' ,
name : 'type' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : 'text/plain' ,
description : 'The MIME type of the attachment.' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : 'myfile.txt' ,
description : 'The file name of the attachment.' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Content' ,
name : 'content' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : 'ZXhhbXBsZSBmaWxl' ,
description : 'The content of the attachment as a base64-encoded string.' ,
} ,
] ,
2019-11-04 09:29:20 -08:00
} ,
{
name : 'attachmentsBinary' ,
displayName : 'Attachments Binary' ,
values : [
{
displayName : 'Property' ,
name : 'property' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
description : 'Name of the binary properties which contain data which should be added to email as attachment' ,
2019-11-04 09:29:20 -08:00
} ,
2019-11-04 12:29:36 -08:00
] ,
} ,
2019-11-03 05:39:03 -08:00
] ,
default : '' ,
2019-11-04 12:29:36 -08:00
description : 'Array of supported attachments to add to the message.' ,
2019-10-28 14:23:25 -07:00
} ,
{
2019-11-03 05:39:03 -08:00
displayName : 'Headers' ,
2019-11-04 09:29:20 -08:00
name : 'headersJson' ,
2019-11-03 05:39:03 -08:00
type : 'json' ,
default : '' ,
2019-11-04 12:29:36 -08:00
placeholder : ` {
"Reply-To" : "replies@example.com"
} ` ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
2019-11-03 05:39:03 -08:00
jsonParameters : [
true
]
}
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
2019-11-04 12:29:36 -08:00
description : 'Optional extra headers to add to the message (most headers are allowed).' ,
2019-10-28 14:23:25 -07:00
} ,
{
2019-11-03 05:39:03 -08:00
displayName : 'Headers' ,
name : 'headersUi' ,
2019-11-03 12:50:37 -08:00
placeholder : 'Add Headers' ,
2019-11-03 05:39:03 -08:00
type : 'fixedCollection' ,
default : '' ,
2019-11-04 09:29:20 -08:00
typeOptions : {
2019-11-04 12:29:36 -08:00
multipleValues : true ,
2019-11-04 09:29:20 -08:00
} ,
2019-10-28 14:23:25 -07:00
displayOptions : {
show : {
2019-11-03 05:39:03 -08:00
jsonParameters : [
2019-11-04 12:29:36 -08:00
false ,
] ,
} ,
2019-10-28 14:23:25 -07:00
} ,
2019-11-03 05:39:03 -08:00
options : [
{
2019-11-04 09:29:20 -08:00
name : 'headersValues' ,
2019-11-03 05:39:03 -08:00
displayName : 'Values' ,
values : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
description : '' ,
2019-11-03 05:39:03 -08:00
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
2019-11-04 12:29:36 -08:00
description : '' ,
} ,
] ,
2019-11-03 05:39:03 -08:00
}
] ,
2019-11-04 12:29:36 -08:00
description : 'Optional extra headers to add to the message (most headers are allowed).' ,
2019-10-28 14:23:25 -07:00
} ,
2019-10-26 09:58:04 -07:00
] ,
2019-11-04 12:29:36 -08:00
} ;
methods = {
2019-10-26 09:58:04 -07:00
loadOptions : {
// Get all the available templates to display them to user so that he can
// select them easily
async getTemplates ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
2019-11-04 12:29:36 -08:00
const returnData : INodePropertyOptions [ ] = [ ] ;
2019-10-26 09:58:04 -07:00
let templates ;
try {
templates = await mandrillApiRequest . call ( this , '/templates' , 'POST' , '/list' ) ;
} catch ( err ) {
throw new Error ( ` Mandrill Error: ${ err } ` ) ;
}
for ( const template of templates ) {
2019-11-04 12:29:36 -08:00
const templateName = template . name ;
const templateSlug = template . slug ;
2019-10-26 09:58:04 -07:00
returnData . push ( {
name : templateName ,
value : templateSlug ,
} ) ;
2019-11-04 12:29:36 -08:00
}
2019-10-26 09:58:04 -07:00
return returnData ;
}
} ,
} ;
async executeSingle ( this : IExecuteSingleFunctions ) : Promise < INodeExecutionData > {
2019-11-04 12:29:36 -08:00
2019-11-04 09:29:20 -08:00
const item = this . getInputData ( ) ;
let emailSentResponse , response ;
2019-11-04 12:29:36 -08:00
const resource = this . getNodeParameter ( 'resource' ) as string ;
if ( resource === 'message' ) {
2019-10-26 09:58:04 -07:00
2019-11-04 12:29:36 -08:00
const options = this . getNodeParameter ( 'options' ) as Options ;
2019-10-28 14:23:25 -07:00
const operation = this . getNodeParameter ( 'operation' ) as string ;
const fromEmail = this . getNodeParameter ( 'fromEmail' ) as string ;
const toEmail = this . getNodeParameter ( 'toEmail' ) as string ;
2019-11-04 09:29:20 -08:00
const jsonActive = this . getNodeParameter ( 'jsonParameters' ) as boolean ;
const toEmailArray = getToEmailArray ( toEmail ) ;
2019-10-26 09:58:04 -07:00
2019-10-28 14:23:25 -07:00
const credentials = this . getCredentials ( 'mandrillApi' ) ;
2019-11-04 12:29:36 -08:00
2019-10-28 14:23:25 -07:00
if ( credentials === undefined ) {
throw new Error ( 'No credentials got returned!' ) ;
}
2019-11-04 12:29:36 -08:00
2019-11-04 09:29:20 -08:00
const message : Message = {
html : ( options . html ) ? options . html : '' ,
text : ( options . text ) ? options . text : '' ,
subject : ( options . subject ) ? options . subject : '' ,
from_email : fromEmail ,
to : toEmailArray ,
2019-11-04 12:29:36 -08:00
important : ( options . important ) ? options.important : false ,
2019-11-04 09:29:20 -08:00
track_opens : ( options . trackOpens ) ? options.trackOpens : false ,
track_clicks : ( options . trackClicks ) ? options.trackClicks : false ,
auto_text : ( options . autoText ) ? options.autoText : false ,
auto_html : ( options . autoHtml ) ? options.autoHtml : false ,
inline_css : ( options . inlineCss ) ? options.inlineCss : false ,
url_strip_qs : ( options . urlStripQs ) ? options.urlStripQs : false ,
preserve_recipients : ( options . preserveRecipients ) ? options.preserveRecipients : false ,
view_content_link : ( options . viewContentLink ) ? options.viewContentLink : false ,
async : ( options . async ) ? options.async : false ,
google_analytics_campaign : ( options . googleAnalyticsCampaign ) ? options . googleAnalyticsCampaign : '' ,
ip_pool : ( options . ipPool ) ? options . ipPool : '' ,
bcc_address : ( options . bccAddress ) ? options . bccAddress : '' ,
tracking_domain : ( options . trackingDomain ) ? options . trackingDomain : '' ,
2019-11-04 12:29:36 -08:00
signing_domain : ( options . signingDomain ) ? options . signingDomain : '' ,
2019-11-04 09:29:20 -08:00
return_path_domain : ( options . returnPathDomain ) ? options . returnPathDomain : '' ,
} ;
if ( options . googleAnalyticsDomains ) {
message . google_analytics_domains = getGoogleAnalyticsDomainsArray ( options . googleAnalyticsDomains ) ;
}
if ( options . tags ) {
message . tags = getTags ( options . tags ) ;
}
if ( options . fromName ) {
message . from_name = options . fromName ;
}
if ( options . subaccount ) {
message . subaccount = options . subaccount ;
}
const body : Body = {
2019-10-28 14:23:25 -07:00
template_content : [ ] ,
2019-11-04 09:29:20 -08:00
message ,
2019-10-28 14:23:25 -07:00
} ;
2019-11-04 09:29:20 -08:00
if ( options . sendAt ) {
2019-11-04 12:29:36 -08:00
body . send_at = moment ( options . sendAt ) . utc ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
2019-11-04 09:29:20 -08:00
}
if ( jsonActive ) {
body . message . headers = validateJSON ( this . getNodeParameter ( 'headersJson' ) as string ) ;
body . message . metadata = validateJSON ( this . getNodeParameter ( 'metadataJson' ) as string ) ;
body . message . global_merge_vars = validateJSON ( this . getNodeParameter ( 'mergeVarsJson' ) as string ) ;
body . message . attachments = validateJSON ( this . getNodeParameter ( 'attachmentsJson' ) as string ) ;
} else {
2019-11-04 12:29:36 -08:00
2019-11-04 09:29:20 -08:00
const headersUi = this . getNodeParameter ( 'headersUi' ) as IDataObject ;
if ( ! _ . isEmpty ( headersUi ) ) {
// @ts-ignore
body . message . headers = _ . map ( headersUi . headersValues , ( o ) = > {
2019-11-04 12:29:36 -08:00
const aux : IDataObject = { } ;
2019-11-04 09:29:20 -08:00
// @ts-ignore
aux [ o . name ] = o . value ;
return aux ;
} ) ;
}
const metadataUi = this . getNodeParameter ( 'metadataUi' ) as IDataObject ;
if ( ! _ . isEmpty ( metadataUi ) ) {
// @ts-ignore
body . message . metadata = _ . map ( metadataUi . metadataValues , ( o ) = > {
2019-11-04 12:29:36 -08:00
const aux : IDataObject = { } ;
2019-11-04 09:29:20 -08:00
aux [ o . name ] = o . value ;
return aux ;
} ) ;
}
const mergeVarsUi = this . getNodeParameter ( 'mergeVarsUi' ) as IDataObject ;
if ( ! _ . isEmpty ( mergeVarsUi ) ) {
// @ts-ignore
body . message . global_merge_vars = _ . map ( mergeVarsUi . mergeVarsValues , ( o ) = > {
2019-11-04 12:29:36 -08:00
const aux : IDataObject = { } ;
2019-11-04 09:29:20 -08:00
aux . name = o . name ;
aux . content = o . content ;
return aux ;
} ) ;
}
const attachmentsUi = this . getNodeParameter ( 'attachmentsUi' ) as IDataObject ;
let attachmentsBinary = [ ] , attachmentsValues = [ ] ;
if ( ! _ . isEmpty ( attachmentsUi ) ) {
2019-11-04 12:29:36 -08:00
if ( attachmentsUi . hasOwnProperty ( 'attachmentsValues' )
&& ! _ . isEmpty ( attachmentsUi . attachmentsValues ) ) {
// @ts-ignore
2019-11-04 09:29:20 -08:00
attachmentsValues = _ . map ( attachmentsUi . attachmentsValues , ( o ) = > {
2019-11-04 12:29:36 -08:00
const aux : IDataObject = { } ;
2019-11-04 09:29:20 -08:00
// @ts-ignore
aux . name = o . name ;
aux . content = o . content ;
aux . type = o . type ;
return aux ;
} ) ;
}
if ( attachmentsUi . hasOwnProperty ( 'attachmentsBinary' )
&& ! _ . isEmpty ( attachmentsUi . attachmentsBinary )
&& item . binary ) {
2019-11-04 12:29:36 -08:00
// @ts-ignore
attachmentsBinary = _ . map ( attachmentsUi . attachmentsBinary , ( o ) = > {
if ( item . binary ! . hasOwnProperty ( o . property ) ) {
const aux : IDataObject = { } ;
aux . name = item . binary ! [ o . property ] . fileName || 'unknown' ;
aux . content = item . binary ! [ o . property ] . data ;
aux . type = item . binary ! [ o . property ] . mimeType ;
return aux ;
}
} ) ;
2019-11-04 09:29:20 -08:00
}
}
2019-11-04 12:29:36 -08:00
2019-11-04 09:29:20 -08:00
body . message . attachments = attachmentsBinary . concat ( attachmentsValues ) ;
}
2019-11-04 12:29:36 -08:00
2019-10-28 14:23:25 -07:00
if ( operation === 'sendTemplate' ) {
const template = this . getNodeParameter ( 'template' ) as string ;
2019-11-04 09:29:20 -08:00
body . template_name = template ;
emailSentResponse = mandrillApiRequest . call ( this , '/messages' , 'POST' , '/send-template' , body ) ;
2019-10-28 14:23:25 -07:00
} else if ( operation === 'sendHtml' ) {
2019-11-04 09:29:20 -08:00
emailSentResponse = mandrillApiRequest . call ( this , '/messages' , 'POST' , '/send' , body ) ;
2019-10-28 14:23:25 -07:00
}
2019-11-04 12:29:36 -08:00
2019-10-28 14:23:25 -07:00
try {
2019-11-04 09:29:20 -08:00
response = await emailSentResponse ;
2019-10-28 14:23:25 -07:00
} catch ( err ) {
throw new Error ( ` Mandrill Error: ${ err } ` ) ;
}
}
2019-10-26 09:58:04 -07:00
return {
2019-10-28 14:23:25 -07:00
json : response ,
2019-10-26 09:58:04 -07:00
} ;
}
}