【问题标题】:Write html email from nodejs - How to style the email?从 nodejs 编写 html 电子邮件 - 如何设置电子邮件的样式?
【发布时间】:2021-11-17 17:34:29
【问题描述】:

我有一个使用 nodemailer 发送电子邮件的 nodejs 程序。起初,我只有一个简单的表格,其中包含一些数据和边框颜色等的最小样式,但现在我想在电子邮件中添加更多内容。

我写了一些 html 代码并通过网络浏览器查看它,完成后,我将 html 代码添加到电子邮件文本中。

但是在给自己发了一封测试邮件后,我发现这封邮件和我在网络浏览器中看到的很不一样。

我已经研究过了,例如,html 电子邮件完全支持 heigth,所以我不明白为什么样式很不合适。我现在唯一不能工作的是“背景图片”。

这是我的html代码:

<html>
    <head>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
            }
            td {
                border-bottom-style: solid;
                border-color: #dddddd;
                border-bottom-width: 1px;
                text-align: left;
                padding-top: 13px;
                padding-right: 14px;
            }
            th {
                padding: 8px;
            }
            .image {
                width: 293px;
                height: 80px;
                background-image: url(data:image/png;base64, DATA);
                background-size: contain;
                margin: 0 auto;
            }
            .topper {
                background-color: #003d8f;
                width: 100%;
                height: 80px;
                color: white;
                font-family: sans-serif;
                text-align: center;
            }
            .topper h1 {
                padding-top: 20px;
            }
            .content {
                margin: 0 auto;
                width: fit-content;
            }
            .footer p {
                width: fit-content;
                margin-left: auto;
                margin-right: auto;
                color: gray;
                font-size: small;
            }
        </style>
    </head>
    <body>
        <div class="topper">
            <h1>Some Text Here</h1>
        </div>
        <br />
        <div class="content">
            <div class="image"></div>
            <br />
            <br />
            <br />
            <table>
                <tr>
                    <td>Table:</td>
                    <td><b>DataDataDataDataDataData</b></td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>DataDataDataDataDataData</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>DataDataDataDataDataData</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>Data</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>DataDataDataDataDataData</td>
                </tr>
                <tr>
                    <td>Table:</td>
                    <td>DataDataDataDataDataData</td>
                </tr>
            </table>
            <br />
            <br />
        </div>
        <br />
        <br />
        <br />
        <hr/ style="border-top: 1px solid #e2e1e1; border-left: 0px">
        <div class="footer content">
            <p>Here is a longer sentence. Here are a few information about the email.</p>
            <p>Here is a longer sentence. Here are a few information about the email. This sentence is even longer but it should be.</p>
        </div>
    </body>
</html>

这就是电子邮件的样子:

Email Screenshot

知道如何更改此代码以使其在 html 电子邮件中正常工作吗?

编辑: 不起作用的事情:

  • .topper 未设置为高度:80px
  • .image 不加载背景图片
  • tablep 元素未以 margin: 0 auto;width: fit-content; 居中
  • p 元素不小且灰暗
  • hr 元素看起来不对

编辑: 我发现了一个非常有用的电子邮件客户端支持和不支持的样式规则集合:https://www.campaignmonitor.com/css/


编辑: 我将.footer p { 更改为p{,并将font-size: small 更改为font-size: 14px,现在底部文本的大小和颜色都正确。


编辑: 我将table{.imagep display: inline-block;text-align: center;.content{,现在它们居中


编辑: 像footer content 这样的多类也不起作用。将其更改为仅 content 确实使 p 居中


编辑: 从.image{ 中删除background-imagebackground-size 并将div 更改为img。将 cid:logo 添加为 src 并将此代码添加到 nodemailer:

attachments: [{
                filename: 'logo.png',
                path: __dirname + "\\logo.png",
                cid: "logo"
            }]

让你显示图像

【问题讨论】:

  • 请注意,电子邮件客户端仅支持 70% 的头部样式。如果您想要对电子邮件模板的最佳兼容支持,请使用内联样式。
  • @tacoshy 好的,我将来会改变它。但是现在为了测试,outlooks 似乎使用了标题样式。

标签: html css email html-email nodemailer


【解决方案1】:

您要渲染的布局非常复杂。这些电子邮件客户端无法正确处理。

尝试使用EJS 之类的模板引擎。您可以在this 答案中找到有关它的更多信息。

希望你能解决这个问题!

【讨论】:

  • 我需要一个模板引擎来使 div 居中并在 html 电子邮件中设置高度?我什至不知道如何在 html 电子邮件中使用 ejs。我不觉得我的布局很复杂。但是,如果 html 电子邮件不支持元素,ejs 可以做些什么来设置元素的样式呢?难道我的 ejs 模板看起来和我的 html 代码一模一样吗?
  • 嗯,我知道它适用于 EJS。为什么不试一试呢?
  • 背景图片见this
  • 试过了。只是看起来完全一样。
【解决方案2】:

对于电子邮件中完全支持的背景图片,最好的资源是backgrounds.cm。此页面由 Mailchimp 创建和维护,用户可以为所有电子邮件客户端添加防弹背景。

首先在“将背景图片应用到”下选择一个选项,然后使用图片路径等填写您需要的详细信息。

您可能还需要一个插件来使您的 CSS 内联或使用在线内联。

下面是我使用Mailchimps CSS inliner的邮箱。

为了保持外观和感觉的一致性,我做了一些补充:

  • 在 td 级别和页脚段落标签中添加了字体系列。
  • 为表格添加了宽度。

<html>
    <head>
        
    </head>
    <body>
        <div class="topper" style="background-color: #003d8f;width: 100%;height: 80px;color: white;font-family: sans-serif;text-align: center;">
            <h1 style="padding-top: 20px;">Some Text Here</h1>
        </div>
        <br>
        <div class="content" style="margin: 0 auto;width: fit-content;">
            <div class="image" style="width: 293px;height: 80px;background-image: url(data:image/png;base64, data): ;background-size: contain;margin: 0 auto;"></div>
            <br>
            <br>
            <br>
            <table width="293" style="font-family: arial, sans-serif;border-collapse: collapse;">
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;"><b>DataDataDataDataDataData</b></td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">DataDataDataDataDataData</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">DataDataDataDataDataData</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Data</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">DataDataDataDataDataData</td>
                </tr>
                <tr>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">Table:</td>
                    <td style="font-family: arial, sans-serif;border-bottom-style: solid;border-color: #dddddd;border-bottom-width: 1px;text-align: left;padding-top: 13px;padding-right: 14px;">DataDataDataDataDataData</td>
                </tr>
            </table>
            <br>
            <br>
        </div>
        <br>
        <br>
        <br>
        <hr style="border-top: 1px solid #e2e1e1; border-left: 0px">
        <div class="footer content" style="margin: 0 auto;width: fit-content;">
            <p style="font-family: arial, sans-serif;width: fit-content;margin-left: auto;margin-right: auto;color: gray;font-size: small;">Here is a longer sentence. Here are a few information about the email.</p>
            <p style="font-family: arial, sans-serif;width: fit-content;margin-left: auto;margin-right: auto;color: gray;font-size: small;">Here is a longer sentence. Here are a few information about the email. This sentence is even longer but it should be.</p>
        </div>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2011-05-22
    • 2013-10-02
    • 2012-06-04
    • 1970-01-01
    • 2012-01-12
    • 2011-07-29
    • 1970-01-01
    • 2012-04-29
    相关资源
    最近更新 更多