How to receive and parse email with Cloud Functions? - python

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

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

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.

Related

How to get the Gmail API to notify a bot whenever I get an email from a certain sender

According to the Push Notifications guide within the Gmail API . The Gmail API can notify your back-end server application whenever your Gmail mailbox changes.
To do this, you need to setup a Cloud Pub/Sub client and create a topic. After that, you can then configure your Gmail account to send notifications for mailbox updates. The guide says that
To configure Gmail accounts to send notifications to your Cloud Pub/Sub topic, simply use your Gmail API client to call watch() on the Gmail user mailbox similar to any other Gmail API call. To do so, provide the topic name created above and any other options in your watch() request, such as labels to filter on." This quote was taken from the "Getting Gmail mailbox updates
section from the guide linked above. The guide also provides an example of making a watch() request.
My problem is that I need to filter by sender, not label. I am creating a bot that is going to be deployed to a hosting provider, probably GCP.
I am basically looking for a way to get the Gmail API to notify the bot whenever I get an email from a certain sender. I looked at the watch method in the docs and it seems like the only possible filter is label.
So is there an alternative way to get a notification via the Gmail API whenever I get an email from a certain sender?
Update:
Oh ok, so I was reading about publishing messages to topics via the Pub/Sub API, and according to the description of watch in the Gmail API docs, it seems like watch is responsible for getting Gmail to send (or publish) notifications to the topic. The Subscriber overview section of the Pub/Sub API docs say that
To receive messages published to a topic, you must create a subscription to that topic. The subscription connects the topic to a subscriber application that receives and processes messages published to the topic."
So the subscriber application would be the bot. According to the Filtering messages section of the Pub/Sub API docs, you can create a subscription with a filter. It says
"When you receive messages from a subscription with a filter, you only receive the messages that match the filter. The Pub/Sub service automatically acknowledges the messages that don't match the filter. You can filter messages by their attributes."
So, do the messages (or notifications) that Gmail is sending (or publishing) to the topic include a "Sender" attribute? I can't seem to find the attributes that Gmail includes in a notification message...
No attributes are populated by default
See this example notification structure here: https://developers.google.com/gmail/api/guides/push. As outlined in the spec, it's permissible for attributes to be empty so long as data is populated, which it is for Gmail API notifications.
So the answer to your question - if you cannot find the "Sender" attribute - this is because the message does not contain any.

How to add custom email headers in Google App Engine Mail API

I need help on how to add several custom email headers when sending email using GAE's Mail API (Python) i.e. Message-id, person-id and etc.
But somehow, I don't see it in the documentation: https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.mail
I can receive emails and read custom email headers using the method below:
mail_message.original.getitem('mail_header')
But I don't know how to send an email with custom email headers.
Here is the code i use to send email:
mail.send_mail(sender=sender_address,
to="handsome_me#yahoo.com",
subject="gwapo",
body="Hi")
Looks like you missed this line in the docs:
PROPERTIES = set(['body', 'amp_html', 'sender', 'to', 'cc', 'bcc', 'attachments', 'headers', 'html', 'reply_to', 'subject'])
You can provide a headers parameter containing a list of custom headers.
Looking at the sources, the headers paramenter is subject to the following restrictions:
must be a dict
all values must be strings
all keys must be ASCII
key should be in the following white list: Auto-Submitted, In-Reply-To, List-Id, List-Unsubscribe, On-Behalf-Of, References, Resent-Date, Resent-From, Resent-To.
I'm afraid you won't be able to override message-id using AppEngine as it is not in the HEADERS_WHITELIST. I forgot how crippled AppEngine is, you may want to check third-party email services. I've used mailgun and they allow arbitrary headers as long as they are prefixed by X- (just include a h:X-My-Arbitrary-Header in the payload). From the AppEngine docs:
Sending Messages with Third-Party Services
Python 2.7/3.7 |Java 8/11 |PHP 5/7 |Ruby |Go 1.9/1.11/1.12 |Node.js
Python 3.7 applications on App Engine can use third-party companies to send email, SMS messages, or make and receive phone calls. For example, you might send email to confirm business transactions, confirm the creation of user accounts, or send marketing communications.
This page lists some examples of companies that provide communication services and client libraries for Python 3.7 applications on App Engine.
Note: The services offered by these third-party companies are not covered by the App Engine Service Level Agreement.
Mailgun
Mailgun provides both RESTful APIs and client libraries for sending email. As a GCP project user, your first 30,000 messages are free every month. See the monthly pricing calculator on the sign up page for pricing on additional messages and volume discounts.
Learn more about Mailgun:
Mailgun developer documentation
Mailgun client libraries
...
SendGrid
You can use SendGrid to power your emails on App Engine. SendGrid can improve your deliverability and provide transparency into what actually happens to those emails your app sends. You can see statistics on opens, clicks, unsubscribes, spam reports and more with the SendGrid interface or its API.
App Engine customers can send 12,000 emails every month for free by signing up for the SendGrid Email API plan through the Google Cloud Platform Console. Note that Google is compensated for customers who sign up for a paid account.
Learn more about SendGrid:
SendGrid developer documentation
SendGrid client libraries
I'm not affiliated with any of those and the original page mentions other services, I'm quoting mailgun because I've used it and know it works, and sendgrid because it is mentioned by OP.
Im working on devselz.com a service which allows unlimited emails to be sent / month. It uses a P2P with private servers, where on users can use their own servers (by installing a compatct SMTP provided by devselz.)
Here are the docs: https://devselz.com/support/

Amazon MWS - how do I go about using AnyOfferChanged Notification with python

I want to develop an app that sends me an email when pricing offers for specific listings change using the AnyOfferChanged MWS notifications. However, I can't find any good documentation on how to go about receiving the notifications. Is it a must to have AWS SQS, or can I use Django? how do I go about subscribing to a notification?
I already have a developers account and I'm using the python mws library
You need to subscribe to the AnyOfferChangedNotification through the Subscriptions API and yes, it must use SQS. I found it easiest to use the scratchpad to create the subscription, since it's usually a one-time event.
Once your price change notifications start flowing into your queue, write an app that reads the queue and you can respond to your messages, including sending an email if that's what you want to do.
See if these code samples for SQS help you: https://docs.aws.amazon.com/code-samples/latest/catalog/code-catalog-python-example_code-sqs.html

GAE Python - Receive incoming email on #mydomain.com address

I am setting up a GAE app that triggers certain tasks by an email from the user. I've set up the app using the information on this page: https://developers.google.com/appengine/docs/python/mail/receivingmail. Everything works as expected, however, I would like to send the emails to a #mydomain.com email address, instead of a #myapp.appspotmail.com email address.
Currently I am using an automatic forward on the email address to get around this, however once this scales I expect to reach the daily limit on forwarded messages at some point (10.000 according to https://support.google.com/a/answer/166852?hl=en).
Is there any way that I can integrate my app with the #mydomain.com email address to prevent this from happening? Or would you know another solution for this issue?
Probably, you should check recently implemented GAE integration with SendGrid, I haven't used it myself (use the same solution you've described with email forwarding), but looks like it can help.
As LA_ mentioned, SendGrid offers a webhook for this. I ended up using MailGun, which has a similar functionality and calls a HTTP POST to my app whenever an email comes in on my #mydomain.com address.

send google hangout notification using python

Is there an API which allows me to send a notification to Google Hangout? Or is there even a python module which encapsulates the Hangout API?
I would like to send system notification (e.g. hard disk failure reports) to a certain hangout account. Any ideas, suggestions?
Hangouts does not currently have a public API.
That said, messages delivered to the Google Talk XMPP server (talk.google.com:5222) are still being delivered to users via Hangouts. This support is only extended to one-on-one conversations, so the notification can't be delivered to a group of users. The messages will need to be supplied through an authenticated Google account in order to be delivered.
There is pre-alpha library for sending hangouts messages on python:
https://pypi.python.org/pypi/hangups/0.1
The API have been reverse engineered and is not published (as someone posted in comments). Thus it might change a Google's will.
Also, messages sent using XMPP ceased to be delivered to Hangouts users. I guess this is another cut (of the thousand scheduled).
I send alarms and other notifications with a python script (failures on database server, partitions without free space, etc), using hangouts. It's easy. Look at http://www.administracion-linux.com/2014/07/enviar-mensajes-por-hangout-desde.html to send hangouts.

Categories

Resources