[英]Attachment Missing python 2.7
我正在嘗試使用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"
您正在讀取要以二進制模式附加的文件,以便您的encodedcontent
為bytes
類型(可以)。 然后,將其格式化而不轉換為字符串。 結果,您的message
包含b'....'
(不正確)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.