-1

I'm trying to send an HTML table into outlook email through the python exchangelib package. However, when the email is received on the delivery end it is showing HTML contents as plain text. What must be the reason for this? How we can set up the MIME version and content type in exchangelib.

Implementation

   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)

It would be greatly appreciated if someone could identify why it is not rendering as we required in the email. Kindly provide me a suggestion on how to implement mime & content-type in exchangelib

Aaditya R Krishnan
  • 495
  • 1
  • 10
  • 31

1 Answers1

0

To mark your email body as HTML, you need to create an HTMLBody instance: email_body = HTMLBody(self.create_html_body()). See https://ecederstrand.github.io/exchangelib/#creating-updating-deleting-sending-moving-archiving-marking-as-junk

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63