Statistics
| Branch: | Tag: | Revision:

root / logic / email_send.py @ 7427d9a3

History | View | Annotate | Download (1 kB)

1
import json
2
import smtplib
3
from email.mime.text import MIMEText
4

    
5
from django.conf import settings
6
import amqp_connection
7

    
8

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

    
16
    msg = dict()
17
    msg['frm'] = frm
18
    msg['to'] = to
19
    msg['subject'] = subject
20
    msg['body'] = body
21

    
22
    routekey = "logic.email.outgoing"
23
    amqp_connection.send(json.dumps(msg), settings.EXCHANGE_API, routekey)
24

    
25

    
26
def send (frm = settings.SYSTEM_EMAIL_ADDR,
27
          to = None, subject = None, body = None):
28
    """
29
        Connect to the email server configured in settings.py
30
        and send the email.
31

32
        This method does not perform any error checking and does
33
        not guarantee delivery
34
    """
35

    
36
    msg = MIMEText(body, _charset="utf-8")
37
    msg['Subject'] = subject
38
    msg['From'] = frm
39
    msg['To'] = to
40

    
41
    s = smtplib.SMTP(host=settings.SMTP_SERVER)
42
    s.sendmail(frm, [to], msg.as_string())
43
    s.quit()