Home  >  Q&A  >  body text

Web crawler - problem with sending email attachments with python+smtp

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

给我你的怀抱给我你的怀抱2711 days ago927

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人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.

    reply
    0
  • Cancelreply