The file is in txt or word format, but the attachment is required to be sent in pdf format. Are there any parameters that can be set for smpt? I set _subtype="pdf", and finally an error will be reported when opening the attachment, saying it is not one. pdf file, cannot be opened
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)
Solution
If you change the filetype directly to pdf, the file will also report an error
漂亮男人2017-05-18 11:03:40
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.