#! /usr/bin/env python3

# This is basically just the example from
# https://developers.google.com/gmail/api/quickstart/python

import base64
from email.mime.text import MIMEText
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from httplib2 import Http
from oauth2client import file, client, tools

# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/gmail.send'

def create_service():
    """ Create a gmail API service from credentials.json

    Returns:
      The service object.
    """
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    # Call the Gmail API
    #message = create_message("<EMAIL>", "<EMAIL>", "Test email2", "This is a big long test message")
    #send_message(service, "me", message)
    return service

def create_message(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}

def send_message(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
      message = (service.users().messages().send(userId=user_id, body=message).execute())
      print("Successfully sent message ID {0}.".format(message['id']))
      return message
  except HttpError as error:
      print("ERROR sending message: {}".format(error))

#! /usr/bin/env python3

# This is basically just the example from
# https://developers.google.com/gmail/api/quickstart/python

import base64
from email.mime.text import MIMEText
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from httplib2 import Http
from oauth2client import file, client, tools

# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/gmail.send'

def create_service():
    """ Create a gmail API service from credentials.json

    Returns:
      The service object.
    """
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    # Call the Gmail API
    #message = create_message("<EMAIL>", "<EMAIL>", "Test email2", "This is a big long test message")
    #send_message(service, "me", message)
    return service

def create_message(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}

def send_message(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
      message = (service.users().messages().send(userId=user_id, body=message).execute())
      print("Successfully sent message ID {0}.".format(message['id']))
      return message
  except HttpError as error:
      print("ERROR sending message: {}".format(error))

#! /usr/bin/env python3 # This is basically just the example from # https://developers.google.com/gmail/api/quickstart/python # If modifying these scopes, delete the file token.json. Create a gmail API service from credentials.json      Returns:       The service object. # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. # Call the Gmail API #message = create_message("<EMAIL>", "<EMAIL>", "Test email2", "This is a big long test message") #send_message(service, "me", message) Create a message for an email.    Args:     sender: Email address of the sender.     to: Email address of the receiver.     subject: The subject of the email message.     message_text: The text of the email message.    Returns:     An object containing a base64url encoded email object. Send an email message.    Args:     service: Authorized Gmail API service instance.     user_id: User's email address. The special value "me"     can be used to indicate the authenticated user.     message: Message to be sent.    Returns:     Sent Message.