forked from binerry/RaspberryPi
-
Notifications
You must be signed in to change notification settings - Fork 22
/
mail.py
executable file
·59 lines (48 loc) · 1.43 KB
/
mail.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
#!/usr/bin/env python
import sys
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
# config
server = "send.example.com"
port = 587
send_from = "[email protected]"
password = "verynicepassword"
send_to = "[email protected]"
subject = "Message from your answering machine"
def send_mail(text, f):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
try:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)
except IOError as e:
print "Can not read file \""+f+"\": I/O error({0}): {1}".format(e.errno, e.strerror)
except:
print "Can not read file", sys.exc_info()[0]
raise
s = smtplib.SMTP(server, port)
# s.debuglevel = 1
s.ehlo()
s.starttls()
# s.ehlo
s.login(send_from, password)
s.sendmail(send_from, send_to, msg.as_string())
s.quit()
# smtp.close()
if len(sys.argv) == 3:
send_mail(sys.argv[1],sys.argv[2])
else:
print"provide two arguments: \"text\" \"file\""