2012-04-24 13 views
12

मैं प्रमाणन के साथ एक सरल smtp प्रेषक लिख रहा हूँ। यहाँ मेरी कोडपायथन 2.7 में smtplib का उपयोग कर ईमेल में एक वर्णमाला कैसे सेट करें?

SMTPserver, sender, destination = 'smtp.googlemail.com', '[email protected]', ['[email protected]'] 
    USERNAME, PASSWORD = "user", "password" 

    # typical values for text_subtype are plain, html, xml 
    text_subtype = 'plain' 


    content=""" 
    Hello, world! 
    """ 

    subject="Message Subject" 

    from smtplib import SMTP_SSL as SMTP  # this invokes the secure SMTP protocol (port 465, uses SSL) 
    # from smtplib import SMTP     # use this for standard SMTP protocol (port 25, no encryption) 
    from email.MIMEText import MIMEText 

    try: 
     msg = MIMEText(content, text_subtype) 
     msg['Subject']=  subject 
     msg['From'] = sender # some SMTP servers will do this automatically, not all 

     conn = SMTP(SMTPserver) 
     conn.set_debuglevel(False) 
     conn.login(USERNAME, PASSWORD) 
     try: 
      conn.sendmail(sender, destination, msg.as_string()) 
     finally: 
      conn.close() 

    except Exception, exc: 
     sys.exit("mail failed; %s" % str(exc)) # give a error message 

यह सही काम करता है, तक मैं गैर- ASCII प्रतीक (रूसी सिरिलिक) भेजने का प्रयास करें। किसी संदेश में इसे सही तरीके से दिखाने के लिए मुझे एक वर्णमाला को कैसे परिभाषित करना चाहिए? अग्रिम में धन्यवाद!

यूपीडी। मैं अपने कोड बदल दिया है:

text_subtype = 'text' 
content="<p>Текст письма</p>" 
msg = MIMEText(content, text_subtype) 
msg['From']=sender # some SMTP servers will do this automatically, not all 
msg['MIME-Version']="1.0" 
msg['Subject']="=?UTF-8?Q?Тема письма?=" 
msg['Content-Type'] = "text/html; charset=utf-8" 
msg['Content-Transfer-Encoding'] = "quoted-printable" 
… 
conn.sendmail(sender, destination, str(msg)) 

तो, पहली बार मैं text_subtype = 'पाठ' spectify, और फिर शीर्षक में मैं एक संदेश [ 'सामग्री-प्रकार'] जगह = "text/html; charset = utf -8 "स्ट्रिंग। क्या यह सही है?

अद्यतन अंत में, मैं अपने संदेश समस्या समाधान कर लिया है आप संदेश = MimeText तरह ख़ाली (content.encode ('utf-8'), 'सादे', 'UTF-8')

उत्तर

19
from email.header import Header 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 

def contains_non_ascii_characters(str): 
    return not all(ord(c) < 128 for c in str) 

def add_header(message, header_name, header_value): 
    if contains_non_ascii_characters(header_value): 
     h = Header(header_value, 'utf-8') 
     message[header_name] = h 
    else: 
     message[header_name] = header_value  
    return message 

............ 
msg = MIMEMultipart('alternative') 
msg = add_header(msg, 'Subject', subject) 

if contains_non_ascii_characters(html): 
    html_text = MIMEText(html.encode('utf-8'), 'html','utf-8') 
else: 
    html_text = MIMEText(html, 'html')  

if(contains_non_ascii_characters(plain)): 
    plain_text = MIMEText(plain.encode('utf-8'),'plain','utf-8') 
else: 
    plain_text = MIMEText(plain,'plain') 

msg.attach(plain_text) 
msg.attach(html_text) 

यह आपको दोनों पाठ और हेडर के लिए अपने उचित इनकोडिंग अपने पाठ गैर- ASCII शामिल चाहे देना चाहिए: UTF-8

msg = MIMEText(content.encode('utf-8'), text_subtype). 

अधिक यहाँ के साथ अपने संदेश-पाठ इनकोड करना चाहिए अक्षर या नहीं। इसका मतलब यह भी है कि आप स्वचालित रूप से बेस 64 एन्कोडिंग का अनावश्यक रूप से उपयोग नहीं करेंगे।

+0

हाय लोर्कन! 'To' और 'From' के बारे में यदि मैं उन्हें अपने संदेश में निर्दिष्ट करूंगा (जैसे संदेश ['से'] = '[email protected]')? मुझे इसे भी एन्कोड करना चाहिए या नहीं? –

1
लिखना चाहिए

आप एसएमटीपी शीर्ष लेख का उपयोग करने के लिए अलग किए गए वर्णसेट भेजने को प्राप्त करने के लिए हो सकता है इस जोड़ने का प्रयास करें -

msg['Content-Type'] = "text/html; charset=us-ascii"

(अपनी जरूरत के अनुसार चारसेट बदलें)

+0

अच्छा, यह मेरे लिए काम करता है - विषय उचित तरीके से दिखाया गया है, हालांकि संदेश टेक्स्ट एन्कोडिंग अभी भी अविश्वसनीय नहीं है। – f1nn

+0

मैंने अपनी पोस्ट अपडेट की है। कृपया, क्या आप एक नया – f1nn

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