Embed Plotly Graph in Email

Hello all,

Iโ€™ve searched here and found no solutions yet. I am trying to embed a plotly graph in an email. All of my email shows up except the actual plot. Does anyone have a solution for this?

Here is my code as is:

def send_email(sender: str, recipient: List[str], subject: str, attachment1: Optional[str],
               html_body: Optional[str]) -> None:
    try:
        message = MIMEMultipart('alternative')
        message["From"] = sender
        message['To'] = ", ".join(recipient)
        message["Subject"] = subject

        if html_body:
            part1 = MIMEText(html_body, "html")
            message.attach(part1)

        if attachment1:
            with open(attachment1, "rb") as attachment_file:
                part2 = MIMEBase("application", "octet-stream")
                part2.set_payload(attachment_file.read())
                encoders.encode_base64(part2)
                part2.add_header("Content-Disposition", f"attachment; filename= {os.path.basename(attachment1)}")
                message.attach(part2)

        msg = message.as_string()

        with smtplib.SMTP(link, 25) as server:
            server.sendmail(sender, recipient, msg)

    except UnicodeEncodeError as u:
        raise Exception(f'Could not encode Message Body\n{u}')

    except Exception as e:
        raise Exception(f'Something else went wrong sending the email.\n{e}')

graph_html = pio.to_html(fig=fig, full_html=False, include_plotlyjs=True)

html_text = f"""
                <!doctype html>
    <html class="no-js" lang="">

    <head>
      <meta charset="utf-8">
      <title></title>
      <meta name="description" content="">
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>

    <body>

      <!-- Add your site or application content here -->
      <p>
        <span style='font-family: "Times New Roman", Times, serif; font-size: 16px;'>
                        Hello,&nbsp;
        </span>
      </p>
      <p>
        <span style='font-family: "Times New Roman", Times, serif; font-size: 16px;'>
                        A summary of the Water Chemistry Alarm data for the previous 90 days is below. Attached to this
                        email is a spreadsheet with more details.
                    </span>
      </p>
      <script src="js/vendor/modernizr-3.11.2.min.js"></script>
      <script src="js/plugins.js"></script>
      <script src="js/main.js"></script>

      {graph_html}

      <p>
                    <span style='font-family: "Times New Roman", Times, serif; font-size: 16px;'><br></span>
                </p>
                <p style='margin-right:0in;margin-left:0in;font-size:15px;font-family:"Calibri",sans-serif;margin:0in;'>
                    <span style='font-size: 16px; font-family: "Times New Roman", Times, serif; color: red; background: yellow;'>
                        This email was autonomously generated. Do not reply to this email, as this inbox is not monitored.&nbsp;
                    </span>
                </p>
                <p>
                    <span style='font-size: 16px; font-family: "Times New Roman", Times, serif;'>
                        Please report any bugs and send your feedback to&nbsp;
                    </span>
                </p>

    </body>

    </html>
                """

send_email(sender=sender,
               recipient=contact,
               subject=f'Water Chemistry Alarms from Past 90 Days',
               html_body=html_text,
               attachment1=excel_file_path
               )

Please find an example here
Librairie used : redmail


from redmail import EmailSender

def send_mail(path_data, data, fig_img_bytes, sender, liste_diffusion, cc_diffusion, date_reporting, references_reporting):
    try:
        email = EmailSender(
            host='smtp.plotly.dom',
            port=25)

            email.send(
                subject=f"๐Ÿ“Š[{references_reporting}] Reporting Data {date_reporting}",
                sender=sender,
                receivers=liste_diffusion,
                cc=cc_diffusion,
                html= """
                <p>Please find in attached files the data reporting: <b>{{ references_reporting}}</b>.</p> \
                <p>You can find all data stored in this folder : <a href="file://plotly/">click here</a></p>  \
                <p>{{ fig_bar_chart }}</p>
                {{ data }} \
                <p>Best regards,</p> \
                <p><strong>&nbsp;</strong></p>""",
                # Embedded content
                body_images={
                    "fig_bar_chart": fig_img_bytes.to_image(format="png", engine="kaleido"),
                body_params={
                    "references_reporting": references_reporting,
                    "date_reporting": date_reporting,
                body_tables={
                    "data": data[::-1][['count_max', 'count_max_per_surface', 'flag']].head(10)},
                attachments={
                        f"{references_reporting}_0_{date_reporting}.png": fig_img_bytes.to_image(format="png", engine="kaleido"),
                }
            )
    except Exception as e:
        print(e)