-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailer.py
executable file
·91 lines (73 loc) · 2.53 KB
/
emailer.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
82
83
84
85
86
87
88
89
90
91
import os
import sys
import smtplib
import string
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Emailer:
"""Base class for objects that need to email stuff."""
def send_msg( self, from_addr, to_addr, subject, message, attachments=[], msgtype="plain" ):
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = string.join(to_addr,", ")
msg.attach(MIMEText(message))
for f in attachments:
fp=open(f,"rb")
msg.attach(MIMEApplication(fp.read(),
Content_Disposition='attachment; filename="%s"' % os.path.basename(f),
Name=os.path.basename(f)))
s = smtplib.SMTP('hci-mail.hci.utah.edu')
# Send the message.
retval = s.sendmail( from_addr, to_addr, msg.as_string() )
s.quit()
return retval
def OLD_send_msg( self, from_addr, to_addr, subject, message, attachments=[], msgtype="plain" ):
# Create the message.
outer = MIMEMultipart()
# Initialize from, to, and subject.
outer['Subject'] = subject
outer['From'] = from_addr
outer['To'] = string.join(to_addr,", ")
# Initialize the main content of the message.
outer.attach(MIMEText(message,msgtype))
# Process the attachments.
for fname in attachments:
fp = open(fname,"rb")
msg = MIMEText(fp.read())
fp.close()
msg.add_header("Content-Disposition","attachment",filename=os.path.basename(fname))
outer.attach(msg)
# Connect to the smtp server.
#s = smtplib.SMTP('EMAIL1.hci.utah.edu')
s = smtplib.SMTP('hci-mail.hci.utah.edu')
# Send the message.
retval = s.sendmail( from_addr, to_addr, outer.as_string() )
s.quit()
return retval
# Example of use:
def main():
if len(sys.argv) != 4:
usage()
sys.exit(1)
flowcell = sys.argv[1]
attachments = sys.argv[2:]
for file in attachments:
if not os.path.exists(file):
print "File '%s' not found." % file
sys.exit(1)
from_addr = "[email protected]"
to_addr = ['[email protected]',
]
subject = "Flow cell %s processing complete." % flowcell
message_content = """Data processing for flow cell %s is complete.
The GERALD summary and sample tracking forms are attached.
If you no longer wish to receive this automated email message
please contact Brett Milash ([email protected]).
""" % flowcell
Emailer().send_msg( from_addr, to_addr, subject, message_content, attachments )