Statistics
| Branch: | Tag: | Revision:

root / logic / email_send.py @ 35079ab2

History | View | Annotate | Download (2.7 kB)

1
import json
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
7

    
8
from django.conf import settings
9
import amqp_connection
10

    
11

    
12
def send_async(frm = settings.SYSTEM_EMAIL_ADDR,
13
               to = None, subject = None, body = None):
14
    """
15
        Queue a message to be sent sometime later
16
        by a worker process.
17
    """
18

    
19
    msg = dict()
20
    msg['frm'] = frm
21
    msg['to'] = to
22
    msg['subject'] = subject
23
    msg['body'] = body
24

    
25
    routekey = "logic.email.outgoing"
26
    amqp_connection.send(json.dumps(msg), settings.EXCHANGE_API, routekey)
27

    
28

    
29
def send (sender = settings.SYSTEM_EMAIL_ADDR,
30
          recipient = None, subject = None, body = None):
31
    """
32
        Connect to the email server configured in settings.py
33
        and send the email.
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
        
43
        This method does not perform any error checking and does
44
        not guarantee delivery
45
    """
46

    
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)
78

    
79
    s = SMTP(host=settings.SMTP_SERVER)
80
    s.sendmail(sender, recipient, msg.as_string())
81
    s.quit()