Skip to content
1 change: 1 addition & 0 deletions forward-message-sendgrid/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Forward SMS message as an email (SendGrid)

The SendGrid Function will forward incoming SMS messages to an email address using the [SendGrid API](https://sendgrid.com/).
Expand Down
56 changes: 56 additions & 0 deletions forward-message-sendgrid/functions/forward-mms-sendgrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const got = require('got');
const request = require('request-promise-native');

exports.handler = function(context, event, callback) {
imagePath = event.MediaUrl0;

//read in the image here:
request({
url: imagePath,
method: 'GET',
encoding: null
})
.then(result => {

let imageBuffer = Buffer.from(result);
let imageBase64 = imageBuffer.toString('base64');

//now create the email message
const msg = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New MMS message from: ${event.From}`,
content: [
{
type: 'text/plain',
value: event.Body
}
],
attachments: [
{
content: imageBase64,
filename: "owl.png",
type: "image/png",
disposition: "attachment",
content_id: "my_image"
}
]
};

//send mail
got.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(msg)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
callback(err);
});
});
};