-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonMail.py
More file actions
32 lines (27 loc) · 932 Bytes
/
pythonMail.py
File metadata and controls
32 lines (27 loc) · 932 Bytes
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
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
fromaddr = "FROM EMAIL"
toaddr = "TO EMAIL"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
def getBody(filename):
with open(str(filename),'r') as myfile:
data=myfile.read().replace('\n', '')
return data
try:
body = "Below is the contents of this file: " + str(sys.argv[1]) + "\n\n\n" + getBody(str(sys.argv[1]))
msg['Subject'] = "Send myself: " + str(sys.argv[1])
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "PASSWORD HERE")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
except (ValueError,IndexError):
print('You have to specify a FILE.')