We’ve recently built an inMail type feature where a user of our app can message another user using Sendgrid parse.
The flow is as follows: User1 messages User2 from within our app. User2 gets an email from our app with a reply to email of uniquekey@parse.ourdomain.com. User2 replies to the email from User1. The email with it’s special reply to domain is routed through sendgrid back to our messaging app in Bubble. The uniquekey is used to figure out who sent who a message and tie that message back to the right inmail box.
Seems complicated, but it was surprisingly easy. This is how we did it…
-
Create an endpoint in Bubble’s API Workflows. Let’s call the endpoint “inmail”.
-
Follow this guide to set up your DNS.
You’ll need to add an MX record to your DNS. (I added 2, one for prod and dev)
prod: “parseprod@mydomain.com”
dev: “parsedev@mydomain.com”
- In Sendgrid, left nav, select inbound parse, and click “Add Host & URL” (upper right). You’ll do this for each of your MX records.
subdomain = “parseprod”
domain = “yourdomain”
destination URL =
prod - “https://mybubble.is…/api/1.1/wf/inmail”
subdomain = “parsedev”
domain = “yourdomain”
destination URL =
dev - “https://mybubble.is…/version-test/api/1.1/wf/inmail”
-
Back to Bubble, set the “inmail” endpoint to “detect request data” . Response type = JSON
-
As the first action, store what the endpoint receives so that you have the full message to troubleshoot later
-
Let’s test it…on dev. Trigger an email from your app to yourself, but use a reply_to email that has the following format. Also store in Bubble a table with “To user”, “From user” and “RandomString”. This will enable you to figure out who is sending who a response when you get the inbound parse message back (from User2).
randomstring@parsedev.yourdomain.com -
Alls well, you should get an email from your Bubble app. Reply to it (eg. reply to randomstring@parsedev.yourdomain.com), this will go through sendgrid and get routed to your “inmail” endpoint then get stored in your app because that’s the 1st action.
-
That’s it. If you want to further parse the contents of the email
:extract with Regex (?<=<)(.*)(?=@) in bubble to get the “To” email address (who wrote who back)
:extract with Regex to parse off only the most recent reply email. Sendgrid by default sends the entire email chain back, so we grab the characters prior to sendgrid timestamp. I’m using python to do this step because the regex was a pain, but it’s also likely doable fully with :extract with Regex.
This is the gist of the regex I’m using.
left of … On [J|F|M|A|J|S|O|N|D][a-z]{2}.[0-9]{2}, [0-9]{4}
or
left of On [J|F|M|A|J|S|O|N|D][a-z]{2}.[0-9]{4}, [0-9]{4}
It works, though if your users’ respond with a timestamp of their own, their message will be cut short. Ideally, sendgrid would have an option to inbound parse the most recent message. I didn’t see it. Would love to know if someone has a better solution.
If anyone is interested in figuring out the regex in bubble, I’ve include a sample below. The goal is to uniquely isolate the left-hand side “…new message”
…new message > On Feb 10, 2019, at 8:34 PM, …old message
…new message \n\n\n\n\n\nOn Sat, Feb 9, 2019 11:22 AM, …old message
…new message \n\n\nOn Thu, Nov 29, 2018 1:46 AM, …old message