示例#1
0
文件: mailerdj.py 项目: marce0202/app
 def mandar_email_mailjet(self, templates_path, vars, tipo=None, debug=False,user="",password="",sender=""):
     """
     Send an e-mail loading its Jinja2 template through mailjet.
     args:
         templates_path: it is the Jinja2 template path to render.
             DEPRECATED: It can also be a dictionary of Jinja2 templates
             whose key has to be specifiead in 'tipo' parameter.
             be a dictionar
         vars: a dictionary which includes the values that will be used
             to parse the Jinja2 template. It also has **ALWAYS** to include
             the following values: 'to_user' (the e-mail recipient),
             'from_user' (the e-mail sender) and 'subject' (just the e-mail
             subject).
         tipo: DEPRECATED: it defines de key for templates_path when it is
             a dictionary.
         debug: if True it prints some params by the standard output.
     """
     if not type(vars['to_user']) == list:
         vars['to_user'] = [vars['to_user']]
     template_context = vars
     template_context.update({'_':self._gettext})
     if tipo: actual_template = templates_path[tipo]
     else: actual_template = templates_path
     with codecs.open(actual_template, 'r', 'utf-8') as template_file:
         mail_template = Template(template_file.read())
     texto_mail = mail_template.render(**template_context)
     if debug:
         print "tipo:",tipo
         print "template:", actual_template
         print 'to_user:'******'to_user']
         print 'from_user:'******'from_user']
         print 'subject:', vars['subject']
     send_mailjet(vars['from_user'],user,password,vars['to_user'], vars['subject'], texto_mail)
示例#2
0
    def mandar_email_mailjet(self, templates_path, vars, tipo=None, debug=False, user="", password="", sender=""):
        """
        Send an e-mail loading its Jinja2 template through mailjet.
        args:
            templates_path: it is the Jinja2 template path to render.
                DEPRECATED: It can also be a dictionary of Jinja2 templates
                whose key has to be specifiead in 'tipo' parameter.
                be a dictionar
            vars: a dictionary which includes the values that will be used
                to parse the Jinja2 template. It also has **ALWAYS** to include
                the following values: 'to_user' (the e-mail recipient),
                'from_user' (the e-mail sender) and 'subject' (just the e-mail
                subject).
            tipo: DEPRECATED: it defines de key for templates_path when it is
                a dictionary.
            debug: if True it prints some params by the standard output.
        """
        try:
            if not settings.configured:
                settings.configure(DEBUG=debug, TEMPLATE_DEBUG=debug, TEMPLATE_DIRS=self.templates_dir)
        except Exception as e:
            pass

        if not type(vars["to_user"]) == list:
            vars["to_user"] = [vars["to_user"]]
        template_context = vars
        template_context.update({"_": self._gettext})
        actual_template = templates_path[tipo] if tipo else templates_path
        mail_template = get_template(actual_template)
        texto_mail = mail_template.render(Context(template_context))
        if vars.get("process_mail", False):
            if vars.get("base_url", ""):
                if not u"http://" in vars.get("base_url", ""):
                    vars["base_url"] = u"http://" + vars.get("base_url", "")
                if not vars.get("base_url", "")[-1] == "/":
                    vars["base_url"] = vars.get("base_url", "") + u"/"
            texto_mail = premailer.transform(texto_mail, base_url=vars.get("base_url", ""))
        additionals = {}
        if "reply_to" in vars and vars["reply_to"]:
            additionals["reply_to"] = vars["reply_to"]
        if "bcc" in vars and vars["bcc"]:
            additionals["bcc"] = vars["bcc"] if type(vars["bcc"]) == list else [vars["bcc"]]
        if "campaign" in vars and vars["campaign"]:
            additionals["campaign"] = vars["campaign"]
        if "attachment" in vars and vars["attachment"]:
            additionals["attachment"] = vars["attachment"]
            if not type(additionals["attachment"]) == list:
                additionals["attachment"] = [additionals["attachment"]]

        if debug:
            printf("tipo:", repr(tipo))
            printf("template:", repr(actual_template))
            printf("to_user:"******"to_user"]))
            printf("from_user:"******"from_user"]))
            printf("subject:", repr(vars["subject"]))
        send_mailjet(vars["from_user"], user, password, vars["to_user"], vars["subject"], texto_mail, **additionals)