【问题标题】:HTML and Text in Python Driven EmailPython 驱动的电子邮件中的 HTML 和文本
【发布时间】:2017-08-08 00:54:57
【问题描述】:

我正在使用 MIMEMultipart 从 Python 发送电子邮件。代码如下:

sender = "EMAIL"
recipients = ["EMAIL"]
msg = MIMEMultipart('alternative')
msg['Subject'] = "Subject Text"
msg['From'] = sender
msg['To'] = ", ".join(recipients)

html = PandasDataFrame.to_html()
part2 = MIMEText(html, 'html')
msg.attach(part2)

SERVER = "SERVER"
server = smtplib.SMTP(SERVER)
server.sendmail(sender, recipients, msg.as_string())
server.quit()  

这会将 Python Pandas 数据框插入为 HTML 并且工作正常。是否也可以将脚注作为文本添加到电子邮件正文中?代码将如何同时进行?或者,我可以将 cmets 添加为 HTML,但更不需要在电子邮件正文中添加一些脚注。

谢谢

【问题讨论】:

  • 只要这不是垃圾邮件脚本:P
  • @CharlieG 否,用于内部工作流程自动化。根据来自 Pandas Dataframe 的动态更新数据发送警报,但需要电子邮件中的说明,以便人们了解其含义。
  • 有任何结果吗?如果正确,请投票或接受答案!
  • @CharlieG 很抱歉现在看到这个给我一些尝试一下。谢谢
  • @CharlieG 我发布了正确的代码。如何在不改变您声誉的情况下删除您的答案。 Downarrow 似乎暗示我可能会说你没有帮助。谢谢

标签: python html email pandas mime


【解决方案1】:

此代码有效:

首先,导入:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication #Used for attachments
import smtplib

还有代码:

sender = "EMAIL"
recipients = ["EMAIL1","EMAIL2"]
msg = MIMEMultipart('mixed') #use mixed instead of alternative to load multiple things 
msg['Subject'] = "Subject Text"
msg['From'] = sender
msg['To'] = ", ".join(recipients)

html = PandasDataFrame1.to_html() #first dataframe

 #insert text as follows
html += '''
    <br><br>
    This is a new line of random text.
    <br><br>
'''

html += PandasDataFrame2.to_html() #second dataframe

#put the html into the email body
html = MIMEText(html, 'html') 
msg.attach(html)

如果您还想在电子邮件中附加文件,请使用此代码

ATTACHMENT_PATH = 'path\\file.type'
with open(ATTACHMENT_PATH, 'r') as fileobj:
    attachment = MIMEApplication(fileobj.read(), Name='file.type')
attachment['Content-Disposition'] = 'attachment; filename="file.type"'
msg.attach(attachment)

以及使用服务器发送的代码

SERVER = "SERVER"
server = smtplib.SMTP(SERVER)
server.sendmail(sender, recipients, msg.as_string())
server.quit()  

【讨论】:

    【解决方案2】:

    所以下面没有工作,请查看the correct answer

    也许是这个?

    html = pd.DataFrame([[1,2,3], ['dog', 'cat', 42]]).to_html()
    part1 = MIMEText(html, 'html')
    msg.attach(part1)
    part2 = MIMEText('html')
    coolstring = 'This is a dope-ass DataFrame yo'
    part2.set_payload(coolstring)
    msg.attach(part2)
    

    虽然我认为它与下面的#2 太相似了。输出如下:

    >> print msg
    # ...header with my email whoops...
    --===============0888735609==
    Content-Type: text/html; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    <table border="1" class="dataframe">
        #...DataFrame html...
    </table>
    --===============0888735609==
    Content-Type: text/plain; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    This is a dope-ass DataFrame yo
    --===============0888735609==--
    

    通过the examples 找到了几种方法,并通过dir(MIMEMultipart) 等价物列出了MIMEMultipart 上的方法

    你必须测试的三个猜测:

    1) 你可以通过

    设置结语
    msg.epilogue = 'This is a dope-ass DataFrame yo' 
    

    但不确定这在电子邮件正文中的位置或是否结束。

    2) 创建另一个 MIMEText 并附上它。这似乎是他们在示例中一次完成发送大量图片的方式,因此这可能是您最好的选择。可能应该领导这一点。

    part_text = MIMEText('This is some text, yessir')
    msg.attach(part_text)
    

    看起来有点像到达那里,因为边界划分是相同的。

    >> print msg.as_string()
    Content-Type: multipart/alternative; boundary="===============1672307235=="
    MIME-Version: 1.0
    Subject: Subject Text
    From: EMAIL
    To: EMAIL
    --===============1672307235==
    Content-Type: text/html; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    <table border="1" class="dataframe">
      # ...DataFrame in HTML here...
    </table>
    --===============1672307235==
    Content-Type: text/plain; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    This is some text, yessir
    --===============1672307235==--
    

    3) 因为要在server.sendmail(sender, recipients, msg.as_string()) 中实际发送它,所以您将msg 转换为字符串,您的另一个选择是直接手动将一些HTML 文本添加到msg.as_string()。类似的东西

    msg.as_string().replace('</table>', '</table>\n<p>...your text here</p>')
    

    会很乱,但应该可以。

    如果这些有帮助,请告诉我!我有点在黑暗中拍摄,因为我现在无法测试它。祝你好运!

    【讨论】:

    • 三个都试过了,都没有用。 #1 和 #2 运行良好,但生成的电子邮件仍然只有数据框,没有文本。 #3 抛出错误:“无效语法”。似乎在 as_string() 创建服务器退出失败的错误之后添加代码。所以也许我实施错了?
    • 嗯可能不是,反正我只是在猜测。让我尝试一下,将其发送给自己并回复您?
    • @GC123 smtplib.SMTP(SERVER) 来自什么包?我还要指定什么服务器?
    • 已更新。那行得通吗?由于某些原因,我无法将 smtplib 导入我的工作计算机
    • 不,不幸的是这也不起作用。我联系了 stackoverflow 上的联系人,他应该能够提供帮助(但可能需要一三天)。当我有它时会发布工作代码。同时欢迎任何人的任何其他想法。谢谢
    猜你喜欢
    • 2014-09-26
    • 2023-04-01
    • 2011-05-02
    • 1970-01-01
    • 2015-05-13
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多