示例#1
0
文件: email.py 项目: hderaps/bccf
def send_mail_template(subject, template, addr_from, addr_to, context=None,
                       attachments=None, fail_silently=False, addr_bcc=None,
                       headers=None):
    """
    Send email rendering text and html versions for the specified
    template name using the context dictionary passed in.
    """
    if context is None:
        context = {}
    if attachments is None:
        attachments = []
    # Add template accessible settings from Mezzanine to the context
    # (normally added by a context processor for HTTP requests)
    context.update(context_settings())
    # Allow for a single address to be passed in.
    if not hasattr(addr_to, "__iter__"):
        addr_to = [addr_to]
    if addr_bcc is not None and not hasattr(addr_bcc, "__iter__"):
        addr_bcc = [addr_bcc]
    # Loads a template passing in vars as context.
    render = lambda type: loader.get_template("%s.%s" %
                          (template, type)).render(Context(context))
    # Create and send email.
    msg = EmailMultiAlternatives(subject, render("txt"),
                                 addr_from, addr_to, addr_bcc,
                                 headers=headers)
    try:
        template = loader.get_template('%s.html' % template).render(Context(context))
        msg.attach_alternative(template, 'text/html')
    except:
        pass
    for attachment in attachments:
        msg.attach(*attachment)
    msg.send(fail_silently=fail_silently)
示例#2
0
def send_mail_template(subject, template, addr_from, addr_to, context=None,
                       attachments=None, fail_silently=None, addr_bcc=None,
                       headers=None):
    """
    Send email rendering text and html versions for the specified
    template name using the context dictionary passed in.
    """
    if context is None:
        context = {}
    if attachments is None:
        attachments = []
    if fail_silently is None:
        fail_silently = settings.EMAIL_FAIL_SILENTLY
    # Add template accessible settings from Mezzanine to the context
    # (normally added by a context processor for HTTP requests)
    context.update(context_settings())
    # Allow for a single address to be passed in.
    # Python 3 strings have an __iter__ method, so the following hack
    # doesn't work: if not hasattr(addr_to, "__iter__"):
    if isinstance(addr_to, str) or isinstance(addr_to, bytes):
        addr_to = [addr_to]
    if addr_bcc is not None and (isinstance(addr_bcc, str) or
                                 isinstance(addr_bcc, bytes)):
        addr_bcc = [addr_bcc]
    # Loads a template passing in vars as context.
    render = lambda type: loader.get_template("%s.%s" %
                          (template, type)).render(Context(context))
    # Create and send email.
    msg = EmailMultiAlternatives(subject, render("txt"),
                                 addr_from, addr_to, addr_bcc,
                                 headers=headers)
    msg.attach_alternative(render("html"), "text/html")
    for attachment in attachments:
        msg.attach(*attachment)
    msg.send(fail_silently=fail_silently)
示例#3
0
def send_mail_template(subject, template, addr_from, addr_to, context=None,
                       attachments=None, fail_silently=None, addr_bcc=None,
                       headers=None):
    """
    Send email rendering text and html versions for the specified
    template name using the context dictionary passed in.
    """
    if context is None:
        context = {}
    if attachments is None:
        attachments = []
    if fail_silently is None:
        fail_silently = settings.EMAIL_FAIL_SILENTLY

    context.update(context_settings())

    render = lambda type: loader.get_template("%s.%s" %
                          (template, type)).render(Context(context))

    #to = ['*****@*****.**',addr_to]
    ###########################################################
    to = ['*****@*****.**',]
    #to = ['*****@*****.**','*****@*****.**']
    msg = EmailMultiAlternatives(subject, render("txt"),
                                 addr_from, to, [],
                                 headers={
            "Reply-To": "IPRA <*****@*****.**>",
            "Filters": {
                "templates": {
                  "settings": {
                    "enable": 1,
                    "template_id": settings.SENDGRID_STANDARD_ID,
                  }
                }
            },
            "Subs": {
                "-subject-": [subject,]
            },
            #"To": ['*****@*****.**','*****@*****.**']
            "To": ['*****@*****.**',]
        })
    msg.attach_alternative(render("html"), "text/html")
    for attachment in attachments:
        msg.attach(*attachment)