Gmail

GmailUtility

class gwrappy.gmail.GmailUtility(json_credentials_path, client_id, **kwargs)[source]

Initializes object for interacting with Bigquery API.

Parameters:
  • client_secret_path – File path for client secret JSON file. Only required if credentials are invalid or unavailable.
  • json_credentials_path – File path for automatically generated credentials.
  • client_id – Credentials are stored as a key-value pair per client_id to facilitate multiple clients using the same credentials file. For simplicity, using one’s email address is sufficient.
  • max_retries (integer) – Argument specified with each API call to natively handle retryable errors.
get_profile()[source]

Abstraction of users().getProfile() method. [https://developers.google.com/gmail/api/v1/reference/users/getProfile]

Returns:Dictionary object representing authenticated profile.
get_message(id, format='full')[source]

Abstraction of users().messages().get() method. [https://developers.google.com/gmail/api/v1/reference/users/messages/get]

Parameters:
  • id (string) – Unique message id.
  • format (string) – Acceptable values are ‘full’, ‘metadata’, ‘minimal’, ‘raw’
Returns:

Dictionary object representing message resource.

get_draft(id, format='full')[source]

Abstraction of users().drafts().get() method. [https://developers.google.com/gmail/api/v1/reference/users/drafts/get]

Parameters:
  • id (string) – Unique message id.
  • format (string) – Acceptable values are ‘full’, ‘metadata’, ‘minimal’, ‘raw’
Returns:

Dictionary object representing draft resource.

list_messages(max_results=None, full_messages=True, **kwargs)[source]

Abstraction of users().messages().list() method with inbuilt iteration functionality. [https://developers.google.com/gmail/api/v1/reference/users/messages/list]

Parameters:
  • max_results (integer) – If None, all results are iterated over and returned.
  • full_messages (boolean) – Convenience toggle to call self.get_message() for each message returned.
  • q – A query for filtering the file results. Can be generated from gwrappy.gmail.utils.generate_q
Returns:

List of dictionary objects representing message resources.

list_drafts(max_results=None, full_messages=True, **kwargs)[source]

Abstraction of users().drafts().list() method with inbuilt iteration functionality. [https://developers.google.com/gmail/api/v1/reference/users/drafts/list]

Parameters:
  • max_results (integer) – If None, all results are iterated over and returned.
  • full_messages (boolean) – Convenience toggle to call self.get_draft() for each message returned.
  • q – A query for filtering the file results. Can be generated from gwrappy.gmail.utils.generate_q
Returns:

List of dictionary objects representing draft resources.

create_draft(sender, to, subject, message_text, attachment_file_paths=None)[source]

New draft based on input parameters.

Parameters:
  • sender (string) – Name of sender
  • to (string or list) – One or more recipients.
  • subject – Subject text
  • message_text (string, dict, or list of dicts) – Message string, or one or more dict representations of message parts. If dict, keys required are type and text.
  • attachment_file_paths (string or list) – One or more file paths of attachments.
Returns:

API response.

send_draft(draft_id)[source]

Send unsent draft.

Parameters:draft_id – Unique draft id.
Returns:API Response.
send_email(sender, to, subject, message_text, attachment_file_paths=None)[source]

Send new message based on input parameters.

Parameters:
  • sender (string) – Name of sender
  • to (string or list) – One or more recipients.
  • subject – Subject text
  • message_text (string, dict, or list of dicts) – Message string, or one or more dict representations of message parts. If dict, keys required are type and text.
  • attachment_file_paths (string or list) – One or more file paths of attachments.
Returns:

API response.

get_attachments(message_id)[source]

Get message attachments.

Parameters:message_id – Unique message id. Can be retrieved and iterated over from list_messages() method.
Returns:Dictionary with parsed dates and attachment_data (ready to write to file!). Duplicate handling and overwriting logic should be handled externally when iterating over list of messages.

Misc Classes/Functions

gwrappy.gmail.utils.generate_q(**kwargs)[source]

Generate query for searching messages. [https://support.google.com/mail/answer/7190]

Parameters:kwargs – Key-Value pairs. Descriptive flags like has or is can take lists. Otherwise, list values would be interpreted as “OR”.
Returns:String representation of search q
gwrappy.gmail.utils.list_to_html(data, has_header=True, table_format=None)[source]

Convenience function to convert tables to html for attaching as message text.

Parameters:
  • data (list of lists) – Table data
  • has_header (boolean) – Flag whether data contains a header in the first row.
  • table_format (dictionary) – Dictionary representation of formatting for table elements. Eg. {‘table’: “border: 2px solid black;”}
Returns:

String representation of HTML.