首页 > 解决方案 > 当我使用 jinja 模板发送电子邮件时,css 不起作用

问题描述

我正在尝试使用 jinja 模板发送电子邮件,但样式(css)不起作用

这是我用来发送带有模板的电子邮件的代码

    msg = EmailMessage()
    msg['Subject'] = 'Prueba de correo'
    msg['From'] = 'my address email'
    msg['To'] = 'name@domail.com'
    msg.set_content('Correo automatico desde python')

    html = render_template('email.html');
    msg.add_alternative(html,subtype="html")

    with smtplib.SMTP_SSL('smtp.gmail.com',465) as server:
        server.login('my address email ','my password')
        server.send_message(msg)
        server.quit()    

这是神社模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- <link rel="stylesheet" href="{{ url_for('static', filename='email.css') }}"/> -->
</head>
<body>
    <style>
        h1{
            text-align: center;
            color: red;
        }
    </style>
    <h1>Hola Mundo</h1>
</body>
</html>

但是当电子邮件发送时

看起来像这样

电子邮件内容

它应该是这样的

带有样式的模板

标签: pythoncssflasksmtpjinja2

解决方案


您是否尝试过取消注释标签?

该行应为:

<link rel="stylesheet" href="{{ url_for('static', filename='email.css') }}"/>

推荐阅读