Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- import pickle
- import os.path
- from googleapiclient.discovery import build
- from apiclient import errors
- from google_auth_oauthlib.flow import InstalledAppFlow
- from google.auth.transport.requests import Request
- from email.mime.text import MIMEText
- import base64
- # If modifying these scopes, delete the file token.pickle.
- SCOPES = ['https://www.googleapis.com/auth/gmail.send']
- def credentials():
- creds = None
- # The file token.pickle stores the user's access and refresh tokens, and is
- # created automatically when the authorization flow completes for the first
- # time.
- if os.path.exists('token.pickle'):
- with open('token.pickle', 'rb') as token:
- creds = pickle.load(token)
- # If there are no (valid) credentials available, let the user log in.
- if not creds or not creds.valid:
- if creds and creds.expired and creds.refresh_token:
- creds.refresh(Request())
- else:
- flow = InstalledAppFlow.from_client_secrets_file(
- 'credentials.json', SCOPES)
- creds = flow.run_local_server(port=0)
- # Save the credentials for the next run
- with open('token.pickle', 'wb') as token:
- pickle.dump(creds, token)
- print('Creds dumped to file.')
- service = build('gmail', 'v1', credentials=creds)
- return service
- # get the input data
- def get_user_input():
- overall_day_mood = input('How was today? ')
- accomplished_tasks = []
- amount_of_tasks_required = 3
- for _ in range(amount_of_tasks_required):
- task = input('What did you accomplish today? ')
- accomplished_tasks.append(task)
- tasks = overall_day_mood, accomplished_tasks
- return tasks
- #format the data into an spaced string
- def format_for_email(data):
- formatted_email_string = "Hey Erin! \n\n" \
- f"Today was {data[0]}. Here's a list of what I accomplished: \n\n" \
- f" - {data[1][0]} \n" \
- f" - {data[1][1]} \n" \
- f" - {data[1][2]} \n\n" \
- f"Thanks,\nPatrick"
- return formatted_email_string
- # sender & destination
- my_email = 'me'
- erin_email = 'efallon@sas.upenn.edu'
- # create our gmail message
- def create_message(sender, to, subject, message_text):
- message = MIMEText(message_text)
- message['to'] = to
- message['from'] = sender
- message['subject'] = subject
- return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
- # the the message we created
- def send_message(service, user_id, message):
- try:
- message = (service.users().messages().send(userId = user_id, body=message).execute())
- print(f'Message sent successfully. ID: {message["id"]}')
- return message
- except errors.HttpError as error:
- print(f'An error occurred: {error}')
- shift_data = get_user_input()
- formatted_email = format_for_email(shift_data)
- print('.')
- print(formatted_email)
- print('.')
- gmail = credentials()
- final_email = create_message(my_email,erin_email,f'Shift Report: {datetime.date.today()}',formatted_email)
- user_wants_to_send = False
- while not user_wants_to_send:
- if input('Do you want to send? ') == 'y':
- send_message(gmail, 'me', final_email)
- user_wants_to_send = True
- else:
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement