电子邮件大文本文件

时间:2013-07-03 10:22:32

标签: python email

我有一个通过电子邮件发送的文本文件。我使用以下代码通过smtplib发送电子邮件。此代码将附件打印为电子邮件正文。由于我的文本文件有点大,所有内容在邮件正文中都不可见?如何显示电子邮件正文中的所有内容?有什么建议吗?

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg['Subject'] = 'ANALYSIS REPORT'

filename = "report.txt"
f = file(filename)
attachment = MIMEText(f.read())
msg.attach(attachment)

smtpObj = smtplib.SMTP('mail.my-domain.com', 25)
smtpObj.sendmail(sender, receivers, msg.as_string())         
print "e-mail Successfully Sent!" 

1 个答案:

答案 0 :(得分:2)

我会尝试压缩内容正文,也许会使邮件大小足以让邮件通过。

示例:

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.application import MIMEApplication
from email.MIMEImage import MIMEImage
import io
import gzip

msg = MIMEMultipart()
msg['Subject'] = 'ANALYSIS REPORT'

msg.attach(MIMEText('report attached'))

filename = "report.txt"
with open(filename, 'rb') as f, io.BytesIO() as b:
    g = gzip.GzipFile(mode='wb', fileobj=b)
    g.writelines(f)
    g.close()
    attachment = MIMEApplication(b.getvalue(), 'x-gzip')
    attachment['Content-Disposition'] = 'attachment; filename=report.txt.gz'
msg.attach(attachment)

smtpObj = smtplib.SMTP('mail.my-domain.com', 25)
print smtpObj.sendmail(sender, receivers, msg.as_string())         
print "e-mail Successfully Sent!"