Skip to content

Instantly share code, notes, and snippets.

@r0yfire
Created July 26, 2021 18:56
Show Gist options
  • Save r0yfire/9f4d04cb3b42411f109955956e8ea54e to your computer and use it in GitHub Desktop.
Save r0yfire/9f4d04cb3b42411f109955956e8ea54e to your computer and use it in GitHub Desktop.
Twilio serverless function to forward incoming SMS to Email
const got = require('got');
exports.handler = function(context, event, callback) {
const requestBody = {
to: context.TO_EMAIL_ADDRESS,
from: context.FROM_EMAIL_ADDRESS,
subject: `New SMS message from: ${event.From}`,
text: event.Body,
};
const apiKey = `Basic ${Buffer.from(`api:${context.MAILGUN_API_KEY}`).toString('base64')}`;
got
.post('https://api.mailgun.net/v3/notice.autohost.ai/messages', {
headers: {
Authorization: apiKey,
},
body: requestBody,
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
console.log(err);
callback(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment