root / logic / email_send.py @ 48130e66
History | View | Annotate | Download (4.2 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
|
30 |
# Methods for sending email
|
31 |
|
32 |
import json |
33 |
|
34 |
from smtplib import SMTP |
35 |
from email.mime import text |
36 |
from email.header import Header |
37 |
from email.utils import parseaddr, formataddr |
38 |
|
39 |
from django.conf import settings |
40 |
import amqp_connection |
41 |
|
42 |
|
43 |
def send_async(frm = settings.SYSTEM_EMAIL_ADDR, |
44 |
to = None, subject = None, body = None): |
45 |
"""
|
46 |
Queue a message to be sent sometime later
|
47 |
by a worker process.
|
48 |
"""
|
49 |
|
50 |
msg = dict()
|
51 |
msg['frm'] = frm
|
52 |
msg['to'] = to
|
53 |
msg['subject'] = subject
|
54 |
msg['body'] = body
|
55 |
|
56 |
routekey = "logic.email.outgoing"
|
57 |
amqp_connection.send(json.dumps(msg), settings.EXCHANGE_API, routekey) |
58 |
|
59 |
|
60 |
def send (sender = settings.SYSTEM_EMAIL_ADDR, |
61 |
recipient = None, subject = None, body = None): |
62 |
"""
|
63 |
Connect to the email server configured in settings.py
|
64 |
and send the email.
|
65 |
|
66 |
All arguments should be Unicode strings (plain ASCII works as well).
|
67 |
|
68 |
Only the real name part of sender and recipient addresses may contain
|
69 |
non-ASCII characters.
|
70 |
|
71 |
The charset of the email will be the first one out of US-ASCII, ISO-8859-1
|
72 |
and UTF-8 that can represent all the characters occurring in the email.
|
73 |
|
74 |
This method does not perform any error checking and does
|
75 |
not guarantee delivery
|
76 |
"""
|
77 |
|
78 |
# Header class is smart enough to try US-ASCII, then the charset we
|
79 |
# provide, then fall back to UTF-8.
|
80 |
header_charset = 'ISO-8859-7'
|
81 |
|
82 |
# We must choose the body charset manually
|
83 |
for body_charset in 'US-ASCII', 'ISO-8859-7', 'UTF-8': |
84 |
try:
|
85 |
body.encode(body_charset) |
86 |
except UnicodeError: |
87 |
pass
|
88 |
else:
|
89 |
break
|
90 |
|
91 |
# Split real name (which is optional) and email address parts
|
92 |
sender_name, sender_addr = parseaddr(sender) |
93 |
recipient_name, recipient_addr = parseaddr(recipient) |
94 |
|
95 |
# We must always pass Unicode strings to Header, otherwise it will
|
96 |
# use RFC 2047 encoding even on plain ASCII strings.
|
97 |
sender_name = str(Header(unicode(sender_name), header_charset)) |
98 |
recipient_name = str(Header(unicode(recipient_name), header_charset)) |
99 |
|
100 |
# Make sure email addresses do not contain non-ASCII characters
|
101 |
sender_addr = sender_addr.encode('ascii')
|
102 |
recipient_addr = recipient_addr.encode('ascii')
|
103 |
|
104 |
# Create the message ('plain' stands for Content-Type: text/plain)
|
105 |
msg = text.MIMEText(body.encode(body_charset), 'plain', body_charset)
|
106 |
msg['From'] = formataddr((sender_name, sender_addr))
|
107 |
msg['To'] = formataddr((recipient_name, recipient_addr))
|
108 |
msg['Subject'] = Header(unicode(subject), header_charset) |
109 |
|
110 |
s = SMTP(host=settings.SMTP_SERVER) |
111 |
s.sendmail(sender, recipient, msg.as_string()) |
112 |
s.quit() |