Skip to content
Jeremy Ephron Barenholtz edited this page Sep 13, 2020 · 7 revisions

simplegmail.Gmail

simplegmail.Gmail(client_secret_file='client_secret.json')

The Gmail class which serves as the entrypoint for the Gmail service API.

Parameters:

  • client_secret_file: str
    The name of the user's client secret file. Default to "client_secret.json" if not provided.

Gmail.send_message(sender, to, subject='', msg_html=None, msg_plain=None, cc=None, bcc=None, attachments=None, signature=False, user_id='me')

Sends an email.

Parameters:

  • sender: str
    The email address the message is being sent from.
  • to: str
    The email address the message is being sent to.
  • subject: str
    The subject line of the email.
  • msg_html: str
    The HTML message of the email. This is displayed instead of the plaintext method by most email clients if provided.
  • msg_plain: str
    The plain text alternate message of the email. This is often displayed on slow or old browsers, or if the HTML message is not provided.
  • cc: List[str]
    The list of email addresses to be cc'd.
  • bcc: List[str]
    The list of email addresses to be bcc'd.
  • attachments: List[str]
    The list of attachment file names.
  • signature: bool
    Whether the account signature should be added to the message.
  • user_id: str
    The address of the sending account. 'me' for the default address associated with the account.

Returns:

  • simplegmail.Message: The Message object representing the sent message.

Raises:

  • googleapiclient.errors.HttpError: There was an error executing the HTTP request.

Gmail.get_messages(self, user_id='me', labels=None, query='', attachments='reference', include_spam_trash=False)

Gets messages from your account.

Parameters:

  • user_id: str
    The user's email address. By default, the authenticated user.
  • label: Optional[List[Label]]
    Labels that messages must match.
  • query: str
    A Gmail query to match.
  • attachments: Union['ignore', 'reference', 'download']
    Accepted values are 'ignore' which completely ignores all attachments, 'reference' which includes attachment information but does not download the data, and 'download' which downloads the attachment data to store locally. Default 'reference'.
  • include_spam_trash: bool
    Whether to include messages from spam or trash.

Returns:

  • List[simplegmail.Message]: A list of message objects.

Raises:

  • googleapiclient.errors.HttpError: There was an error executing the HTTP request.

The following convenience functions are provided with the same interface as get_messages:

  • get_starred_messages
  • get_important_messages
  • get_unread_messages
  • get_drafts
  • get_sent_messages
  • get_unread_inbox (no include spam_trash parameter)
  • get_trash_messages (no include_spam_trash parameter)
  • get_spam_messages (no include_spam_trash parameter)

Gmail.list_labels(self, user_id='me')

Retrieves all labels for the specified user.

These Label objects are to be used with other functions like Message.modify_labels().

Parameters:

  • user_id: str
    The user's email address. By default, the authenticated user.

Returns:

  • List[simplegmail.Label]: A list of message objects.

Raises:

  • googleapiclient.errors.HttpError: There was an error executing the HTTP request.
Clone this wiki locally