You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python生成的Reddit报告嵌入自定义HTML邮件模板?

Solution to Embed Reddit DataFrames into Styled HTML Email Template

Got it, let's get your polished email working properly. Here's a straightforward, step-by-step approach to embed your generated DataFrame HTMLs into your custom template:


Step 1: Prepare Your HTML Template with a Placeholder

First, update your existing HTML template to include a clear placeholder where you want the DataFrame content to live. I’ll add <!-- REDDIT_CONTENT --> right after the "Top Reddit Posts" heading—this makes it simple to swap in your dynamic content later. I’ve also added the missing closing tags to your provided snippet to ensure it’s valid HTML:

# Define your styled template with a content placeholder
html_template = """
<table width="689" border="0" cellspacing="0" cellpadding="1" align="center" bgcolor="#353A71">
  <tr>
    <td valign="middle" align="center">
      <table width="689" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" align="center">
        <tr align="left">
          <td valign="top" colspan="2">
            <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
              <tr>
                <td width="0%">&nbsp;</td>
                <td valign="top" width="100%">
                  <center><h1 style="font-family:helvetica;">Top Reddit Posts</h1></center>
                  <!-- REDDIT_CONTENT --> <!-- This is our dynamic content placeholder -->
                </td>
                <td width="0%">&nbsp;</td>
              </tr>
              <tr>
                <td width="0%">&nbsp;</td>
                <td>&nbsp;</td>
                <td width="0%">&nbsp;</td>
              </tr>
              <tr>
                <td width="0%" bgcolor="#FFFFFF">&nbsp;</td>
                <td align="center" class="profileCaptionWhiteBold" width="100%" valign="top" bgcolor="#FFFFFF">
                </td>
                <td width="0%" bgcolor="#FFFFFF">&nbsp;</td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
"""

Step 2: Combine Your DataFrame HTMLs

Take the three rendered HTML blocks from your DataFrames and combine them into a single section. Add section headers and spacing to make the email easy to scan:

# Combine DataFrame HTMLs with visual separators
combined_content = f"""
<h3 style="font-family:helvetica; color:#353A71; margin-top:20px;">Test Posts</h3>
{test_html}
<br><br>
<h3 style="font-family:helvetica; color:#353A71;">Brand Posts</h3>
{brand_html}
<br><br>
<h3 style="font-family:helvetica; color:#353A71;">Competitor Posts</h3>
{competitor_html}
"""

Step 3: Inject Content into the Template

Swap the placeholder in your template with the combined DataFrame content:

# Replace placeholder with actual dynamic content
final_email_html = html_template.replace("<!-- REDDIT_CONTENT -->", combined_content)

Step 4: Update Your Email Sending Logic

Modify your email code to handle both empty and non-empty data cases, and send the styled HTML instead of plain text:

# Email Integration
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import date

today = str(date.today())
fromaddr = "email@email.com"
toaddr = "email@email.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Daily Reddit Monitor " + today

if test_test_df.empty:
    # Send plain text alert if no data exists
    message = "There are no relevant posts above the 100 score threshold today!"
    email_part = MIMEText(message, 'plain')
else:
    # Send styled HTML email with embedded DataFrames
    email_part = MIMEText(final_email_html, 'html')

msg.attach(email_part)

# Connect to SMTP server and dispatch the email
server = smtplib.SMTP('smtp.postmarkapp.com', 587)
server.starttls()
server.login('API-KEY', 'API-KEY')
email_text = msg.as_string()
server.sendmail(fromaddr, toaddr, email_text)
server.quit()

Quick Tips for Better Email Rendering:

  • Keep CSS Inline: Email clients often ignore external stylesheets, so sticking to inline styles (like you already have) ensures consistent formatting.
  • Test Across Clients: Preview your email in tools like Litmus or even just Gmail/Outlook to fix any rendering quirks.
  • Simplify Tables: Email clients handle tables better than modern flexbox/grid layouts, so your existing table-based template is a great choice.

内容的提问来源于stack exchange,提问作者Piyush Patil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:58:17