无法使通过电子邮件发送的HTML模板居中。

huangapple go评论79阅读模式
英文:

Unable to center an HTML template sent by email

问题

我正在使用React和MUI开发一个Web项目,代码会获取用户输入的信息,然后传递给HTML模板,最后通过电子邮件发送。我遇到的问题之一是,当我查看电子邮件时,HTML内容未垂直居中(参见图片)1。为了使HTML内容垂直居中,我正在使用以下CSS:

display: flex;
justify-content: center !important;

奇怪的是,如果我测试HTML,它能正常工作,但是当我在Chrome或Firefox中查看电子邮件时,它不起作用。通过检查开发者工具,发现justify-content: center !important这个值从未出现。如果我手动添加它,它将使内容垂直居中。

以下是代码示例:

import dgoow from "../images/dgo 2.png";

export const emailBody = (event, date, channels, priority, countries) => {
  return `
<html>
<head>
  <style>
    * {
      font-family: Helvetica, Arial, sans-serif;
    }
    h1 {
      color: white;
    }
    h3,
    h5 {
      color: white;
    }
    table {
      border: none;
    }
    button {
      color: white;
      border-radius: 5px;
      font-family: Helvetica, Arial, sans-serif;
      font-weight: bold;
      font-size: 16px;
      cursor: pointer;
      background-color: #fd8204;
      border: none;
      padding: 1rem;
      margin-bottom: 2rem;
    }
    img {
      height: 70px;
      width: 150px;
    }
    #myElement{
      display: flex;
      justify-content: center !important;
    }
  </style>
</head>
<body id="myElement">
  <div id='dos' style="background-color: #050914; border-radius: 10px; padding: 3rem; width: 800px; margin: 3rem;"> 
  <table>
      <tr>
        <td style="text-align: start; padding: 1rem; width: 50%; position: relative;">
          <img src=${dgoow} alt="DWEGO logo" />
        </td>
        <td style="text-align: end; padding: 1rem; width: 50%; position: relative;">
          <img src=${dgoow} alt="DWEGO logo" />
        </td>
      </tr>
      <!-- 其他内容 -->
    </table>
  </div>
</body>
</html>`;
};

另一个问题是关于图片,它们未加载,原因不明。

英文:

I am working on a web project with react and MUI, the code takes information that a user writes and pass it to an HTML template then it sends it by email. One of the problems I have is that when I check the email the HTML is not vertically center (see image)1. To center the HTML I am using:

display: flex;
justify-content: center !important

What is odd is that if I test the HTML, it works, but when I check the email either on chrome or Firefox it does not work. Checking the devtools the value justify-content: center !important is never there. If I added manually it will center the content the way I wanted.

This is the code:

import dgoow from &quot;../images/dgo 2.png&quot;;

export const emailBody = (event, date, channels, priority, countries) =&gt; {
  return `
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
    * {
      font-family: Helvetica, Arial, sans-serif;
    }
    h1 {
      color: white;
    }
    h3,
    h5 {
      color: white;
    }
    table {
      border: none;
    }
    button {
      color: white;
      border-radius: 5px;
      font-family: Helvetica, Arial, sans-serif;
      font-weight: bold;
      font-size: 16px;
      cursor: pointer;
      background-color: #fd8204;
      border: none;
      padding: 1rem;
      margin-bottom: 2rem;
    }
    img {
      height: 70px;
      width: 150px;
    }
    #myElement{
      display: flex;
      justify-content: center !important;
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body id=&quot;myElement&quot;&gt;
  &lt;div id=&#39;dos&#39; style=&quot;background-color: #050914;border-radius: 10px;padding: 3rem;width: 800px;margin: 3rem;&quot;&gt; 
  &lt;table&gt;
      &lt;tr&gt;
        &lt;td style=&quot;text-align: start; padding: 1rem; width: 50%; position: relative;&quot;&gt;
          &lt;img src=${dgoow} alt=&quot;DWEGO logo&quot; /&gt;
        &lt;/td&gt;
        &lt;td style=&quot;text-align: end; padding: 1rem; width: 50%; position: relative;&quot;&gt;
          &lt;img src=${dgoow} alt=&quot;DWEGO logo&quot; /&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 20px&quot;&gt;
          &lt;h1&gt;HWRTE Notification&lt;/h1&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 20px&quot;&gt;
          &lt;h3&gt;Event: ${event}&lt;/h3&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 20px&quot;&gt;
          &lt;h3&gt;Date: ${date}&lt;/h3&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 20px&quot;&gt;
          &lt;h3&gt;Channels: ${channels}&lt;/h3&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 20px&quot;&gt;
          &lt;h3&gt;Priority: ${priority}&lt;/h3&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; style=&quot;text-align: center; font-size: 20px&quot;&gt;
          &lt;h3&gt;Countries: ${countries}&lt;/h3&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;
          &lt;a href=&quot;www.google.com&quot; target=&quot;_blank&quot;&gt;
            &lt;button&gt;JOIN THE BRIDGE&lt;/button&gt;
          &lt;/a&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;`;
};

Another error I have is with the images, they are not loading not sure why.

答案1

得分: 0

根据这个帖子,大多数邮件客户端不支持 display:grid/flex

我用以下CSS替换了:

#myElement{
  display: flex;
  justify-content: center !important;
}

替换成了:

#myElement{
  text-align:center&quot;;      
}

这样可以运行。

如果你正在使用nodemailer发送邮件,你可以参考这个页面来发送嵌入式图片。

英文:

According to this post, the display:grid/flex is not supported by most email clients.

I replaced the following CSS:

#myElement{
  display: flex;
  justify-content: center !important;
}

with

#myElement{
  text-align:center&quot;;      
}

It works.

If you are using the nodemailer to send email, you can refer to this page for sending embedded images.

huangapple
  • 本文由 发表于 2023年6月29日 06:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定