Forward SMS to email via Twilio and Mailgun
Back in 2017 Phil Nash wrote a very nice blog post for Twilio
called Forward incoming SMS messages to email with Node.js, SendGrid and Twilio
Functions.
I used this exact setup this year for Ratio because I wanted a dedicated phone number
for the business. Voice calls are forwarded directly to my mobile using TwiML Bins
via the following simple
bin:
Back to SMS forwarding… we don’t get many SMS messages and instead of warning us via email, SendGrid just closed the account. So for some months we weren’t getting SMS messages forwarded to our Google Group inbox. Even though I added a credit card, they could not reactivate it, so here I am with a port to Mailgun.
So, same basic steps as Phil’s blog post except Functions/Functions
are now Functions/Services
which you
can find
here.
Create a new function at path /forward
.
Under Dependencies
add mailgun.js
version 4.1.1 and form-data
version 4.0.0.
Under Environment Variables
add MAILGUN_DOMAIN
, MAILGUN_API_KEY
, FROM_EMAIL_ADDRESS
, and
TO_EMAIL_ADDRESS
.
I based this function body on the Mailgun example:
Make sure you hooked it up to the correct phone number, then test it out by sending an SMS message to your
Twilio number. If you need to debug, look in the Messaging
logs
in Twilio and the Mailgun Sending/Logs
.
If you run into issues or have feedback, please don’t hesitate to send me an email if you know it, or mention me on Twitter. Thanks!
- Jesse