-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.py
53 lines (41 loc) · 1.61 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
#!/opt/ts/python/2.7/bin/python
# task list generator - utility stuff for sending email
import sys
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def sendFriendlyBotMessage(recipient, text, attachmentText, attachmentSubtype, host='localhost'):
mailFrom= 'The Friendly Task List Robot <[email protected]>'
mailTo= recipient
msg= MIMEMultipart()
msg['Subject'] = 'Task List'
msg['From']= mailFrom
msg['To']= mailTo
msgText= MIMEText(text)
msg.attach(msgText)
attachment= MIMEText(attachmentText, attachmentSubtype, 'utf-8')
if attachmentSubtype=='plain': fn= 'output.txt'
else: fn= 'output.'+attachmentSubtype
attachment.add_header('Content-Disposition', 'attachment', filename=fn)
msg.attach(attachment)
s = smtplib.SMTP(host)
s.sendmail(mailFrom, [mailTo], msg.as_string())
s.quit()
if __name__=='__main__':
sendFriendlyBotMessage('[email protected]', 'the foo of the bar.\n', """<html><head><title>Foo Bar!</title></head><body>blah blah.</body></html>""", 'html')
sys.exit(0)
# the base message container
msg= MIMEText("""Hello, World!
Foo, Bar, Baz.
End of message.
""")
mailFrom= 'The Friendly Task List Robot <[email protected]>'
mailTo= '[email protected]'
msg['Subject'] = 'The foo of the bar'
msg['From'] = mailFrom
msg['To'] = mailTo
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(mailFrom, [mailTo], msg.as_string())
s.quit()