我正在使用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插入并正常工作。是否可以将脚注作为文本添加到电子邮件正文中?这两个代码如何工作?或者,我可以将评论添加为HTML,但更不用说需要在电子邮件正文中添加一些脚注。
谢谢
答案 0 :(得分: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()
答案 1 :(得分:0)
也许这个?
>> 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==--
虽然我认为它与下面的#2太相似了。输出如下:
MIMEMultipart
找到了几种查看the examples的方法,并在dir(MIMEMultipart)
上列出了相当于msg.epilogue = 'This is a dope-ass DataFrame yo'
的方法
您需要测试的三个猜测:
1)您可以通过
设置结语part_text = MIMEText('This is some text, yessir')
msg.attach(part_text)
然而,不确定电子邮件正文中的最终位置是什么?
2)创建另一个MIMEText并附加它。这似乎是他们如何在示例中一次性完成发送大量图片,所以这可能是您最好的选择。可能应该带领这一点。
>> 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==--
由于边界划分相同,它看起来有点像。
server.sendmail(sender, recipients, msg.as_string())
3)由于要在msg
中实际发送,您将msg.as_string()
转换为字符串,您的另一个选择是直接手动将一些HTML文本添加到msg.as_string().replace('</table>', '</table>\n<p>...your text here</p>')
。像
PhotoCell *cellToUpdate = (id)[self.collectionView cellForItemAtIndexPath:indexPath];
if (cellToUpdate) {
cellToUpdate.imageView.image = thumbnail;
} else {
NSLog(@"cell is no long visible");
[self.collectionView reloadData];
}
会很乱,但应该有用。
如果有任何有用的,请告诉我们!我有点在黑暗中拍摄,因为我现在无法测试它。祝你好运!