My Python script takes a list of lists and creates the text of an email body by joining the inner lists' elements with a "," and appends each of these with a newline like so:
for row in rows:
emailBody += ",".join(row) + "\n"
(I know that I can do something similar using the CSV module, but I'm aiming to do without it - at least for now.)
Then later it sends the email by using:
import smtplib
from email.mime.text import MIMEText
smtpServer = serverDNS
msg = MIMEText(emailBody, "plain")
msg['from'] = fromAddress
msg['to'] = ", ".join(recipients)
msg['subject'] = subject
smtp = smtplib.SMTP(smtpServer)
smtp.sendmail(msg['from'], recipients, msg.as_string())
smtp.quit()
This works fine, except that the newline on the first line gets ignored and the first two lines end up on one line. The newline on every other line is just fine.
If I manually add an extra newline to the last item of the first line, then the email has TWO newlines after the first line, so that there is a blank line between the first two lines.
What is going on here? How can I get this to behave as expected?
Note that the first line (the first list) consists of a bunch of hard-coded strings. So it's a very simple list. All the other lines are dynamically generated, but are all simple string values in the end (otherwise the 'join' would complain, right?).
If I simply print emailBody
, it prints fine. It is only when converting it to an email that the first line's newline gets lost.
I've also tried using "\r\n"
instead of just "\n"
. Same result.