Heim  >  Fragen und Antworten  >  Hauptteil

Webcrawler – Problem beim Senden von E-Mail-Anhängen mit Python+SMTP

Die Datei liegt im TXT- oder Word-Format vor, der Anhang muss jedoch im PDF-Format gesendet werden. Gibt es Parameter, die in smpt festgelegt werden können, und beim Öffnen wird schließlich ein Fehler gemeldet Der Anhang, der besagt, dass es sich nicht um eine PDF-Datei handelt, kann nicht geöffnet werden

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)

Lösung
Wenn Sie den Dateityp direkt in PDF ändern, meldet die Datei ebenfalls einen Fehler

给我你的怀抱给我你的怀抱2711 Tage vor928

Antworte allen(1)Ich werde antworten

  • 漂亮男人

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

    Antwort
    0
  • StornierenAntwort