-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_email.py
More file actions
72 lines (65 loc) · 2.47 KB
/
send_email.py
File metadata and controls
72 lines (65 loc) · 2.47 KB
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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import argparse
import os
import getpass
def send_email(args):
msg = MIMEMultipart()
msg['From'] = args.user
msg['To'] = args.to
msg['Subject'] = args.subject
for file_name in args.txt_files:
with open(file_name, 'r') as f:
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_name))
msg.attach(attachment)
for file_name in args.png_files:
with open(file_name, 'rb') as f:
attachment = MIMEImage(f.read(), name=os.path.basename(file_name))
msg.attach(attachment)
if not args.body:
body = 'No desired body'
content = MIMEText(body, 'plain')
msg.attach(content)
else:
with open(args.body, 'r') as f:
body = MIMEText(f.read())
f.close()
msg.attach(body)
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(args.user, args.password)
server.sendmail(args.user, args.to, msg.as_string())
server.close()
print('Email sent!')
def main(args):
send_email(args)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--user', type=str, required=True,
help='Email address of the user')
parser.add_argument('--password', type=str,
default='',
help='Password for email account of user')
parser.add_argument('--to', type=str, required=True,
help='Email address of the receiver')
parser.add_argument('--subject', type=str,
default='Email sent from the terminal',
help='Subject of the email. Default value of \'Email send from the terminal\'')
parser.add_argument('--body', type=str,
default='',
help='File containing body of the message to send to the receiver')
parser.add_argument('--attach_png', action='append', dest='png_files',
default=[],
help='png file to be sent to the receiver.')
parser.add_argument('--attach_txt', action='append', dest='txt_files',
default=[],
help='txt file to be sent to the receiver.')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass('Password: ')
main(args)