2012-04-24 25 views
12

Sto scrivendo un semplice smtp-sender con autenticazione. Ecco il mio codiceCome impostare un set di caratteri nell'e-mail usando smtplib in Python 2.7?

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 

Funziona perfetto, fino cerco di inviare i simboli non ASCII (cirillico russo). Come dovrei definire un set di caratteri in un messaggio per farlo mostrare in modo corretto? Grazie in anticipo!

UPD. Ho cambiato il mio codice:

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)) 

Quindi, prima volta che ho spectify 'testo' text_subtype =, e quindi nell'intestazione ho posto un msg [ 'Content-Type'] = "text/html; charset = utf -8 "stringa. È corretto?

UPDATE Infine, ho risolto il mio problema messaggio Si dovrebbe scrivere smth come msg = MIMEText (content.encode ('utf-8'), 'normale', 'UTF-8')

risposta

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) 

Questo dovrebbe dare la codifica corretta per il testo e le intestazioni indipendentemente dal fatto che il testo contiene non-ASCII personaggi o no. Significa anche che non si utilizzerà automaticamente la codifica base64 inutilmente.

+0

Ciao Lorcan! Che ne dici di "A" e "Da" se li specificherò nel mio messaggio (come come msg ["Da"] = "[email protected]")? Dovrei codificarlo anche io o no? –

1

potrebbe essere necessario utilizzare l'intestazione SMTP per ottenere l'invio di diversi set di caratteri, prova ad aggiungere questo -

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

(Cambia il set di caratteri in base alle proprie necessità)

+0

Bene, questo ha funzionato per me: il soggetto viene mostrato in modo corretto, sebbene la codifica del testo dei messaggi non sia ancora valida. – f1nn

+0

Ho aggiornato il mio post. Per favore, puoi dare un'occhiata a quello nuovo – f1nn

Problemi correlati