Gmail Connector for Python How to Send a New Message for the specified User
Introduction
In this article we will delve deeper into Gmail and Python integration, and will learn how to send a new message for the specified user. We are continuing from where we left off. By this time, you must have installed ODBC PowerPack, created ODBC Data Source, and configured authentication settings in your Gmail account .
So, let's not waste time and begin.
Use Query Builder to generate SQL query
-
The first thing you have to do is open Query Builder:
ZappySys API Driver - GmailRead / search Gmail messages, download attachments, send mail and more using Gmail REST API.GmailDSN
-
Then simply select the Send a New Message for the specified User endpoint (action).
-
Continue by configuring the Required parameters. You can also set optional parameters too.
-
Move on by hitting Preview Data button to preview the results.
-
If you see the results you need, simply copy the generated query:
-
That's it! You can use this query in Python.
Let's not stop here and explore SQL query examples, including how to use them in Stored Procedures and Views (virtual tables) in the next steps.
SQL query examples
Use these SQL queries in your Python data source:
How to Send a message
This example shows how to send an HTML email using Gmail REST API. It has multiple recepients, cc and bcc email ids. You can also set Reply-To email (other than From)
INSERT INTO Messages(From,To,Cc,Bcc,ReplyTo,Subject,Body,MimeType)
VALUES('me'
,'bob@zappysys.com,sam@zappysys.com'
,'some-cc1@zappysys.com,some-cc2@zappysys.com'
,'some-bcc1@zappysys.com,some-bcc1@zappysys.com'
,'reply-to@gmail.com'
,'This is sample HTML email'
,'This is <u>Bob''s</u> HTML email. Sent on <b>*** <<FUN_NOW>> ** </b>'
,'text/html' --or use text/plain for Plain format
)
/*
OR call using SELECT statement
SELECT * FROM send_message
WITH(From='me'
,To='bob@zappysys.com,sam@zappysys.com'
,Cc='some-cc1@zappysys.com,some-cc2@zappysys.com'
,Bcc='some-bcc1@zappysys.com,some-bcc1@zappysys.com'
,ReplyTo='reply-to@gmail.com'
,Subject='This is sample HTML email'
,Body='This is <u>Bob''s</u> HTML email. Sent on <b>*** <<FUN_NOW>> ** </b>'
,MimeType='text/html' --or use text/plain for Plain format
)
*/
How to Send a message
This example shows how to send an HTML email using Gmail REST API. It has multiple recepients, cc and bcc email ids. You can also set Reply-To email (other than From)
INSERT INTO Messages(From,To,Cc,Bcc,ReplyTo,Subject,Body,MimeType)
VALUES('me'
,'bob@zappysys.com,sam@zappysys.com'
,'some-cc1@zappysys.com,some-cc2@zappysys.com'
,'some-bcc1@zappysys.com,some-bcc1@zappysys.com'
,'reply-to@gmail.com'
,'This is sample HTML email'
,'This is <u>Bob''s</u> HTML email. Sent on <b>*** <<FUN_NOW>> ** </b>'
,'text/html' --or use text/plain for Plain format
)
/*
OR call using SELECT statement
SELECT * FROM send_message
WITH(From='me'
,To='bob@zappysys.com,sam@zappysys.com'
,Cc='some-cc1@zappysys.com,some-cc2@zappysys.com'
,Bcc='some-bcc1@zappysys.com,some-bcc1@zappysys.com'
,ReplyTo='reply-to@gmail.com'
,Subject='This is sample HTML email'
,Body='This is <u>Bob''s</u> HTML email. Sent on <b>*** <<FUN_NOW>> ** </b>'
,MimeType='text/html' --or use text/plain for Plain format
)
*/
send_message endpoint belongs to
MessageAttachments
, Messages
, MyMessages
table(s), and can therefore be used via those table(s).
Stored Procedures and Views
Create Custom Stored Procedure
You can create procedures to encapsulate custom logic and then only pass handful parameters rather than long SQL to execute your API call.
Steps to create Custom Stored Procedure in ZappySys Driver. You can insert Placeholders anywhere inside Procedure Body. Read more about placeholders here
-
Go to Custom Objects Tab and Click on Add button and Select Add Procedure:
-
Enter the desired Procedure name and click on OK:
-
Select the created Stored Procedure and write the your desired stored procedure and Save it and it will create the custom stored procedure in the ZappySys Driver:
Here is an example stored procedure for ZappySys Driver. You can insert Placeholders anywhere inside Procedure Body. Read more about placeholders here
CREATE PROCEDURE [usp_get_orders] @fromdate = '<<yyyy-MM-dd,FUN_TODAY>>' AS SELECT * FROM Orders where OrderDate >= '<@fromdate>';
-
That's it now go to Preview Tab and Execute your Stored Procedure using Exec Command. In this example it will extract the orders from the date 1996-01-01:
Exec usp_get_orders '1996-01-01';
-
Let's generate the SQL Server Query Code to make the API call using stored procedure. Go to Code Generator Tab, select language as SQL Server and click on Generate button the generate the code.
As we already created the linked server for this Data Source, in that you just need to copy the Select Query and need to use the linked server name which we have apply on the place of [MY_API_SERVICE] placeholder.
SELECT * FROM OPENQUERY([LS_TO_GMAIL_IN_GATEWAY], 'EXEC usp_get_orders @fromdate=''1996-07-30''')
-
Now go to SQL served and execute that query and it will make the API call using stored procedure and provide you the response.
Create Custom Virtual Table
ZappySys API Drivers support flexible Query language so you can override Default Properties you configured on Data Source such as URL, Body. This way you don't have to create multiple Data Sources if you like to read data from multiple EndPoints. However not every application support supplying custom SQL to driver so you can only select Table from list returned from driver.
If you're dealing with Microsoft Access and need to import data from an SQL query, it's important to note that Access doesn't allow direct import of SQL queries. Instead, you can create custom objects (Virtual Tables) to handle the import process.
Many applications like MS Access, Informatica Designer wont give you option to specify custom SQL when you import Objects. In such case Virtual Table is very useful. You can create many Virtual Tables on the same Data Source (e.g. If you have 50 URLs with slight variations you can create virtual tables with just URL as Parameter setting.
-
Go to Custom Objects Tab and Click on Add button and Select Add Table:
-
Enter the desired Table name and click on OK:
-
And it will open the New Query Window Click on Cancel to close that window and go to Custom Objects Tab.
-
Select the created table, Select Text Type AS SQL and write the your desired SQL Query and Save it and it will create the custom table in the ZappySys Driver:
Here is an example SQL query for ZappySys Driver. You can insert Placeholders also. Read more about placeholders here
SELECT "ShipCountry", "OrderID", "CustomerID", "EmployeeID", "OrderDate", "RequiredDate", "ShippedDate", "ShipVia", "Freight", "ShipName", "ShipAddress", "ShipCity", "ShipRegion", "ShipPostalCode" FROM "Orders" Where "ShipCountry"='USA'
-
That's it now go to Preview Tab and Execute your custom virtual table query. In this example it will extract the orders for the USA Shipping Country only:
SELECT * FROM "vt__usa_orders_only"
-
Let's generate the SQL Server Query Code to make the API call using stored procedure. Go to Code Generator Tab, select language as SQL Server and click on Generate button the generate the code.
As we already created the linked server for this Data Source, in that you just need to copy the Select Query and need to use the linked server name which we have apply on the place of [MY_API_SERVICE] placeholder.
SELECT * FROM OPENQUERY([LS_TO_GMAIL_IN_GATEWAY], 'EXEC [usp_get_orders] ''1996-01-01''')
-
Now go to SQL served and execute that query and it will make the API call using stored procedure and provide you the response.
Send a New Message for the specified User in Python
-
Python code to get the data:
')
-
When you run the code it will make the API call and read the data:
-
Here is Python program's code in text format:
import pyodbc conn = pyodbc.connect('DSN=') cursor = conn.cursor() #execute query to fetch data from API service cursor.execute("SELECT id,title FROM products") row = cursor.fetchone() while row: print(row) row = cursor.fetchone() ##For loop example #for row in cursor: # print(row)
More actions supported by Gmail Connector
Learn how to perform other actions directly in Python with these how-to guides:
- Download Message Attachments (With search condition)
- Get a Specific Attachment included in a Specific Message for the Current User
- Get a Specific Message for a Specific User
- Get a Specific Message for the Current User
- Get labels
- Get List of Messages for a Specific User
- Get List of Messages for the Current User
- Get message attachment for user
- Get Message Attachments (With search condition)
- Make Generic API Request
- Make Generic API Request (Bulk Write)