-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailer.py
79 lines (63 loc) · 2.79 KB
/
mailer.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
## Checked for 3.7 ##
import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# This function creates a new file object. (email encoded)
def create_file(filename, filetype, filecontent):
'''filetype is the end string without a period, ex. for "index.html", filetype is 'html'.
filename is without the filetype at the end.'''
# Create a text file.
part = MIMEBase('application', "octet-stream")
part.set_payload(filecontent) # This is the text in the file.
encoders.encode_base64(part)
part.add_header( 'Content-Disposition', 'attachment; filename="{}.{}"'.format(filename, filetype) )
return part
# This function sends an email from an sfu email.
# attachment_list is a list of the attachment class. -1 means no attachments. attachment_list must be a list.
# For cc email, if -1 then none, else it holds the addresses to cc. (list)
def send_sfu_email(sender_name, reciever_email, subject_text, body_text, attachment_list=-1, cc_list=-1):
'''this function sends an email from an sfu email.
attachment_list is a list of the attachment class. -1 means no attachments. attachment_list must be a list.'''
### construct the message and sending information.
sender = "{}@bccfe.ca".format(sender_name)
reciever = reciever_email
msg = MIMEMultipart()
msg.attach(MIMEText(body_text)) # attach the text to the email.
msg['Subject'] = subject_text
msg['From'] = sender
msg['To'] = reciever
### attach a file if there are any.
if attachment_list != -1:
for attachment in attachment_list:
msg.attach(attachment)
send_to = reciever
### cc the people if need be.
if cc_list != -1:
ccs = ", ".join(cc_list) # people to cc to.
msg['Cc'] = ccs
send_to = [reciever, ccs] # send the email to the cced people too.
### attempt to send the email.
try:
### for some reason the newer smtp server thinks we aren't part of sfu
### so we have to use the old smtp server. -> if emails stop working this is
### very likely the case.
smtpobj = smtplib.SMTP(os.environ['SMTP_MAIL_SERVER'], os.environ['SMTP_MAIL_PORT'])
smtpobj.starttls()
smtpobj.ehlo()
smtpobj.login(os.environ['SMTP_MAIL_USER'], os.environ['SMTP_MAIL_PASSWORD'])
smtpobj.sendmail(sender, send_to, msg.as_string())
return 0
except Exception as e:
print ( "Error: unable to send email. \nerror: {}".format(e) )
return 1
### EXAMPLE CODE ###
'''
txt_file = create_file("great_filename", 'txt', "This is just some text in a file.")
msg_body = "This is the body of the email. There is some text here. There is also an attached file."
if send_sfu_email("test_email_name", "[email protected]", "You've Got Some Mail!", msg_body, [txt_file]) == 1:
print "Error: unable to send email"
else:
print "Successfully sent email"
'''