Statistics
| Branch: | Tag: | Revision:

root / logic / email_send.py @ e6209aa2

History | View | Annotate | Download (2.6 kB)

1 48130e66 Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
2 48130e66 Georgios Gousios
#
3 48130e66 Georgios Gousios
# Redistribution and use in source and binary forms, with or without
4 48130e66 Georgios Gousios
# modification, are permitted provided that the following conditions
5 48130e66 Georgios Gousios
# are met:
6 48130e66 Georgios Gousios
#
7 48130e66 Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
8 48130e66 Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
9 48130e66 Georgios Gousios
#
10 48130e66 Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
11 48130e66 Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
12 48130e66 Georgios Gousios
#     documentation and/or other materials provided with the distribution.
13 48130e66 Georgios Gousios
#
14 48130e66 Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 48130e66 Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 48130e66 Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 48130e66 Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 48130e66 Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 48130e66 Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 48130e66 Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 48130e66 Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 48130e66 Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 48130e66 Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 48130e66 Georgios Gousios
# SUCH DAMAGE.
25 48130e66 Georgios Gousios
#
26 48130e66 Georgios Gousios
# The views and conclusions contained in the software and documentation are
27 48130e66 Georgios Gousios
# those of the authors and should not be interpreted as representing official
28 48130e66 Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
29 48130e66 Georgios Gousios
30 48130e66 Georgios Gousios
# Methods for sending email
31 48130e66 Georgios Gousios
32 522c1282 Georgios Gousios
import json
33 35079ab2 Georgios Gousios
34 2252a338 Georgios Gousios
from django.core.mail import send_mail
35 522c1282 Georgios Gousios
from django.conf import settings
36 5ad78098 Georgios Gousios
import amqp_connection
37 522c1282 Georgios Gousios
38 6afc46cb Georgios Gousios
39 5ad78098 Georgios Gousios
def send_async(frm = settings.SYSTEM_EMAIL_ADDR,
40 5ad78098 Georgios Gousios
               to = None, subject = None, body = None):
41 522c1282 Georgios Gousios
    """
42 522c1282 Georgios Gousios
        Queue a message to be sent sometime later
43 522c1282 Georgios Gousios
        by a worker process.
44 522c1282 Georgios Gousios
    """
45 522c1282 Georgios Gousios
46 522c1282 Georgios Gousios
    msg = dict()
47 522c1282 Georgios Gousios
    msg['frm'] = frm
48 522c1282 Georgios Gousios
    msg['to'] = to
49 522c1282 Georgios Gousios
    msg['subject'] = subject
50 522c1282 Georgios Gousios
    msg['body'] = body
51 522c1282 Georgios Gousios
52 522c1282 Georgios Gousios
    routekey = "logic.email.outgoing"
53 5ad78098 Georgios Gousios
    amqp_connection.send(json.dumps(msg), settings.EXCHANGE_API, routekey)
54 522c1282 Georgios Gousios
55 6afc46cb Georgios Gousios
56 35079ab2 Georgios Gousios
def send (sender = settings.SYSTEM_EMAIL_ADDR,
57 35079ab2 Georgios Gousios
          recipient = None, subject = None, body = None):
58 2252a338 Georgios Gousios
    import logging
59 2252a338 Georgios Gousios
    logger = logging.getLogger("synnefo.logic")
60 2252a338 Georgios Gousios
    attempts = 0
61 522c1282 Georgios Gousios
62 2252a338 Georgios Gousios
    while attempts < 3:
63 35079ab2 Georgios Gousios
        try:
64 2252a338 Georgios Gousios
            send_mail(subject, body, sender, [recipient])
65 2252a338 Georgios Gousios
            return
66 2252a338 Georgios Gousios
        except Exception as e:
67 2252a338 Georgios Gousios
            logger.warn("Error sending email: ", e)
68 2252a338 Georgios Gousios
        finally:
69 2252a338 Georgios Gousios
            attempts += 1
70 2252a338 Georgios Gousios
    logger.warn("Failed all %d attempts to send email, aborting", attempts)