Python 邮件:编码附件被截断

发布于 2024-12-01 06:41:51 字数 1493 浏览 1 评论 0原文

我正在使用以下函数在 python 脚本中发送带有两个附件的电子邮件:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

...

def sendMail(sender_name, to, subject, text, files=None,server="localhost"):
      assert type(to)==list
      if files:
        assert type(files)==list
      print "Files: ",files
      fro = sender_name

      msg = MIMEMultipart()
      msg['From'] = fro
      msg['To'] = COMMASPACE.join(to)
      msg['Date'] = formatdate(localtime=True)
      msg['Subject'] = subject

      msg.attach( MIMEText(text) )

      if files:
        for file in files:
# ************** File attaching - Start **************
                part = MIMEBase('application', "octet-stream")
                part.set_payload( open(file,"rb").read() )
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
                msg.attach(part)
# ************** File attaching - End **************

      server.set_debuglevel(1)
      server.ehlo()
      server.starttls()
      server.ehlo()
      server.sendmail(fro, to, msg.as_string())
      server.quit()

我收到邮件,附件在那里,但由于某种原因,它们被截断了一点。我的猜测是我在编码过程中遗漏了一些东西。

例如:

附件 1:原始文件字节数为 1433902,而新字节数为 1433600

附件 2:原始文件字节数为 2384703,而新字节数为 2383872

有什么想法吗?

I'm using the following function to send an email message with two attachments in my python script:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

...

def sendMail(sender_name, to, subject, text, files=None,server="localhost"):
      assert type(to)==list
      if files:
        assert type(files)==list
      print "Files: ",files
      fro = sender_name

      msg = MIMEMultipart()
      msg['From'] = fro
      msg['To'] = COMMASPACE.join(to)
      msg['Date'] = formatdate(localtime=True)
      msg['Subject'] = subject

      msg.attach( MIMEText(text) )

      if files:
        for file in files:
# ************** File attaching - Start **************
                part = MIMEBase('application', "octet-stream")
                part.set_payload( open(file,"rb").read() )
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
                msg.attach(part)
# ************** File attaching - End **************

      server.set_debuglevel(1)
      server.ehlo()
      server.starttls()
      server.ehlo()
      server.sendmail(fro, to, msg.as_string())
      server.quit()

I get the mail, and the attachments are there, but for some reason, they are truncated a bit. My guess is I'm missing something in the encoding process.

For example:

Attachment 1: Original file byte count is 1433902, while the new byte count is 1433600

Attachment 2: Original file byte count is 2384703, while the new byte count is 2383872

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

预谋 2024-12-08 06:41:51

发现问题了。结果我尝试在写入过程的缓冲区完全刷新之前发送文件。

因此,这是一个同步问题,而不是编码问题。

对此深表歉意,并感谢各位的帮助!

Found the problem. Turns out I tried sending the files before the buffer of the writing process was fully flushed.

So, it was a synchronization issue and not an encoding issue.

Sorry about that, and thanks for the help guys!

倾听心声的旋律 2024-12-08 06:41:51

它可能与您当前的 base64.MAXBINSIZE 有关吗? Encoders.encode_base64 在内部使用 base64.encodestringbase64.MAXBINSIZE 的默认值为 57,可以随时尝试将其设置得更大:base64.MAXBINSIZE = 65536

Could it be related to your current base64.MAXBINSIZE? Encoders.encode_base64 uses base64.encodestring internally. The default value for base64.MAXBINSIZE is 57, can always try setting it larger: base64.MAXBINSIZE = 65536

缱绻入梦 2024-12-08 06:41:51

如果文件已写入,请务必 .close() 文件并为有效负载重新 open()/.read() 文件。

我的问题源于时间安排,这为我解决了问题。

If the file is already written--be sure to .close() the file and re-open()/.read() it for the payload.

My issues stemmed from timing and this solved the issue for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文