Trigger event from gmail new mail - gmail

Is there an API or events that can be trapped within gmail so that I can initiate a workflow or even fire a python script.
I am trying to automate a job which will extract a csv attachment from an email that has arrived in gmail. It will then ETL the csv into BigQuery.
Any thoughts ?

Recently did this integration:
Turn on watching for message events: https://developers.google.com/gmail/api/v1/reference/users/watch
When you get a new incoming messageId (messagesAdded history type) you ask for its information (in full format): https://developers.google.com/gmail/api/v1/reference/users/messages/get
In metadata you look for a payload.parts[i] where it has body.attachmentId and get an attachment using its id: https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get

Im not sure within gmail itself,
but one thing you try is to write a python script utilising IMAP lib to read gmail messages and load it to bigquery using googleclient python. Set it using cron to run every x minutes to check the gmail account.
This should give you a starter code to read gmail messages:
https://gist.github.com/baali/2633554
If you want just new messages, just replace the search param 'ALL' with 'UNSEEN'.
More info:
https://docs.python.org/2/library/imaplib.html
Example python to load it to BigQuery:
https://developers.google.com/bigquery/loading-data-into-bigquery
I am still waiting for IFTTT https://ifttt.com/wtf to provide the API for users to create their own channel or custom action. i.e. Google BigQuery channel.
Hope that helps.

Related

How to trigger automation job only when there is data in table

I have made an automation which sends email of a visualization . Now i want to trigger email only when there is data in that visualization . Is it possible to do so if yes and then please let me know.
There is a way in which this is possible but it's fairly convoluted.
Create an Automation job file which has the 'Open analysis from Library' step (link)
Schedule it however you/your organization usually schedule Spotfire automations. Here's how to schedule it using Windows Task Scheduler: link
Once you have this set up, create an IronPython script within Spotfire which checks the underlying datatable: data check
If data exists, send email: HTML email
Since you're working with a visualization, you might have to google a bit to modify the code to export the image and then create a MIMEImage object which will then be sent with the email.

Is it possible to send emails via GMAIL RESTful APIs without having a copy in Sent Folder?

Thank you all in advance.
Is it possible to send an email via the GMAIL RESTful API (https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media) without saving a copy in the sent folder?
I have seen implementations in Outlook (Exchange/REST/Graph APIs) where emails can be sent without having copies in the sent folder. I was wondering if this is possible with the GMail APIs as well.
Again, thanks.
Workaround:
Currently, sending an email using Gmail API will always create a copy in the SENT folder, but one workaround you can do is:
Get the current timestamp timestamp before sending the message.
Send the message using messages.send.
Call users.messages.list with query parameter labelIds=SENT (note: this is case-sensitive) and ?q=in:sent after:<timestamp> to list the most recent sent message.
Call users.messages.delete and use the ID fetched from the previous call as the parameter.
References:
Method: users.messages.list
Method: users.messages.delete

How to send emails from python without needing to deal with security

I'm trying to send an email from python on an AWS Sagemaker jupyter notebook, to let me know when something is done. However, every solution I've found (doing it all from scratch, using Yagmail, whatever) results in this error:
Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 o12sm15924607qtl.48 - gsmtp')
I think this is a security measure, but this is problematic because I don't have a browser to log into.
I have tried
turning off 2fa
enabling less secure apps
for reference, this is an example of the code i'm using
#emailing myself
contents = [
'bla bla bla'
]
yagmail.SMTP('*******','********').send('*******', 'Done', contents)
I don't particularly care about compromising my password, nor do I care about security settings, nor do I mind setting up an email through a different service. the account i'm using is exclusively for this task. Does anyone know of a secrete spell, option laid away in Google security, or some other service I can use to make this work?
You could make a SNS Topic and add your E-Mail address you want to send the notification to as a receiver(subscriber). Then you can simply send a Notification/E-Mail using the boto3 SNS Client.
Set up SNS Topic
Send Notification via boto3

Scheduling an email with the Gmail API

I found a similar question from 2016, however at that time Gmail itself did not support scheduled sending of emails.
Now that you can schedule messages to send later directly from Gmail, I was wondering if there was a way to do it with their API.
Interestingly, scheduled emails appear as message objects when calling messages.list, but they do not contain any labels.
Any help would be appreciated! And if it's not possible at the moment, it would be awesome to get a reply from someone at Google about when this will become possible (I believe they officially endorse the gmail-api tag to StackOverflow)
I don't think a time-based trigger will work--even if you write the code to store email send data and then build something that regularly checks whether it's time for an email to be sent. See Google's documentation on triggers, and you'll notice that time-based triggers aren't available for Gmail scripts.
Unfortunately, there is no Gmail API endpoint for scheduling the sending of emails directly.
One workaround would be to write a script in Google Apps Script (https://script.google.com) which handles the composing of the email you wish to send, as well as a function to send the mail via the API. You can then use the built-in 'Apps Script Project Triggers' feature to trigger the function to run on a schedule; for example on action/event or at a specific/repeated time.
Button for adding trigger to Apps Script

trigger a .sh script when a specific subject email is received

Anyway, I have a script that I want to run whenever I receive an email on gmail. And if possible a subject specific email. is such a thing possible and if so, what programs do I need to allow it.
You can't instruct gmail to trigger an external script for you. I think you've got a few basic choices. In order of increasing difficulty and complexity:
1) Configure a gmail filter to deliver your desired messages to a special folder. Write a script to poll that folder, download (or delete or mark as read) messages it finds there, and then launch your local script. Set up a cron on your local machine to run the script every few minutes. You can poll the folder with IMAP or the GMAIL API. IMAP is probably easier. This will be tricky with shell, you're better of with Python, PHP, or similar.
2) Configure a gmail filter to forward your desired messages to an address on a mail server that you control. Use procmail or similar to intercept the incoming messages and launch your script.
3) Set up an account at Mailgun and configure the emails so they get delivered there directly. (Or forward from gmail as in #2.) Configure Mailgun to launch an API request when it receives messages. Build an API handler to receive the request. Launch your process from your API handler.
I have never done it, but I guess the first thing you should do is to take a look at the Google's Gmail API...
What is the Gmail API?
The Gmail API gives you flexible, RESTful access to the user's inbox,
with a natural interface to Threads, Messages, Labels, Drafts, and
History.
It seems to fit what you want - at least, without knowing the details of what you want to do.
The Gmail API can be used in a variety of different applications,
including, typically:
Read-only mail extraction, indexing, and backup
Label management
(add/remove labels)
Automated or programmatic message sending
You can use several programming languages - maybe the trick is using your programming language of choice to write a wrapper for the .sh script... I hope this helps!

Resources