Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive and parse email with Cloud Functions?

Google Cloud Functions allows you to easily activate a function upon a trigger (eg Firebase data change, HTTP request...).

I am looking for a way to execute a function when a user sends (or typically replies) to a email address. For instance, my dashboard sends an email, I would like to catch the reply, parse the content and upload it to Firebase as a comment in my Dashboard.

I understand that Google recommends to use Sendgrid. I however don't understand: - How to setup the trigger upon a reply - How to read the content and set the reading

I only found how to send emails here

like image 481
JohnAndrews Avatar asked Feb 05 '19 23:02

JohnAndrews


Video Answer


2 Answers

One option if you use GSuite is to use the Gmail watch mechanism to listen to new emails in your inbox. The message can then be posted to a PubSub topic which can trigger a Cloud Function to parse the email and perform your necessary next steps.

Here is a good use case that explains this mechanism
https://cloud.google.com/blog/products/application-development/adding-custom-intelligence-to-gmail-with-serverless-on-gcp

like image 115
Kannappan Sirchabesan Avatar answered Sep 21 '22 23:09

Kannappan Sirchabesan


Google Cloud Functions does not provide a permanent listener on an endpoint. There is also no event source for SMTP, which is the protocols involved with email delivery. So you can't simple respond to emails as they come in with Cloud Functions at the moment.

What you can do is direct the traffic to an existing SMTP server, and then use Cloud Functions to read from there at an interval.

An alternative is to use the Sendgrid Inbound Email API, which can call a webhook for every message it receives. And your webhook would then be a HTTP triggered Cloud Function.

like image 36
Frank van Puffelen Avatar answered Sep 23 '22 23:09

Frank van Puffelen