SMS Pictures to Email
@mjclemente
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
inactive
last updated:-last event:-
steps.
parse_message
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
}
46
  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;
steps.
send_email
auth
to use OAuth tokens and API keys in code via theauths object
(auths.sendgrid)
params
To email

The email address of the recipient.

 
string ·params.to_email
Subject
 
string ·params.subject
From email

The email address of the sender.

 
string ·params.from_email
Type

The mime type of the content you are including in your email. For example, text/plain or text/html.

 
string ·params.type
Value

The actual content of the specified mime type that you are including in your email.

 
string ·params.value
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
}
37
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
  }
})