附件缺少python 2.7 此脚本中缺少附件

附件缺少python 2.7 此脚本中缺少附件,python,email,smtp,Python,Email,Smtp,我正在尝试使用python脚本发送附件,但我没有收到附件,它丢失了,请告诉我如何获取现在丢失的附件,以及此代码中的错误是什么 import smtplib import base64 filename = "D:\python/Files_List.txt" fo = open("Files_List.txt", "rb") filecontent = fo.read() encodedcontent = base64.b64encode(filecontent) # base64 se

我正在尝试使用python脚本发送附件,但我没有收到附件,它丢失了,请告诉我如何获取现在丢失的附件,以及此代码中的错误是什么

import smtplib
import base64

filename = "D:\python/Files_List.txt"

fo = open("Files_List.txt", "rb")
filecontent = fo.read()
encodedcontent = base64.b64encode(filecontent)  # base64

sender = 'rom@domain.com'
reciever = 'to@domain.com'

marker = "AUNIQUEMARKe"

body ="""
This is a Attachment Mail
"""
part1 = """From:from<from@domain.com>
To:to<to@domain.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body,marker)

part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s

%s--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, reciever, message)
   print "Successfully sent email"
except Exception:
   print "Error: unable to send email"
导入smtplib
导入base64
filename=“D:\python/Files\u List.txt”
fo=打开(“Files_List.txt”、“rb”)
filecontent=fo.read()
encodedcontent=base64.b64编码(文件内容)#base64
发送者rom@domain.com'
接受者to@domain.com'
marker=“AUNIQUEMARKe”
body=”“”
这是一封附件邮件
"""
part1=“”发件人:发件人
致:致
主题:发送附件
MIME版本:1.0
内容类型:多部分/混合;边界=%s
--%
“”“%(标记,标记)
part2=“”内容类型:文本/普通
内容传输编码:8比特
%
--%
“”“%(正文,标记)
part3=“”内容类型:多部分/混合;名称=\%s\”
内容传输编码:base64
内容处置:附件;文件名=%s
%s--%s--
“”%(文件名、文件名、编码内容、标记)
消息=第1部分+第2部分+第3部分
尝试:
smtpObj=smtplib.SMTP('localhost')
smtpObj.sendmail(发送者、接收者、消息)
打印“已成功发送电子邮件”
除例外情况外:
打印“错误:无法发送电子邮件”

您正在以二进制模式读取要附加的文件,因此您的
encodedcontent
类型为
bytes
(没关系)。然后格式化它,而不转换为字符串。因此,您的
邮件
包含
b'..'
(这不好)。

您收到过这封邮件吗?如果是的话,它看起来怎么样?是否存在预期的标题?身体部位是否存在?我收到了邮件,但只有身体部位……“这是附件邮件”。