1 |
1 |
import json
|
2 |
|
import smtplib
|
3 |
|
from email.mime.text import MIMEText
|
|
2 |
|
|
3 |
from smtplib import SMTP
|
|
4 |
from email.mime import text
|
|
5 |
from email.header import Header
|
|
6 |
from email.utils import parseaddr, formataddr
|
4 |
7 |
|
5 |
8 |
from django.conf import settings
|
6 |
9 |
import amqp_connection
|
... | ... | |
23 |
26 |
amqp_connection.send(json.dumps(msg), settings.EXCHANGE_API, routekey)
|
24 |
27 |
|
25 |
28 |
|
26 |
|
def send (frm = settings.SYSTEM_EMAIL_ADDR,
|
27 |
|
to = None, subject = None, body = None):
|
|
29 |
def send (sender = settings.SYSTEM_EMAIL_ADDR,
|
|
30 |
recipient = None, subject = None, body = None):
|
28 |
31 |
"""
|
29 |
32 |
Connect to the email server configured in settings.py
|
30 |
33 |
and send the email.
|
31 |
34 |
|
|
35 |
All arguments should be Unicode strings (plain ASCII works as well).
|
|
36 |
|
|
37 |
Only the real name part of sender and recipient addresses may contain
|
|
38 |
non-ASCII characters.
|
|
39 |
|
|
40 |
The charset of the email will be the first one out of US-ASCII, ISO-8859-1
|
|
41 |
and UTF-8 that can represent all the characters occurring in the email.
|
|
42 |
|
32 |
43 |
This method does not perform any error checking and does
|
33 |
44 |
not guarantee delivery
|
34 |
45 |
"""
|
35 |
46 |
|
36 |
|
msg = MIMEText(body, _charset="utf-8")
|
37 |
|
msg['Subject'] = subject
|
38 |
|
msg['From'] = frm
|
39 |
|
msg['To'] = to
|
|
47 |
# Header class is smart enough to try US-ASCII, then the charset we
|
|
48 |
# provide, then fall back to UTF-8.
|
|
49 |
header_charset = 'ISO-8859-7'
|
|
50 |
|
|
51 |
# We must choose the body charset manually
|
|
52 |
for body_charset in 'US-ASCII', 'ISO-8859-7', 'UTF-8':
|
|
53 |
try:
|
|
54 |
body.encode(body_charset)
|
|
55 |
except UnicodeError:
|
|
56 |
pass
|
|
57 |
else:
|
|
58 |
break
|
|
59 |
|
|
60 |
# Split real name (which is optional) and email address parts
|
|
61 |
sender_name, sender_addr = parseaddr(sender)
|
|
62 |
recipient_name, recipient_addr = parseaddr(recipient)
|
|
63 |
|
|
64 |
# We must always pass Unicode strings to Header, otherwise it will
|
|
65 |
# use RFC 2047 encoding even on plain ASCII strings.
|
|
66 |
sender_name = str(Header(unicode(sender_name), header_charset))
|
|
67 |
recipient_name = str(Header(unicode(recipient_name), header_charset))
|
|
68 |
|
|
69 |
# Make sure email addresses do not contain non-ASCII characters
|
|
70 |
sender_addr = sender_addr.encode('ascii')
|
|
71 |
recipient_addr = recipient_addr.encode('ascii')
|
|
72 |
|
|
73 |
# Create the message ('plain' stands for Content-Type: text/plain)
|
|
74 |
msg = text.MIMEText(body.encode(body_charset), 'plain', body_charset)
|
|
75 |
msg['From'] = formataddr((sender_name, sender_addr))
|
|
76 |
msg['To'] = formataddr((recipient_name, recipient_addr))
|
|
77 |
msg['Subject'] = Header(unicode(subject), header_charset)
|
40 |
78 |
|
41 |
|
s = smtplib.SMTP(host=settings.SMTP_SERVER)
|
42 |
|
s.sendmail(frm, [to], msg.as_string())
|
|
79 |
s = SMTP(host=settings.SMTP_SERVER)
|
|
80 |
s.sendmail(sender, recipient, msg.as_string())
|
43 |
81 |
s.quit()
|