Ejemplo n.º 1
0
def send_mail(to, etype, request, data=None):
    add_cc = ["mentoring_request", "mentoring_request_intro"]
    if data is None:
        data = {}
    mi = emails.get(etype, "data").copy()

    if mi.get("mailer"):
        mailer = mailer_factory_from_settings(mi.get("mailer"), "")
    else:
        mailer = get_mailer(request)

    if "user" not in data and request.user:
        data["user"] = request.user

    if request and request.authenticated_userid and data["user"].id != request.authenticated_userid:
        data["auth_user"] = request.user

    for k, v in mi.iteritems():
        if callable(v):
            v = v(data, request)
        if ".jinja2" in v:
            v = render(v, data, request)
        elif isinstance(v, basestring):
            if "{{" in v and "}}" in v:
                v = Template(v).render(**data)
            else:
                v = v.format(**data)
        mi[k] = v

    if "attachments" in data:
        attachments = data["attachments"]
    elif "attachments" in mi and mi["attachments"] and mi["attachments"] != "None":
        with open(os.path.dirname(os.path.dirname(__file__)) + mi["attachments"]) as fo:
            a_file = fo.read()
        attachments = [Attachment(os.path.basename(mi["attachments"]), "application/" + mi["attachments"][-3:], a_file)]
    else:
        attachments = None
    if not isinstance(to, list):
        to = to.split(",")

    from alchemist.models.settings import Settings

    send_from = mi.get("from")
    bcc = [bc for bc in data.get("bcc", mi.get("bcc", "").split(",")) if bc]
    sender = Settings.get("admin_mail", "*****@*****.**")
    sender = sender.split(",")
    main_admin_mail = sender[0].strip()
    if main_admin_mail:
        bcc.append(main_admin_mail)
    if not send_from:
        send_from = main_admin_mail or "*****@*****.**"
    cc = mi.get("cc") if mi.get("cc") else []
    if "@" not in cc and not isinstance(cc, list):
        cc = Settings.get(cc, cc).split(",")
    if not cc and etype in add_cc:
        cc = [email.strip() for email in Settings.get("cc_emails", "").split(",")]
    if debug and not set(Settings.get("allowed_outgoing", "").split(",")).issuperset(bcc + cc + to):
        print "Wanted to send email %s to %s, didn't" % (etype, bcc + cc + to)
        return None
    mail = Message(mi["subject"], to, mi["text"], mi["html"], sender=send_from, bcc=bcc, cc=cc)
    if attachments:
        mail.attachments = attachments
    mailer.send(mail)
Ejemplo n.º 2
0
def send_mail(to, etype, request, data=None):
    add_cc = ['mentoring_request', 'mentoring_request_intro']
    if data is None:
        data = {}
    mi = emails.get(etype, 'data').copy()

    if mi.get('mailer'):
        mailer = mailer_factory_from_settings(mi.get('mailer'), '')
    else:
        mailer = get_mailer(request)

    if 'user' not in data:
        from alchemist.models import User
        data['user'] = User.fr(request)

    if request.authenticated_userid and data['user'].id != request.authenticated_userid:
        from alchemist.models import User
        data['auth_user'] = User.fr(request)

    for k, v in mi.iteritems():
        if callable(v):
            v = v(data, request)
        if '/templates/' in v:
            v = render(v, data, request)
        elif isinstance(v, basestring):
            if '{{' in v and '}}' in v:
                v = Template(v).render(**data)
            else:
                v = v.format(**data)
        mi[k] = v

    if 'attachments' in data:
        attachments = data['attachments']
    elif 'attachments' in mi and mi['attachments'] and mi['attachments'] != 'None':
        with open(os.path.dirname(__file__) + mi['attachments']) as fo:
            a_file = fo.read()
        attachments = [Attachment(os.path.basename(mi['attachments']),
                                  'application/' + mi['attachments'][-3:],
                                  a_file)]
    else:
        attachments = None
    if not isinstance(to, list):
        to = to.split(',')

    from alchemist.models.settings import Settings

    send_from = mi.get('from')
    bcc = [bc for bc in mi.get('bcc', '').split(',') if bc]
    sender = Settings.get('admin_mail', '*****@*****.**')
    sender = sender.split(',')
    main_admin_mail = sender[0].strip()
    if main_admin_mail:
        bcc.append(main_admin_mail)
    if not send_from:
        send_from = main_admin_mail or '*****@*****.**'
    cc = mi.get('cc') if mi.get('cc') else []
    if '@' not in cc and not isinstance(cc, list):
        cc = Settings.get(cc, cc).split(',')
    if not cc and etype in add_cc:
        cc = [email.strip() for email in Settings.get('cc_emails', '').split(',')]
    mail = Message(mi['subject'], to, mi['text'], mi['html'],
                   sender=send_from,
                   bcc=bcc, cc=cc)
    if attachments:
        mail.attachments = attachments
    mailer.send(mail)