我有一个html的密码重置邮箱模板,我正在try 设置,而不是默认的django邮箱.代码如下:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css2?family=Inter:wght@300&display=swap"
      rel="stylesheet"
    />
    <style>
      body {
        font-family: "Inter", sans-serif;
        font-size: 20px;
        color: #434343;
        margin: 0;
      }

      .main-container {
        max-width: 60%;
      }

      .centered-contents {
        display: flex;
        justify-content: center;
      }

      .pswd-button {
        background: #677db6;
        border-radius: 25px;
        color: #ffffff;
        cursor: pointer;
        text-decoration: none;
        padding: 15px;
        padding-top: 10px;
        padding-bottom: 10px;
      }

      .top-bar {
        width: 100%;
        background-color: #d9d9d9;
        display: flex;
        align-items: center;
      }

      .top-bar-title {
        color: #677db6;
        margin: 20px;
      }

    </style>
  </head>
  <body>
    <div class="top-bar">
      <p class="top-bar-title">Nombre de Web</p>
    </div>

    <div class="centered-contents">
      <div class="main-container">
        <p style="font-size: 24px">
          Hola <span style="color: #677db6">nombre</name></span>!
        </p>

        <p>
          Está recibiendo este correo porque ha solicitado un cambio de
          contraseña en
          <span style="color: #677db6">web</span>. Para
          continuar, por favor utilice el botón que se muestra a continuación:
        </p>

        <div class="centered-contents">
          <a class="pswd-button" href="#"> Restablecer Contraseña </a>
        </div>

        <p>
          Le recordamos su nombre de usuario:
          <span style="color: #677db6">usuario</span>
        </p>

        <p>
          Si no ha solicitado ningún cambio de contraseña, no se preocupe. Puede
          ignorar este correo.
        </p>
        <p>Atentamente,</p>
        <p>El equipo técnico de <span style="color: #677db6">Web</span></p>
      </div>
    </div>
  </body>
</html>

并且,在我的浏览器(chrome)中渲染,它看起来如下所示:

custom email template

我正在try 使用django中的模板,在templates文件夹中创建一个password_reset_email.html,并将其传递给django.contrib.auth.PasswordResetView.然而,我收到的邮箱(显示在gmail中)如下所示:

email

我做错了什么?我try 了以下方法来解决此问题:

  1. 删除所有css,并将所有样式inside放入组件.
<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css2?family=Inter:wght@300&display=swap"
      rel="stylesheet"
    />
  </head>
  <body style="
    font-family: 'Inter', sans-serif;
    font-size: 20px;
    color: #434343;
    margin: 0;
    ">
    <div style="
      width: 100%;
      background-color: #d9d9d9;
      display: flex;
      align-items: center;
      ">
      <p style="
        color: #677db6;
        margin: 20px;
        ">
        Nombre de Web</p>
    </div>

    <div style="
      display: flex;
      justify-content: center;
      ">
      <div style="max-width: 60%;">
        <p style="font-size: 24px">
          Hola <span style="color: #677db6">nombre</name></span>!
        </p>

        <p>
          Está recibiendo este correo porque ha solicitado un cambio de
          contraseña en
          <span style="color: #677db6">web</span>. Para
          continuar, por favor utilice el botón que se muestra a continuación:
        </p>

        <div style="
          display: flex;
          justify-content: center;
          ">
          <a style="
            background: #677db6;
            border-radius: 25px;
            color: #ffffff;
            cursor: pointer;
            text-decoration: none;
            padding: 15px;
            padding-top: 10px;
            padding-bottom: 10px;
            " 
            href="#"> Restablecer Contraseña </a>
        </div>

        <p>
          Le recordamos su nombre de usuario:
          <span style="color: #677db6">usuario</span>
        </p>

        <p>
          Si no ha solicitado ningún cambio de contraseña, no se preocupe. Puede
          ignorar este correo.
        </p>
        <p>Atentamente,</p>
        <p>El equipo técnico de <span style="color: #677db6">Web</span></p>
      </div>
    </div>
  </body>
</html>

这将修复除字体和对齐方式以外的所有内容

styling fixed

  1. 为对齐添加表 struct :
<!DOCTYPE html>
<html>

<head>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300&display=swap" rel="stylesheet" />
</head>

<body style="
  font-family: 'Inter', sans-serif;
  font-size: 20px;
  color: #434343;
  margin: 0;
  ">
    <div style="
      width: 100%;
      background-color: #d9d9d9;
      display: flex;
      align-items: center;
      ">
        <p style="
        color: #677db6;
        margin: 20px;
        ">Nombre de Web</p>
    </div>
    <table cellspacing=”0” cellpadding=”0” width="100%">
        <tr>
            <td width="25%"></td>
            <td>
                <table cellspacing=”0” cellpadding=”0” width="100%">
                    <tr>
                        <td>
                            <p style="font-size: 24px">
                                Hola <span style="color: #677db6">nombre</name></span>!
                            </p>

                            <p>
                                Está recibiendo este correo porque ha solicitado un cambio de contraseña en
                                <span style="color: #677db6">web</span>. Para continuar, por favor utilice el botón que se muestra a continuación:
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" style="padding: 15px;">
                            <a style="
                              background: #677db6;
                              border-radius: 25px;
                              color: #ffffff;
                              cursor: pointer;
                              text-decoration: none;
                              padding: 15px;
                              padding-top: 10px;
                              padding-bottom: 10px;
                              " href="#">Restablecer Contraseña</a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <p>
                                Le recordamos su nombre de usuario:
                                <span style="color: #677db6">usuario</span>
                            </p>

                            <p>
                                Si no ha solicitado ningún cambio de contraseña, no se preocupe. Puede ignorar este correo.
                            </p>
                            <p>Atentamente,</p>
                            <p>El equipo técnico de <span style="color: #677db6">Web</span></p>
                        </td>
                    </tr>
                </table>
            </td>
            <td width="25%"></td>
        </tr>
    </table>
</body>

</html>

这只会使字体仍然无法工作:

tables for alignment

所以,我的问题是:如何使字体(Inter,font-weight 300)也正确显示?

推荐答案

你认为Gmail应该支持自己的字体库!

事实上,它只支持Google SAN和Roboto.

@导入使用@font-face,它具有以下邮箱支持:https://www.caniemail.com/features/css-at-font-face/(脚注:不支持.可以使用Roboto和Google SAN,但这只是因为它们嵌入了webmail自己的样式.)

因此,如果您有偏好,我建议您在字体堆栈中添加其中一个,例如Inter、Roboto、sans-serif(不通过@import加载Roboto,因为它已经在内部加载).您还应该为不支持这两种字体的用户添加Arial或类似的websafe字体.

最终堆叠:Inter, Roboto, Arial, sans-serif;

Django相关问答推荐

我希望用户能够预订相同的桌子,但不是在同一时间

获取 Django (postgres) 中带注释字段的平均值

Django Inline Tabular admin:删除一个不工作的对象

django 的 Manager.create() 方法有什么作用?

django - 让用户登录到测试客户端

我的 django 模板布尔变量在 javascript 中没有按预期工作

直接在模型类上使用 Django 管理器与静态方法

在 django web 应用程序中,如何给用户自己的子域?

手动触发 Django 邮件错误报告

始终将用户包含在 django 模板上下文中

django:TypeError:'tuple'对象不可调用

Django - 了解 X-Sendfile

如何在 Django 中测试自定义模板标签?

引发 404 并继续 URL 链

如何在 django 中仅获取表的特定列?

如何在该元素中的所有应用程序之间共享元素范围的模板标签

Django模板转换为字符串

使用 request.user 的 Django 和中间件始终是匿名的

如何在 Python 中运行另一个脚本而不等待它完成?

在 Django 中使用 select_related Select 特定字段