-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_simple_template_email.py
124 lines (104 loc) · 4.99 KB
/
send_simple_template_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Main script used to send emails. Allows user to specify the attachment suffix, email suffix, and maximum image size. The contents of the email are specified in the config.py file.
For more information on script parameter run: python3 send_emails.py --help. An example of running the script is:
python3 send_emails.py --attachment_suffix png jpg --email_suffix txt --max_image_size 1024
"""
# Arguments to specify the attachment and email suffix, as well as the maximum image size
from utils import get_gmail_service, create_message, message2bytes, press_Yn_to_continue
from config import txt_message_royalties_delayed, gmail_address, subject_line_delayed_report, scopes, credenciales, port, max_emails
from time import sleep
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import os.path
import csv
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--email_suffix', nargs='+', default='txt',
help='Suffix of the email to send (default: "txt")')
parser.add_argument('--max_image_size', type=int, default=1024,
help='Maximum size of the image to send')
parser.add_argument("--csv_file_path", type=str,
help="Path to the CSV file containing email-identifier mapping.")
args = parser.parse_args()
# External libraries
# Internal config and utility files
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# 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.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credenciales 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(
'credenciales.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credenciales for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
# Call the Gmail API
service = build('gmail', 'v1', credentials=creds)
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
return
print('Labels:')
for label in labels:
print(label['name'])
except HttpError as error:
# TODO(developer) - Handle errors from gmail API.
print(f'An error occurred: {error}')
if __name__ == '__main__':
# -- (i) Set up the service connection -- #
# Note, this will open a browser window to authenticate
service = get_gmail_service(
scopes=scopes, credentials=credenciales, port=port)
email_user_mapping2 = {}
# Force the user to confirm they would like to continue after
press_Yn_to_continue()
# -- (iv) Loop over each recipient, and send message -- #
# for i, email in enumerate(emails):
with open(args.csv_file_path, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for i, row in enumerate(csv_reader):
identifier = ""
# email = ""
if len(row) == 2:
identifier, email = row
print(f'Company name is: {identifier}')
print(f'Email is: {email}')
email_user_mapping2[identifier] = email
# -- (ii) Create the message -- #
message_formatted = txt_message_royalties_delayed.format(
company_name=identifier)
message = create_message(message=message_formatted, email_from=gmail_address, email_to=gmail_address,
subject=subject_line_delayed_report, max_image_size=args.max_image_size)
# Update the recipient email
del message['To']
message['To'] = {email}
print(f'Sending to {email}')
# Convert the message to a raw byte
raw_message = message2bytes(message)
# Send the message
send_message = (service.users().messages().send(
userId="me", body=raw_message).execute())
if not 'SENT' in send_message['labelIds']:
print(f'Email not sent to {email}')
if (i+1) % max_emails == 0:
print(f'Pausing for 1 minute after sending {i+1} emails')
sleep(60)
print('~~~ End of send_emails.py ~~~')