【问题标题】:How to send html table content in an outlook email using python exchangelib如何使用 python exchangelib 在 Outlook 电子邮件中发送 html 表格内容
【发布时间】:2021-11-05 19:48:02
【问题描述】:

我正在尝试通过 python exchangelib 包将 HTML 表格发送到 Outlook 电子邮件中。但是,当在交付端收到电子邮件时,它会将 HTML 内容显示为纯文本。这一定是什么原因?我们如何在 exchangelib 中设置 MIME 版本和内容类型。

实施

   def create_template(self):
     return
"""
<!DOCTYPE html>
<html>
<head>
<style>
#customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #04AA6D;
  color: white;
}
</style>
</head>
<body>

<table id="customers">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Königlich Essen</td>
    <td>Philip Cramer</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>Simon Crowther</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris spécialités</td>
    <td>Marie Bertrand</td>
    <td>France</td>
  </tr>
</table>

</body>
</html>

"""

    def send_mail(self, send_to=email_config.to_mail):
        try:
            self._to_mail = email_config.to_mail
            if len(email_config.cc_mail):
                self._cc_mail=email_config.cc_mail

            if self._to_mail is None:
                raise ReportException(ErrorLevel.ERROR, DeliveryErrors.MailRecipientNotSet)
            recipients = self.prepare_recipient_list(self._to_mail)

            email_body = self.create_html_body()
            email_message = Message(
                account=self._account,
                subject=self._subject_prefix + self._mail_subject,
                body=email_body,
                to_recipients=recipients,
                cc_recipients=self._cc_mail
            )

            if self._attachment_file is not None:
                email_attachment = self.get_attachment()
                email_message.attach(email_attachment)
            email_message.send()  # send_and_save()
        except Exception as e:
            print(e)

如果有人能确定为什么它没有按照我们在电子邮件中的要求呈现,我们将不胜感激。请给我一个关于如何在 exchangelib 中实现 mime 和 content-type 的建议

【问题讨论】:

    标签: python html-email mime-types exchangelib


    【解决方案1】:

    要将您的电子邮件正文标记为 HTML,您需要创建一个 HTMLBody 实例:email_body = HTMLBody(self.create_html_body())。见https://ecederstrand.github.io/exchangelib/#creating-updating-deleting-sending-moving-archiving-marking-as-junk

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      相关资源
      最近更新 更多