You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

HTML邮件模板发送后收件箱中图标、字体及图片无法显示问题排查求助

解决Django注册邮件模板图标、字体、图片全挂的问题

兄弟我之前做邮件模板的时候也踩过一模一样的坑!邮件客户端的渲染逻辑和浏览器完全不是一回事,咱们逐个拆解问题,一步步搞定:

1. Font Awesome图标死活不显示的核心原因

你用的<script src="https://kit.fontawesome.com/...">引入方式完全行不通——几乎所有主流邮件客户端(Gmail、Outlook、QQ邮箱等)都直接禁用JavaScript,JS加载的字体图标根本不会被渲染。就算你换成CSS引入的Font Awesome,很多客户端也不支持外部@font-face引用。

实用解决办法:

  • 直接内嵌SVG图标:去Font Awesome官网下载单个图标的SVG文件,把代码直接粘贴到HTML里替换<i>标签,比如把<i class="fas fa-arrow-circle-right"></i>换成对应的SVG代码,完全不需要外部请求;
  • 退而求其次用Unicode符号:比如箭头用&#8594;、信封用&#9993;,虽然样式不如Font Awesome精致,但兼容性拉满,不会出问题。

2. Google字体加载失败的问题

邮件客户端对@import url(...)的支持差到离谱,Gmail、Outlook这些直接会忽略这种外部字体引用,所以你设置的Carattere字体根本不会生效。

解决办法:

  • 加系统字体兜底:给字体声明添加系统自带的相似字体,保证排版风格不会崩,比如:
.welcome {
  font-family: 'Carattere', cursive, Apple Chancery, cursive;
}
  • 极端需求用Base64内嵌字体:把字体文件转成Base64编码,用内联@font-face写在<style>里,但这种方式会大幅增加邮件体积,而且部分客户端还是不买账,谨慎使用。

3. 图片无法加载的问题

你用的{% static '../static/images/logo1.png' %}生成的是相对路径(比如/static/images/logo1.png),但邮件客户端不知道你的服务器根路径在哪,必须用完整的绝对URL才能加载图片。

解决办法:

  • 在Django视图里生成绝对URL:用request.build_absolute_uri(static('images/logo1.png'))生成完整的图片链接,再传给模板;
  • 或者直接在模板里拼接:
{{ request.scheme }}://{{ request.get_host }}{% static 'images/logo1.png' %}

这样生成的路径就是http://你的域名/static/images/logo1.png,邮件客户端就能正常请求了。

4. 邮件CSS渲染的额外注意事项

就算你内联了CSS,邮件客户端对CSS的支持还是有一堆限制:

  • 坚持用表格布局(你已经这么做了,这点很棒),别用div的flex/grid,大部分客户端不支持;
  • 所有关键样式尽量直接写在元素的style属性里<style>标签里的样式在Gmail等客户端会被过滤,只能做补充;
  • 尽量少用CSS类选择器,直接把样式写在元素上,比如把<p class="welcome">改成<p style="font-family: 'Carattere', cursive, Apple Chancery, cursive; font-size:50px;">

调整后的代码片段示例

给你改了核心部分的代码,参考一下:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    /* 仅作为补充,核心样式全部内联 */
    body { margin: 0; padding: 0; }
    table { border-spacing: 0; }
    td { padding: 0; }
  </style>
</head>
<body>
  <center style="width:100%; table-layout:fixed; background-color:#cccccc; padding-bottom:60px;">
    <table width="100%" style="background-color:#ffffff; margin:0 auto; max-width:600px; border-spacing:0; font-family:sans-serif; color:#4a4a4a;">
      <!-- 头部蓝色条 -->
      <tr><td height="8" style="background-color:#289dcf;"></td></tr>
      <!-- Logo部分 -->
      <tr>
        <td style="background-color:#001035;">
          <a href="{{ request.scheme }}://{{ request.get_host }}/">
            <img src="{{ request.scheme }}://{{ request.get_host }}{% static 'images/logo1.png' %}" alt="Solve Litigation Logo" width="600" style="max-width:100%;">
          </a>
        </td>
      </tr>
      <!-- 欢迎内容 -->
      <tr>
        <td style="padding:5px 0 50px;">
          <table width="100%">
            <tr>
              <td style="text-align:center; padding:15px;">
                <p style="font-size:50px; font-family:'Carattere', cursive, Apple Chancery, cursive;">
                  <strong style="color:#289dcf;">Welcome to Solve Litigation</strong>
                </p>
                <p style="font-family:'Carattere', cursive, Apple Chancery, cursive; font-weight:bold; font-size:25px; padding:5px 0 15px;">
                  Hello, {{name}}, you have been successfully registered with Solve Litigation.
                </p>
                <a href="{{ request.scheme }}://{{ request.get_host }}/" style="background-color:#289dcf; color:#ffffff; text-decoration:none; padding:12px 12px; font-weight:bold; border-radius:5px; cursor:pointer;">
                  Go to Website 
                  <!-- 替换为内嵌SVG图标 -->
                  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
                    <path fill-rule="evenodd" d="M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8zm15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM4.5 7.5a.5.5 0 0 0 0 1h5.793l-2.147 2.146a.5.5 0 0 0 .708.708l3-3a.5.5 0 0 0 0-.708l-3-3a.5.5 0 1 0-.708.708L10.293 7.5H4.5z"/>
                  </svg>
                </a>
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <!-- 其他部分同理,把样式内联、图标替换即可 -->
    </table>
  </center>
</body>
</html>

内容的提问来源于stack exchange,提问作者Ritankar Bhattacharjee

火山引擎 最新活动