2010-05-09 15 views
6

पायथन 3.1.2 का उपयोग करके मुझे द्विआधारी अनुलग्नक फ़ाइलों (जेपीईजी, पीडीएफ, आदि) भेजने में समस्या आ रही है - MIMEText अनुलग्नक ठीक काम करते हैं। सवाल में कोड इस प्रकार है ...बाइनरी फ़ाइल ईमेल अटैचमेंट समस्या

for file in self.attachments: 
    part = MIMEBase('application', "octet-stream") 
    part.set_payload(open(file,"rb").read()) 
    encoders.encode_base64(part) 
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % file) 
    msg.attach(part) # msg is an instance of MIMEMultipart() 

server = smtplib.SMTP(host, port) 
server.login(username, password) 
server.sendmail(from_addr, all_recipients, msg.as_string()) 

हालांकि, जिस तरह से बुला-स्टैक में नीचे (नीचे ट्रैस बैक देखें) के रूप में यद्यपि msg.as_string() एक अनुलग्नक जिनमें से एक पेलोड बनाता प्राप्त हुआ है, ऐसा लगता है स्ट्रिंग के बजाय 'बाइट्स' प्रकार।

क्या किसी को भी कोई विचार है कि समस्या का कारण क्या हो सकता है? किसी भी सहायता की सराहना की जाएगी।

एलन


builtins.TypeError: string payload expected: <class 'bytes'> 
File "c:\Dev\CommonPY\Scripts\email_send.py", line 147, in send 
    server.sendmail(self.from_addr, all_recipients, msg.as_string()) 
File "c:\Program Files\Python31\Lib\email\message.py", line 136, in as_string 
    g.flatten(self, unixfrom=unixfrom) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 76, in flatten 
    self._write(msg) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 101, in _write 
    self._dispatch(msg) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 127, in _dispatch 
    meth(msg) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 181, in _handle_multipart 
    g.flatten(part, unixfrom=False) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 76, in flatten 
    self._write(msg) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 101, in _write 
    self._dispatch(msg) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 127, in _dispatch 
    meth(msg) 
File "c:\Program Files\Python31\Lib\email\generator.py", line 155, in _handle_text 
    raise TypeError('string payload expected: %s' % type(payload)) 
+0

एक समाधान के वैसे से समाधान (अच्छी तरह से, एक संभावित समाधान, मुझे वास्तव में कोई जानकारी नहीं है कि यह मदद करेगा), आप 'MIMEBase' –

+0

के बजाय 'MIMEAplication' का उपयोग करने का प्रयास कर सकते हैं हाय डेविड उत्तर के लिए धन्यवाद। मैंने MIMEAplication का प्रयास किया, लेकिन इसका कोई फायदा नहीं हुआ (यानी msg। Msg.get_payload() अभी भी स्ट्रिंग के बजाय बाइट्स देता है)। मुझे संदेह है कि यह एन्कोडिंग के साथ कुछ करने के लिए बाइनरी फ़ाइल को स्ट्रिंग में सही ढंग से परिवर्तित नहीं कर रहा है, लेकिन मैं गलत हो सकता हूं। मुझे यह भी चिंता है कि मैंने विभिन्न वेब साइटों से कई समान उदाहरणों की कोशिश की है - वे सभी मेरे लिए असफल हैं, लेकिन लेखक के लिए काम करना चाहिए। –

उत्तर

3

ठीक है - ज्यादा हताशा और वेब खोज करने के बाद, मैं ने पाया है कि समस्या पायथन 3.x, encoders.py, समारोह encode_base64 पर लागू होने वाला एक ज्ञात बग है, जो पढ़ना चाहिए इस प्रकार है ...

def encode_base64(msg): 
    """Encode the message's payload in Base64. 

    Also, add an appropriate Content-Transfer-Encoding header. 
    """ 
    orig = msg.get_payload() 
    encdata = _bencode(orig) 

    # new line inserted to ensure all bytes characters are converted to ASCII 
    encdata = str(encdata, "ASCII") 

    msg.set_payload(encdata) 
    msg['Content-Transfer-Encoding'] = 'base64' 

बग मुद्दा # 4768 के रूप में उठाया गया है, और 2010-05-10 पर महत्वपूर्ण स्थिति के पास भेजा गया था। उम्मीद है कि यह अगले संस्करण (3.1.3?)

सादर में तय हो जाएगा एलन

+0

क्या कोई कामकाज है जिसका मैं उपयोग कर सकता हूं? –

3
for file in self.attachments: 
    fp = open(file,"rb")  
    part = MIMEApplication(fp.read())  
    fp.close()  
    encoders.encode_base64(part) 

    # the miracle 
    part.set_payload(part.get_payload().decode('ASCII')) 

    part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)  
    msg.attach(part) 
+0

यह आशाजनक लग रहा है - मैं इसे आज़मा दूंगा - धन्यवाद। –

+0

"ईमेल आयात एन्कोडर्स से" – mastier

2

this SO answer

from base64 import encodebytes 
for file in self.attachments: 
    fp = open(file, 'rb') 
    part = MIMEBase('application', "octet-stream") 
    part.set_payload(encodebytes(fp.read()).decode()) 
    fp.close() 
    part.add_header('Content-Transfer-Encoding', 'base64') 
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % file) 
    msg.attach(part) # msg is an instance of MIMEMultipart() 

server = smtplib.SMTP(host, port) 
server.login(username, password) 
server.sendmail(from_addr, all_recipients, msg.as_string()) 
+0

@bstpierre जोड़ें, इस समाधान के लिए धन्यवाद। पाइथन के भविष्य के संस्करण में मेरा वर्तमान समाधान गिरने के मामले में मैं इसे दूर रखूंगा। –

संबंधित मुद्दे