Reference

Gmail

class gmailer.gmail.Gmail

This class defines all the core functional methods of Gmailer

exception StateError(message=None, error=None)

When state of the request is not matched with state from request, this exception is raised

Parameters
  • message (str) – Subject of the email send

  • error (Exception) – Body of the email send

exception UnauthorizedAPIError(message=None, error=None)

When app try to access API without proper authorization, this exception is raised

Parameters
  • message (str) – Subject of the email send

  • error (Exception) – Body of the email send

activated

A flag which indicate if Gmail API is authorized to be used

static add_settings()

Add each setting in to Django settings if they dont exists inside Django Settings

Return type

bool

authorize()

Generate authorization url and state from Flow instance

Returns

A tuple of authorizationurl and state

flow

Store a Flow instance using client secret and scope list

revoke()

Revoke the data which is used by Gmail API to work

send_mail(subject, message, recipient_list, html=False)

Send email using Gmail API service

Parameters
  • subject (str) – Subject of the email send

  • message (str) – Body of the email send

  • recipient_list (list) – A list of email addresses to send this email

  • html (bool) – If True, message created will be in HTML formate. Otherwise this message created will be plain text. Default is False

Return type

dict

test_mail(subject=None, message=None, recipient_list=None)

Send a test email using Gmail API service. If recipient_list is parameter not provided, mail is sent to sender itself.

Parameters
  • subject (str) – Subject of the email send

  • message (str) – Body of the email send

  • recipient_list (list) – A list of email addresses to send this email

verify(request)

This method verify authorizated data and generate access token. It also generate a gmail api service as class property. It returns a dictionary of name, email id and credentials from Flow instance.

Parameters

request – A request object received from requested view

Return type

dict

Views

gmailer.views.auth(request)

Generate Google OAuth authorization url and state. State is stored inside session and view is redirected to the authorization url.

Parameters

request – A request object received from requested view

Return type

Redirection to authorization url

gmailer.views.revoke(request)

Revoke the Google OAuth authorization.

Parameters

request – A request object received from requested view

Return type

JsonResponse instance

gmailer.views.test_send_mail(request)

Test the Gmail API by sending an email to self.

Parameters

request – A request object received from requested view

Return type

JsonResponse instance

gmailer.views.verify(request)

Verify code and state received from request.

Parameters

request – A request object received from requested view

Return type

JsonResponse instance

Settings

GMAIL_USER

Default: Django Mail Admin

This settings is the name of admin which shows up inside mail received by user

GMAIL_SECRET

Default: google_client_secret.json

The Google OAuth client secret obtained from Gmail API Setup

GMAIL_SCOPES

Default: [ https://www.googleapis.com/auth/gmail.metadata, https://www.googleapis.com/auth/gmail.send ]

Gmail scopes express the permissions you need to authorize for your app and send mails

GMAIL_REDIRECT

Default: http://localhost:8000/gmailer/verify

The redirect URI is the endpoint to which the OAuth 2.0 server can send responses.