Blog
March 26, 2024

How to automate Gmail with n8n AI


Featured image for “How to automate Gmail with n8n AI”

Hello everyone Gmail has an incredible Potential for building automations with External tools such as na10 and today we Are about to build some really cool Workflows with new na10 AI nodes for Example we will use open AI assistants To create drafts of emails for our Incoming for example inquiries from Customers and so on so let's get started Let's start with the first example which Is autol labeling of new incoming emails I've prepared threee labels and I want To automatically add the specific label For the message that comes into my inbox So let's go to the workflow and as you Can see I have already added my Credentials for Gmail here I will leave You link in the description for official Documentation in na10 on how to set up Your credentials in this example I will Use Gmail trigger which will check for New messages every minute and when I Test execute this note you can see that I received a message and this is my Inquiry that I received from my Potential customer as I have the ID of This message I can download the full Content of this email and forward it to Further steps of this workflow so when I Execute this note you can see that in Table view I have not only the basic Information such as threat ID or message ID but I also have the content of this Message in HTML or plain text and here

Comes the crucial part which is the AI Chain here as a user prompt I want to Pass the plane text message from the Previous note and as a system prompt I Want to add instructions about labels Available in my Gmail account in this Example I've got three labels available The first one is partnership the second One inquiry and the third one is Notification I also added the Instruction that one email message can Have more than one label in settings of Open AI note I choose model gp4 turbo Preview and as a response mode I choose Json I also limited the temperature to 0.0 in output parser I defined my Json Schema because I wanted the chain to Return the array of labels that should Feed the content of my incoming emails Let's right now make a test execution of This chain for the data that I already Have in my workflow which is the inquiry About smartphones as you can see in the Output of this chain I have correctly Received the label of this message which Is inquiry in the next step I simply set This array once again to easier Manipulate this workflow and make some Future changes a bit less complicated Right now the workflow has two paths the First one is responsible for getting all Labels for the specific Gmail account so When I execute this note I should Receive all labels that I can add to my

Emails within those labels we can see The free ones that we added to our Gmail Account and those labels have also Specific IDs the second branch is Responsible for splitting out the array Of labels that were generated by the AI Chain next we want to merge the outputs Of those two Puffs together and as a key We use here the name of the label Eventually we should receive the IDS of The labels that have been generated by The AI chain in this example message has Only one label which is inquiry finally We can create an array of labels for This email message and for this purpose We use aggregate note where we want to Specify the IDS of those labels and in The Next Step which is the final step we Select the ID of the message in our Inbox and in the label names field we Simply added the array of the labels for This message so after we test execute This note we should receive in our email Account the correctly added label for This message since the workflow is ready We can test it in action so what I'm Going to do right now is I'm going back To na10 and I will activate the workflow Now every message that comes into my Inbox will be checked whether the labels Available for this Gmail account can be Added to this specific email so I Received a message about shipment that Is available and when I refreshed my

Inbox you can see that the workflow Added properly the label notification in The second example we will build a Workflow for creating autod drafts of Replies that are based on our company Data so for this purpose we will use Open AI assistant and I've also prepared An example database of products each Position includes the name of the Product description price or even stock Number and photos of those items now we Need to add this file to our assistant I've have already created the open a Assistant so I go to top assistants and Here you can see the name of my Assistant instructions that I've pasted And the model that I use I used of Course P PT for Turbo preview and I've Also added the retrieval mode because I Want to use our products database as a File so I can test my assistant and I Will ask the question what laptops are Available in your offer so when I run This assistant I should receive a Response that is based on the database Of products that I've uploaded and I Receive of course the correct response Now let's get to our Gmail account and Let's create a new label I will create This label because I want the workflow To recognize which emails should be Processed by the open AI assistant so I've created the label Ai and I will add It to one of the messages in this

Workflow I will use the schedule trigger To run the workflow every minute and in The next step I want to get all messages That have specific labels in this Example I want to get all inquiries that Have label AI once again I will need the Full content of the message which is the Plain text and this is the parameter That I will pass to the second Noe which Is the open AI assistant Noe since I've Already created my assistant using open AI dashboard I don't need to create the Second one using na10 Noe so I simply Choose my assistant from the list and I Can test run this note in the output I Receive a response from the assistant Which is correct and this one is Formatted in markdown so we need to a Bit format it to actually create a draft Of this response but first I need to set Some parameters for example I need to Set threet ID email address of the Recipient the subject of the message and Message ID with this done I can move to The next step which is converting the Markdown to HTML for this purpose I use The specific note in na10 and as a Markdown I of course point to the input That is response from open AI assistant And as a destination key I use response As well because I simply want to replace The markdown input with the HTML output Next we need to format our message with The RFC standard so I create a set note

With the property row and I need to Point the recipient of the message the Subject of the message and as a content Type I need to paste HTML because we Will use HTML as the basic format of our Message in order to make this email Draft acceptable by the Gmail API we Need to convert it to base 64 so for This purpose I use the simple code Snippet and the code note in na10 so After executing this note I should Receive the Bas 64 encoded string that I Can use in my API call finally comes the Crucial part which is inserting the Draft to our Gmail account and for this Purpose I use the endpoint drafts for Gmail API and of course I make a post Request with the HTTP request note I Want to also use the predefined Credential which I've prepared before Finally I need to add the body of this Request so I simply paste the Json with Property message and under this property I need to specify the r message which is The encoded message from open AI Assistants and the Fret ID that is the Destination of my email draft so after I Click test note I should see in my Gmail Inbox that the draft for this specific Message has been added and when I scroll Down I can see that this one is the Proper answer for the question that I Received in the incoming email still However I need to delete this AI label

From this message because otherwise Workflow will keep generating drafts for This message over and over again so in The last step of this workflow I used Gmail note once again and I point to the Message ID that has the label Ai and I Simply want to delete it after I refresh My inbox you can see that this AI label Is gone but what if you want to trigger One or multiple automations for the Specific email message manually so for This purpose I've created the dedicated Gmail addon with this plugin you can Trigger your automations for messages Instantly for example right now I want To add the label using AI for this Specific message and when I refresh my Inbox I can see that here is the added Label for for this email which is the Notification label in the second message I want not only to add the AI label but Also to create AI generated the draft of The response so after I triggered the Second automation after a few seconds I Received the very customized response That I can edit and send to my customer This draft has been made using open a Assistant and the file that I uploaded To it since I've triggered both Automations you can see that the inquiry Label is also added to this after the Plugin is installed and configured Adding of the web hooks is actually very Easy in this example I don't have any

Web hooks in my library so I need to add One for this purpose I go to my na10 Instance and here in the web hook note I Need to copy the address of my web hook What is very important I need to set my HTTP method to post I'm going to copy The test URL and I'm going back to my Plug-in dashboard here I will paste the The URL and add the label of this web Hook in this case I want to add the AI Label by triggering this web hook so I Simply added such a label then I need to Refresh my addon let's see right now What kind of data is passed together With the web hook so I'm going to run The web hook Noe in na10 and I'm going To trigger the web Hook from my gmail Addon after I have the confirmation that Web hook has been triggered I can go Back to na10 dashboard as you can see in The output we have all necessary data That we can use to create automations Using na1 or any other automation Software the data such as fret ID Subject or the body of the message are There so we can use it in the further Steps of the workflow I'm going to Activate this Automation and I need to Make a small adjustment because the URL Of the web hook has changed so I need to Delete the test suffix from this URL and Refresh the add-on once again when I Trigger my newly added web hook you can See see that automation works properly

Because when I refresh my inbox you can See that the label notification has been Added to the message that I wanted to Trigger let's see right now how we can Do instant replies using this Gmail Addon so I need to add the new web hook And here I have the other workflow that Is responsible for creating the drafts Of responses with open AI assistants Once again I'm going to use post as HTTP Method but I want to respond with the Respond to web hook note now I'm going To copy the test URL of the web hook Paste it in my Gmail addon and add the Label which in this case is response AI Draft next after I click add web hook And refresh the plugin I can see how it Performs so I will go back to na10 click Listen for test even for the web hook And Trigger the web hook in the Gmail Plug-in after I received the Confirmation that web hook has been Triggered I can go back to n and see What data has been passed to the na10 Once again we received all information Necessary for the creating AI drafts With open AI assistants in the next step Of this workflow which is ask open AI Assistant I simply create the draft of The response to my customer so after I Execute this step I receive in the Output just as before markdown version Of the response from open AI assistant Next I need to convert this response to

HTML so I use for this purpose note Convert to HTML from na10 library and Finally I need to use respond to web Hook note in response body field I need To enter the expression to pass the HTML Version of the draft and in respond with Field I need to select text eventually I Can activate this Automation and go back To my Gmail plug-in to modify the URL of The web hook once again so for the web Hook response AI draft I I need to click Edit and delete the safic test so I Click edit web hook once again and Refresh the plug-in now as my automation Is fully operational I can click trigger For this specific web hook and after a Few seconds I should receive the AI Response from open AI assistants and can Edit for example this message and send To the customer all web hooks in this Plugin are stored within the memory of Your Gmail account but if you want to Create a backup file of your web hooks You can simply export them to the External Json file and then import them This is the root directory of my Google Drive account connected with my Gmail Account and now as I click export web Hooks I should get the file with the web Hooks Library exported from the plugin I Can save this file as a backup or for Example share it with co-workers that Also want to use the same automations And the same web hook endpoints

Importing of web hooks is also very easy I'm going to delete all web hooks from My library and as you remember the Backup file is in my root directory of Google Drive account so I will click Import and just with this one click I Restore all web hooks you can use this Addon also on mobile so in this example I will trigger the automation for my IPhone so here I have the automation Response AI draft and after I trigger it I receive the draft from my automation From na10 let's say right now you you Want to create a draft of the message But now not using the open AI assistants But using AI agents this one gives you The ability to also add the memory to Your email conversations I moved the Product database to superbase and I'm Going to add AI label to one of the Inquiry messages the concept is very Similar to the one that we used with Open AI assistants so first I need to Get the full content of the message so I Use the same Gmail note the message is Now passed not to open AI assistant but To AI agent and the agent has the Specific ability to retrieve the Information using the tools I'm using The open AI functions agent and as a Text I simply paste the content of my Message as subnote I added of course the Open AI chat model and also window Buffer memory so as a session ID I will

Use the Fred ID retrieved from the Gmail As a tool I've created the very custom One it has a name products tool and as a Description I've added information that It's responsible for retrieving the data From database my workflow tool is on the Same canvas so I simply added the Expression that points to the workflow ID every time this tool is triggered it Will pass the query from the user to the Second workflow so to keep this workflow Clean I've created the separate set note Where I added the query parameter this Workflow tool also includes AI agent but This time it's SQL agent so this note is Responsible for retrieving the data from The database and providing information Based on this data since I use superbase As a data source I selected postgress And I also added my postgress Credentials in the last note of this Workflow tool I've created the set note With response parameter because I want To pass back to this mine agent the Output from the SQL agent let me right Now test execute this agent Noe and in The output we receive the response that Is fully based on the data from our Postgress database the other parts of This workflow are almost the same as in Case of open AI functions workflow so First we need to map the parameters and Then we will convert the output from the Agent to HTML next we need to convert

This message to RSC format so for this Purpose we use once again set note Finally we need to encode this message So once again we use the code note with The snippet that we used before Eventually we need to make an HTTP Request to Gmail API so we added the Draft to our GMA tread and we also need To remove the AI label from this message The only difference is the last note Which is chat memory manager so we can See the chat history of the threet in Our Gmail account so when I click it and Select the option to get many messages And execute this note you can see the Human message which is the input message From our Gmail account and AI response Which is the output generated by the AI Agent when we go back to the Gmail inbox And refresh it we can see that AI label Is gone for this message and the draft Has been added just as before we can Click and edit this Draft before we send It to the customer let's run a test Execution of this automation for the one Last time so I will go back to na10 and Activate this workflow currently I have Two inquiries in my inbox the first one Is about smartphones that are available From stock and the second one is about The price of iPhone x for both of those Messages I will add the label AI so the Automation knows that those messages Should be processed since the automation

Runs in 1 minute interval after a moment I can refresh the Inbox and I can see That for both of those inquiries the Draft of message has been added and the AI label is gone with those Automation Workflows and Gmail plug-in you can Really speed up your everyday work with Responding email messages and that's all For today thank you very much for Watching if you like this video please Give a thumbs up subscribe to my channel And to my newsletter link like always is In the description and of course see you In the next video Bye