Statistics
| Branch: | Tag: | Revision:

root / logic / email_send.py @ cf8482f2

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 f2bdb9ab Georgios Gousios
from synnefo.logic import log
39 f2bdb9ab Georgios Gousios
40 f2bdb9ab Georgios Gousios
_logger = log.get_logger("synnefo.logic")
41 6afc46cb Georgios Gousios
42 5ad78098 Georgios Gousios
def send_async(frm = settings.SYSTEM_EMAIL_ADDR,
43 5ad78098 Georgios Gousios
               to = None, subject = None, body = None):
44 522c1282 Georgios Gousios
    """
45 522c1282 Georgios Gousios
        Queue a message to be sent sometime later
46 522c1282 Georgios Gousios
        by a worker process.
47 522c1282 Georgios Gousios
    """
48 522c1282 Georgios Gousios
49 522c1282 Georgios Gousios
    msg = dict()
50 522c1282 Georgios Gousios
    msg['frm'] = frm
51 522c1282 Georgios Gousios
    msg['to'] = to
52 522c1282 Georgios Gousios
    msg['subject'] = subject
53 522c1282 Georgios Gousios
    msg['body'] = body
54 522c1282 Georgios Gousios
55 522c1282 Georgios Gousios
    routekey = "logic.email.outgoing"
56 5ad78098 Georgios Gousios
    amqp_connection.send(json.dumps(msg), settings.EXCHANGE_API, routekey)
57 522c1282 Georgios Gousios
58 6afc46cb Georgios Gousios
59 35079ab2 Georgios Gousios
def send (sender = settings.SYSTEM_EMAIL_ADDR,
60 35079ab2 Georgios Gousios
          recipient = None, subject = None, body = None):
61 f2bdb9ab Georgios Gousios
62 2252a338 Georgios Gousios
    attempts = 0
63 522c1282 Georgios Gousios
64 2252a338 Georgios Gousios
    while attempts < 3:
65 35079ab2 Georgios Gousios
        try:
66 2252a338 Georgios Gousios
            send_mail(subject, body, sender, [recipient])
67 583bfaa0 Georgios Gousios
            return True
68 2252a338 Georgios Gousios
        except Exception as e:
69 f2bdb9ab Georgios Gousios
            _logger.exception("Error sending email")
70 2252a338 Georgios Gousios
        finally:
71 2252a338 Georgios Gousios
            attempts += 1
72 583bfaa0 Georgios Gousios
73 f2bdb9ab Georgios Gousios
    _logger.warn("Failed all %d attempts to send email, aborting", attempts)
74 583bfaa0 Georgios Gousios
    return False