附件缺少python 2.7

时间:2017-06-08 07:10:24

标签: 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

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"

1 个答案:

答案 0 :(得分:0)

您正在阅读要以二进制模式附加的文件,因此encodedcontent的类型为bytes(可以)。然后你格式化它而不转换为字符串。因此,您的message包含b'....'(不正常)。