""" Copyright 2019 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ # [START gmail_create_draft] import base64 from email.message import EmailMessage import google.auth from googleapiclient.discovery import build from googleapiclient.errors import HttpError def gmail_create_draft(): """Create and insert a draft email. Print the returned draft's message and id. Returns: Draft object, including draft id and message meta data. Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for the application. """ creds, _ = google.auth.default() try: # create gmail api client service = build("gmail", "v1", credentials=creds) message = EmailMessage() message.set_content("This is automated draft mail") message["To"] = "gduser1@workspacesamples.dev" message["From"] = "gduser2@workspacesamples.dev" message["Subject"] = "Automated draft" # encoded message encoded_message = base64.urlsafe_b64encode(message.as_bytes()).decode() create_message = {"message": {"raw": encoded_message}} # pylint: disable=E1101 draft = ( service.users() .drafts() .create(userId="me", body=create_message) .execute() ) print(f'Draft id: {draft["id"]}\nDraft message: {draft["message"]}') except HttpError as error: print(f"An error occurred: {error}") draft = None return draft if __name__ == "__main__": gmail_create_draft() # [END gmail_create_draft]