コード例 #1
0
    def format_msg(self):
        # Construct email message with MIME
        msg = MIMEMultipart("alternative")
        msg["From"] = self.from_email
        msg["To"] = ", ".join(self.to_emails)
        msg["Subject"] = self.subject

        # If a text-type template was passed get the context
        if self.template_text_name is not None:
            template_filepath = Template(template_name=self.template_text_name,
                                         context=self.context)

            # context is already passed to template_filepath just need to render.
            template_content_string: str = template_filepath.render()
            txt_part = MIMEText(template_content_string, "plain")
            print(f"txt_part: {txt_part}")
            msg.attach(txt_part)

        if self.template_html_name is not None:
            template_filepath = Template(template_name=self.template_html_name,
                                         context=self.context)

            template_content_string: str = template_filepath.render()
            html_part = MIMEText(template_content_string, "html")
            print(f"html_part: {html_part}")
            msg.attach(html_part)

        msg_str: str = msg.as_string()

        return msg_str
コード例 #2
0
ファイル: scripts.py プロジェクト: mikonse/progen
def cli(ctx, type: str, name: str, template_dir, dest):
    if ctx.invoked_subcommand is not None:
        return

    if template_dir is not None:
        base_dir = pathlib.Path(template_dir)
    else:
        # relative path to module install
        base_dir = pathlib.Path(__file__).resolve().parents[1] / 'templates'

    if dest is None:
        dest = pathlib.Path.cwd() / name
    else:
        dest = pathlib.Path(dest)

    template = Template(base_dir / type / name)
    template.render(dest)
コード例 #3
0
ファイル: send_mail.py プロジェクト: TejasRavi51/python-first
    def format_msg(self):
        msg = MIMEMultipart('alternative')
        msg['From'] = self.from_email
        msg['To'] = ", ".join(self.to_emails)
        msg['Subject'] = self.subject

        if self.template_name != None:
            tmpl_str = Template(template_name= self.template_name,context = self.context)
            txt_part = MIMEText(tmpl_str.render(), 'plain')
            print(txt_part)
            msg.attach(txt_part)
        if self.template_html != None:
            tmpl_str = Template(template_name= self.template_html,context = self.context)
            html_part = MIMEText(tmpl_str.render(), 'html')
            print(html_part)
            msg.attach(html_part)
        msg_str = msg.as_string()
        return msg_str
コード例 #4
0
    def format_message(self):
        msg = MIMEMultipart('alternative')
        msg['From'] = self.from_email
        msg['To'] = self.to_emails
        msg['subject'] = self.subject

        if self.template_name is not None:
            template_obj = Template(self.template_name, self.context)
            text_part = MIMEText(template_obj.render(), 'plain')
            msg.attach(text_part)
        elif self.template_html is not None:
            template_obj = Template(self.template_html, self.context)
            html_part = MIMEText(template_obj.render(), 'html')
            msg.attach(html_part)
        else:
            pass

        msg_str = msg.as_string()
        return msg_str
コード例 #5
0
    def format_msg(self):
        message = MIMEMultipart("alternative")
        message["Subject"] = self.subject
        message["From"] = self.from_to
        message["To"] = ', '.join(self.send_to)

        if self.template_name is not None:
            tmpl_str = Template(self.template_name, self.context)
            txt_part = MIMEText(tmpl_str.render(), 'plain')
            print(txt_part)
            message.attach(txt_part)

        if self.template_html is not None:
            tmpl_str = Template(self.template_html, self.context)
            html_part = MIMEText(tmpl_str.render(), 'html')
            print(html_part)
            message.attach(html_part)

        message = message.as_string()
        return message