Skip to content

Instantly share code, notes, and snippets.

@ahmadnassri
Created November 2, 2018 04:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadnassri/b78bccac0d85ac982d68eff23dbd9697 to your computer and use it in GitHub Desktop.
Save ahmadnassri/b78bccac0d85ac982d68eff23dbd9697 to your computer and use it in GitHub Desktop.
Twilio Functions: SMS to Email Twilio with SendGrid Client
// import sendgrid library
const sendgrid = require('@sendgrid/client')
// target email address
const email = 'address@domain.tld'
// sms email domain
const domain = 'sms.domain.tld'
exports.handler = function (context, event, done) {
// authenticate with token
sendgrid.setApiKey(context.SENDGRID_API_KEY)
// configure request object
const request = {
method: 'POST',
url: '/v3/mail/send',
body: {
personalizations: [{
to: [{ email }]
}],
from: {
email: `${event.From}@${domain}`
},
subject: `RE: SMS: ${event.From}`,
content: [{
type: 'text/plain', value: event.Body
}]
}
}
sendgrid
.request(request) // send the email
.then(() => done(null, new Twilio.twiml.MessagingResponse())) // send TwiML output
.catch(done) // quiet failure
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment