Created
November 2, 2018 04:33
-
-
Save ahmadnassri/b78bccac0d85ac982d68eff23dbd9697 to your computer and use it in GitHub Desktop.
Twilio Functions: SMS to Email Twilio with SendGrid Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import sendgrid library | |
const sendgrid = require('@sendgrid/client') | |
// target email address | |
const email = '[email protected]' | |
// 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