Skip to content

Instantly share code, notes, and snippets.

@ahmadnassri
Created November 2, 2018 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadnassri/9c1032697b2870dc6eebc5d2dfc29e34 to your computer and use it in GitHub Desktop.
Save ahmadnassri/9c1032697b2870dc6eebc5d2dfc29e34 to your computer and use it in GitHub Desktop.
Twilio Functions: Email to SMS with SendGrid Parse
const from = '+1xxxxxxxxxx' // your phone number
const sender = 'address@domain.tld' // your email address
exports.handler = function (context, event, callback) {
// use the first part of the reply-to address as destination
const to = event.to.split('@').shift()
// take only the first line of the email body as the sms message
const body = event.text.split('\n').shift()
// display a log message for debugging
console.log('to: [%s] message: %s', to, body)
// parse message envelope
const envelope = JSON.parse(event.envelope)
// only accept emails from yourself
if (envelope.from !== sender) {
console.log('invlid sender: %s ', envelope.from)
// quietly fail
return callback()
}
// send sms using Twilio with quiet failure
context.getTwilioClient().messages.create({ from, to, body }, () => callback())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment