-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailService.py
61 lines (46 loc) · 1.68 KB
/
MailService.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
# MailService.py
#
# Handles the connection to mail server and recievers
#
# @author Orhun Dalabasmaz
# @since Dec, 2015
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from config.MailConfig import *
SMTP_SSL_PORT = 465
def connect_to_mail_server():
if server_port == SMTP_SSL_PORT:
mail_server = smtplib.SMTP_SSL(server_address, server_port)
else:
mail_server = smtplib.SMTP(server_address, server_port)
mail_server.set_debuglevel(mail_server_debug_enabled)
mail_server.starttls()
mail_server.login(sender_username, sender_password)
return mail_server
def add_signature_to_mail(mail_content):
return mail_content.replace("</body></html>",
"</br>" + mail_signature + "</body></html>")
def send_mail(mail_subject, mail_content):
print 'Sending mail...'
mail_content = add_signature_to_mail(mail_content)
mail_server = connect_to_mail_server()
# Create the message
msg = MIMEMultipart()
msg['From'] = sender_address
msg['To'] = ", ".join(receiver_addresses)
msg['CC'] = ", ".join(receiver_addresses_cc)
msg['BCC'] = ", ".join(receiver_addresses_bcc)
msg['Subject'] = mail_subject
msg.attach(MIMEText(mail_content, 'html'))
try:
mail_server.sendmail(sender_address, receiver_addresses, msg.as_string())
print "Mail sent successfully."
print msg.as_string()
except EOFError:
print "Error occurred when sending mail."
mail_server.quit()
def send_success_mail(content):
send_mail(usual_mail_subject, content)
def send_failure_mail(content):
send_mail(failure_mail_subject, content)