为什么通过Python发送电子邮件没有正确使用CSS?

问题描述 投票:0回答:1

我尝试通过 smtplib 发送电子邮件。它可以正确地与其他 html/css 代码一起使用,但在我的情况下无法正常工作。这是我的 Python 代码:

smtp_server = smtplib.SMTP("smtp.gmail.com", 587)
smtp_server.starttls()
smtp_server.login(email, password)

message = EmailMessage()
message["From"] = email
message["To"] = recipient
message["Subject"] = "Test"
with open("test.html", "r") as file:
    text = file.read()

message.set_content(text, subtype="html")

smtp_server.send_message(message)
smtp_server.quit()

这是我的html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <style type="text/css">
.email-confirmation {
  background-color: #fff;
  display: flex;
  max-width: 480px;
  width: 100%;
  flex-direction: column;
  overflow: hidden;
  color: #000;
  margin: 0 auto;
  padding: 91px 69px 330px;
  font: 15px Inter, sans-serif;
}

.logo {
  aspect-ratio: 7.81;
  object-fit: contain;
  object-position: center;
  width: 179px;
  align-self: center;
  max-width: 100%;
}

.confirmation-title {
  font-size: 20px;
  font-weight: 600;
  align-self: start;
  margin-top: 51px;
}

.confirmation-message {
  font-weight: 300;
  margin-top: 24px;
}

.confirmation-code {
  text-align: center;
  border-radius: 30px;
  background-color: #000;
  align-self: center;
  margin-top: 55px;
  width: 215px;
  max-width: 100%;
  font-size: 35px;
  color: #fff;
  font-weight: 700;
  padding: 7px 35px;
}

.signature {
  font-weight: 500;
  align-self: start;
  margin-top: 40px;
}
</style>

<section class="email-confirmation">
  <img loading="lazy" src="url">
  <h1 class="confirmation-title">Подтверждение почты</h1>
  <p class="confirmation-message">
    Это письмо отправлено, чтобы <br />
    подтвердить электронную почту. <br />
    <br />
    Если это были не Вы, проигнорируйте данное письмо. <br />
    <br />
    Для завершения подтверждения - <br />
    вернитесь к боту и введите этот код.
  </p>
  <div class="confirmation-code" tabindex="0" role="textbox" aria-label="Confirmation code">173 586</div>
  <p class="signature">С уважением, команда Bard!</p>
</section>
</body>
</html>

但是如果我通过浏览器打开文件,CSS 会正确加载,但在 gmail 中则不会。

我能做什么? 这是浏览器视图 这是 GMAIL 中的视图

真的能实现吗?

python html-email smt-lib
1个回答
0
投票

这是因为你的认识有根本性的缺陷。 Gmail 等电子邮件客户端不是网络浏览器。它们不根据正常的 Web 标准呈现代码。它们本身就是法律。

您需要检查 caniemail.com 了解 Gmail 可以使用哪些功能。

另请注意,如果它适用于 Gmail,则不一定适用于其他电子邮件客户端。

您可能更喜欢获得已经尝试和测试过的片段,因为那里有很多片段。

© www.soinside.com 2019 - 2024. All rights reserved.