Ejemplo n.º 1
0
    def mandar_email_gmail(self, templates_path, vars, tipo=None, debug=False, password="", email=""):
        """
        Send an e-mail loading its Jinja2 template through gmail.
        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 dictionary
            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", ""))
        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"]]
        if "sender" in vars and vars["sender"]:
            additionals["sender"] = vars["sender"]

        send_gmail(email, password, vars["to_user"], vars["subject"], texto_mail, encoding="utf-8", **additionals)
Ejemplo n.º 2
0
 def mandar_email_gmail(self, templates_path, vars, tipo=None, debug=False,password="",email=""):
     """
     Send an e-mail loading its Jinja2 template through gmail.
     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 dictionary
         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']
     if 'from_user_name' in vars and 'bcc' in vars:
         send_gmail(email,password, vars['to_user'], vars['subject'], texto_mail,encoding='utf-8',sender=vars['from_user_name'],bcc=vars['bcc'])
     else:
         send_gmail(email,password, vars['to_user'], vars['subject'], texto_mail,encoding='utf-8')