2017-04-26 1 views
0

Ich versuche SMTP zu verwenden, um E-Mail mit Anhang zu senden. Und wenn ich rohe E-Mails bekomme, gibt es zwei Inhaltstypen für einen Anhang. Wie kann ich nur einen Inhaltstyp bekommen? Und die beiden Typen beeinflussen sich gegenseitig? Danke für jede Hilfe!SMTP senden E-Mail und warum ein Anhang zwei Content-Type haben kann?

import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.application import MIMEApplication 

server = smtplib.SMTP() 
server.connect("smtp.XX.com") 

server.login("","") 

msg = MIMEMultipart("") 
msg['From'] = "" 
msg['Subject'] = "titlesub" 
part = MIMEApplication(open("D:\data.txt", 'rb').read()) 
filename="data.txt" 
#part['Content-Type']="application/pdf" 
part.add_header('Content-Type','application/pdf') 
part.add_header('Content-Disposition', 'attachment', filename=filename) 

msg.attach(part) 
msg['To'] = "" 
server.send_message(msg) 
server.quit() 

Raw E-Mail:

Received: from [127.0.0.1] (unknown [101.81.225.242]) 
by smtp8 (Coremail) with SMTP id DMCowABH3zUeOgBZsU+uAg--.2242S2; 
Wed, 26 Apr 2017 14:11:42 +0800 (CST) 
Content-Type: multipart/; boundary="===============4516509904929376112==" 
MIME-Version: 1.0 
From: 
Subject: titlesub 
To: 
X-CM-TRANSID:DMCowABH3zUeOgBZsU+uAg--.2242S2 
Message-Id:<[email protected]> 
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 
VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUkebkUUUUU 
X-Originating-IP: [101.81.225.242] 
Date: Wed, 26 Apr 2017 14:11:42 +0800 (CST) 
X-CM-SenderInfo: pix130tbbsiiqu6rljoofrz/1tbivh7F0FZcM5OV1wAAsd 

--===============4516509904929376112== 
Content-Type: application/octet-stream 
MIME-Version: 1.0 
Content-Transfer-Encoding: base64 
Content-Type: application/pdf 
Content-Disposition: attachment; filename="data.txt" 

77u/ 

--===============4516509904929376112==-- 

Antwort

0

Wenn man sich die documentation for the MIMEApplication class aussehen, sollten Sie den MIME-Typ im Konstruktor sein vorbei, nicht als separate Header hinzugefügt wird.

part = MIMEApplication(open("file.pdf", 'rb').read(), 'pdf') 
filename="file.pdf" 
part.add_header('Content-Disposition', 'attachment', filename=filename) 
+0

Wenn ich Teil [ 'Content-Type'] verwenden = 'application/pdf' Content-Type (application/octet-stream) in Header dict wird nicht geändert? – Bella

+0

application/octet-stream ist ein Standard für den Fall, dass kein MIME-Typ bekannt ist. In diesem Fall wissen Sie, dass Sie eine PDF-Datei anhängen, daher sollten Sie die PDF-MIME-Datei verwenden. Siehe: https://tools.ietf.org/rfc/rfc3778 – gview

+0

Ich versuche es (Teil = MIMEApplication (open ("D: \ data.txt", "rb"). Lesen(), "Anwendung/pdf"))! Und der Attachment-Content-Type ändert sich in Anwendung/Anwendung! – Bella

Verwandte Themen