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

### WARNING !!!
### This Repo is now out of date.

With the release of Node v18, the Node.js ecosystem are migrating over from the old CommonJS (CJS) standard to the newer, ES Modules (ESM) standard. You can read about the differences in far more detail in this [Blog Post.](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1). The following snippets may causes errors.


# 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 SMS 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);
});
});
};