Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

文件是txt或者word格式的,但是要求附件发送过去是pdf格式的,smpt有没有什么参数是可以设置的,我设置了_subtype="pdf",最后附件打开会报错,说不是一个pdf文件,打不开

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import traceback
import os
server=smtplib.SMTP()
server.connect("smtp.163.com")
server.login("XXXXXX@163.com","YYYYYY")
msg=MIMEMultipart('')
msg['From']="XXXXXX@163.com"
msg['Subject']="opp"
part = MIMEApplication(open("D:log.txt", 'rb').read(),_subtype='pdf')
#filetype="pdf"
filetype = os.path.splitext("D:log.txt")[-1][1:]
newfilename = 'resume' + '.' + filetype
part.add_header('Content-Disposition', 'attachment', filename=newfilename)
msg.attach(part)
msg['To']="TTTTTT@163.com"
server.send_message(msg)

求解
直接报filetype改成pdf也会文件报错


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
309 views
Welcome To Ask or Share your Answers For Others

1 Answer

SMTP is the protocol you are sending the completed email with, the MIME type is the content type of the attachment as declared in the email and the actual content type the file has. If you want to send a doc file as pdf you have to convert it first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...