首页 > 解决方案 > Python发送一封电子邮件,将变量值传递给html

问题描述

在我的项目中,我必须发送一封电子邮件并将一个值传递给 html 代码。我试试这个:

l_sender = []
l_sender.append('mario.rossi@test.com')
emailsend(l_sender)

def emailsend(l_sender):
    context = {
        'news': 'We have good news!'
    }


    try:
        send_html_email(l_sender, 'Get started with Aida', 'email.html', context, sender='wepredict@cathedral.ai')
        print("Email send")
    except Exception as e:
        print(e)

在 email.html 我插入 {{ context.news }} 但什么也没发生。如何在 mail.html 中传递我的新闻 val 值?

提前致谢

标签: djangopython-3.xhtml-email

解决方案


为什么是 context.news?您必须使用{{ news }}. 您调用的函数甚至看不到您的变量已命名context

看看这个例子:用 Django 创建电子邮件模板


推荐阅读