示例#1
0
文件: mailerdj.py 项目: marce0202/app
 def mandar_email_masivo(self, templates_path, group_of_vars, tipo=None):
     """
     Send a group of e-mails rendering the same Jinja2 template. For each
     collection of variables defined in `group_of_vars` it will send a
     rendered e-mail with de defined data.
     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
         group_of_vars: an iterable of dictionaries where each dictionary
             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 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())
     for vars in group_of_vars:
         if not type(vars['to_user']) == list:
             vars['to_user'] = [vars['to_user']]
         template_context = vars
         template_context.update({'_':self._gettext})
         texto_mail = mail_template.render(**template_context)
         send_server_mail(vars['from_user'], vars['to_user'], vars['subject'], texto_mail)
示例#2
0
    def mandar_email(self, templates_path, vars, tipo=None, debug=False):
        """
        Send an e-mail loading its Django template.
        args:
            templates_path: it is the Django template path to render.
                DEPRECATED: It can also be a dictionary of django 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 django 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", ""))
        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"]))
        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"]]
        send_server_mail(vars["from_user"], vars["to_user"], vars["subject"], texto_mail, **additionals)
示例#3
0
文件: mailerdj.py 项目: marce0202/app
 def mandar_email(self, templates_path, vars, tipo=None, debug=False):
     """
     Send an e-mail loading its Jinja2 template.
     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']
     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']
     send_server_mail(vars['from_user'], vars['to_user'], vars['subject'], texto_mail,**additionals)