2013-01-03 7 views
15

Ho scritto uno script che scrive un messaggio in un file di testo e lo invia anche come messaggio di posta elettronica. Tutto va bene, tranne che l'e-mail sembra essere tutto in una riga.Come ottenere interruzioni di riga nelle e-mail inviate usando smtplib di Python?

Aggiungo interruzioni di riga da \n e funziona per il file di testo ma non per l'e-mail. Sai quale potrebbe essere la possibile ragione?


Ecco il mio codice:

import smtplib, sys 
import traceback 
def send_error(sender, recipient, headers, body): 

    SMTP_SERVER = 'smtp.gmail.com' 
    SMTP_PORT = 587 
    session = smtplib.SMTP('smtp.gmail.com', 587) 
    session.ehlo() 
    session.starttls() 
    session.ehlo 
    session.login(sender, 'my password') 
    send_it = session.sendmail(sender, recipient, headers + "\r\n\r\n" + body) 
    session.quit() 
    return send_it 


SMTP_SERVER = 'smtp.gmail.com' 
SMTP_PORT = 587 
sender = '[email protected]' 
recipient = '[email protected]' 
subject = 'report' 
body = "Dear Student, \n Please send your report\n Thank you for your attention" 
open('student.txt', 'w').write(body) 

headers = ["From: " + sender, 
       "Subject: " + subject, 
       "To: " + recipient, 
       "MIME-Version: 1.0", 
       "Content-Type: text/html"] 
headers = "\r\n".join(headers) 
send_error(sender, recipient, headers, body) 

risposta

12

avete il vostro corpo del messaggio dichiarato di avere contenuto HTML ("Content-Type: text/html"). Il codice HTML per l'interruzione di riga è <br>. È necessario modificare il tipo di contenuto su text/plain o utilizzare il codice HTML per le interruzioni di riga anziché il semplice \n poiché quest'ultimo viene ignorato durante il rendering di un documento HTML.


Come nota a margine, dare un'occhiata allo email package. Esistono alcune classi che possono semplificare la definizione dei messaggi di posta elettronica per l'utente (with examples).

Per esempio si potrebbe provare (non testata):

import smtplib 
from email.mime.text import MIMEText 

# define content 
recipients = ["[email protected]"] 
sender = "[email protected]" 
subject = "report reminder" 
body = """ 
Dear Student, 
Please send your report 
Thank you for your attention 
""" 

# make up message 
msg = MIMEText(body) 
msg['Subject'] = subject 
msg['From'] = sender 
msg['To'] = ", ".join(recipients) 

# sending 
session = smtplib.SMTP('smtp.gmail.com', 587) 
session.starttls() 
session.login(sender, 'my password') 
send_it = session.sendmail(sender, recipients, msg.as_string()) 
session.quit() 
21

Purtroppo per tutti noi, non ogni tipo di programma o applicazione utilizza la stessa standardizzazione che python fa.

Guardando alla tua domanda ho notato l'intestazione è: "Content-Type: text/html"

Il che significa che è necessario utilizzare i tag HTML di stile per le nuove linee, questi sono chiamati interruzioni di linea. <br>

Il testo dovrebbe essere:

"Dear Student, <br> Please send your report<br> Thank you for your attention" 

Se si preferisce utilizzare tipo di carattere nuove linee, è necessario modificare l'intestazione di leggere: "Content-Type: text/plain"

si avrebbe ancora di cambiare il new- carattere di linea da un singolo \n al doppio \r\n utilizzato nell'e-mail.

Il testo sarebbe:

"Dear Student, \r\n Please send your report\r\n Thank you for your attention" 
+0

Grazie mille. '\ r \ n' non funziona per me ma lo' '
'. –

0

Impostazione dell'intestazione Content-Type a Content-Type: text/plain (con \r\n alla fine) mi ha permesso di inviare messaggi di posta elettronica multi-linea di testo normale.

0

Outlook eliminerà i feed di riga dal testo normale che ritiene siano extra. https://support.microsoft.com/en-us/kb/287816

È possibile provare l'aggiornamento sottostante per rendere le linee simili a punti elenco. Questo ha funzionato per me.

body = "Dear Student, \n- Please send your report\n- Thank you for your attention" 
+0

Perché il voto negativo? – edW

Problemi correlati