auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
const extName = require('ext-name');
const urlUtil = require('url');
const path = require('path');
const fs = require("fs");
const axios = require('axios');
const attachments = [];
const NumMedia = steps.trigger.event.NumMedia;
const payload = steps.trigger.event;
for (var i = 0; i < NumMedia; i++) { // eslint-disable-line
const mediaUrl = payload[`MediaUrl${i}`];
const contentType = payload[`MediaContentType${i}`];
const extension = extName.mime(contentType)[0].ext;
const mediaSid = path.basename(urlUtil.parse(mediaUrl).pathname);
const filename = `${mediaSid}.${extension}`;
const fullPath = `/tmp/${filename}`;
const response = await axios({
url: mediaUrl,
method: 'GET',
responseType: 'arraybuffer'
})
await fs.writeFileSync(fullPath, response.data);
let contents_in_base64 = fs.readFileSync(fullPath).toString('base64');
// Postmark format
// let attachment = {
// Content: contents_in_base64,
// Name: filename,
// ContentType: contentType
// };
// SendGrid format
let attachment = {
content: contents_in_base64,
filename,
type: contentType
};
attachments.push(attachment);
}
return attachments;
auths
objectThe email address of the recipient.
The email address of the sender.
The mime type of the content you are including in your email. For example, text/plain or text/html.
The actual content of the specified mime type that you are including in your email.
return
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, params, auths) => {
}
const axios = require('axios')
let to_email
if (params.to_email.indexOf(',') > -1) {
to_email = params.to_email.split(',').map(
(item,index) => {
return {
"email": item
}
}
)
} else {
to_email = [{ "email" : params.to_email }]
}
return await require("@pipedreamhq/platform").axios(this, {
url: `https://api.sendgrid.com/v3/mail/send`,
headers: {
Authorization: `Bearer ${auths.sendgrid.api_key}`,
},
method: 'POST',
data: {
"personalizations": [{
"to": to_email,
"subject": params.subject,
}],
"from": {
"email": params.from_email,
},
"content": [{
"type": params.type,
"value": params.value
}],
"attachments": steps.parse_message.$return_value
}
})