-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
81 lines (75 loc) · 2.32 KB
/
send_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
import argparse
import json
import smtplib
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def main(args) -> int:
if args.recipients_path is not None:
# load json as a list
with open(args.recipients_path, 'r') as f:
recipients = json.load(f)
email_recipients = ', '.join(recipients)
else:
email_recipients = args.recipient
# Create a message
message = MIMEMultipart()
message['From'] = args.sender
message['Subject'] = args.subject
message['To'] = email_recipients
if args.body_path is not None:
with open(args.body_path, 'r') as f:
body = f.read()
else:
body = ''
# Attach the body of the email
message.attach(MIMEText(body, 'plain'))
# Connect to the SMTP server
with smtplib.SMTP(args.smtp_host, args.smtp_port) as server:
server.starttls()
server.login(args.sender, args.password)
server.send_message(msg=message)
print('An email has been sent.')
return 0
def setup_parser():
parser = argparse.ArgumentParser(description='')
parser.add_argument(
'--smtp_host',
help='IP or domain name of the server.',
type=str,
required=True)
parser.add_argument(
'--smtp_port',
help='Port number of the server.',
type=int,
required=True)
parser.add_argument(
'--sender',
help='Address of the sender.',
type=str,
required=True)
parser.add_argument(
'--password', help='Password of the sender.', type=str, required=True)
parser.add_argument(
'--recipients_path',
help='Path to a json file for address list of the recipients.',
type=str)
parser.add_argument(
'--recipient',
help='Email address of only one recipient. ' +
'Valid when recipients_path is None.',
type=str)
# Subject, body
parser.add_argument(
'--subject', help='Subject of the email.', type=str, required=True)
parser.add_argument(
'--body_path',
help='Path to a txt file, which is the body of email.',
type=str,
required=False)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = setup_parser()
ret_val = main(args)
sys.exit(ret_val)