How to send an email with style in Python3?

那年仲夏 提交于 2019-12-02 21:10:54

Edit after clarification

The msg_full result of your example looks like this:

From: Sender Name <sender@server>
To: Receiver Name <receiver@server>
Cc: Receiver2 Name <receiver2@server>
MIME-Version: 1.0
Content-type: text/html
Subject: Any subject
<h2>My title > <font color="green">OK</font></h2>

Your E-Mail format is not conforming to RFC 2822:

  • You must use CRLF ('\r\n') as newline separators, LF (\n') only is illegal
  • Headers and body must be separated by a CRLF (i.e. one empty line). If you do a ''.join([msg_header, msg_body]), this does not insert this line. What you want to be transmitted as the body text is therefore treated as a header.

A correct version of the same email would look like this:

Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
From: Sender Name <sender@server>
To: Receiver Name <receiver@server>
Cc: Receiver2 Name <receiver2@server>
Subject: Any subject

<h2>My title > <font color="green">OK</font></h2>

I strongly encourage you to use Python's built in libraries for building RFC conformant payloads.

import smtplib
from email.mime.text import MIMEText

title = 'My title'
msg_content = '<h2>{title} > <font color="green">OK</font></h2>\n'.format(title=title)
message = MIMEText(msg_content, 'html')

message['From'] = 'Sender Name <sender@server>'
message['To'] = 'Receiver Name <receiver@server>'
message['Cc'] = 'Receiver2 Name <receiver2@server>'
message['Subject'] = 'Any subject'

msg_full = message.as_string()

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login('sender@server.com', 'senderpassword')
server.sendmail('sender@server.com',
                ['receiver@server.com', 'receiver@server.com'],
                msg_full)
server.quit()

In addition, it is good form to add a text/plain version of your message as well, so that any receipient can read it anywhere (I have HTML mail disabled and don't see any of that on my client). You can do that easily with email.mime.text:

from email.mime.multipart import MIMEMultipart

message = MIMEMultipart('alternative')
message['From'] = 'Sender Name <sender@server>'
message['To'] = 'Receiver Name <receiver@server>'
message['Cc'] = 'Receiver2 Name <receiver2@server>'
message['Subject'] = 'Any subject'

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
message.attach(part1)
message.attach(part2)

Previous Answer

Your question lacks the code you are using to send the mail. I am strongly suspecting you pass msg_content directly as the message to SMTP.sendmail.

SMTP.sendmail, however transmits this string as-is, i.e. as the payload of the mail according to RFC 5321. This payload-data consists of email headers and content, with headers at the top of the message (see RFC 2822).

Your message "My title: My title:" and not shown at the receiving end. If you remove the colon after {title}:, then the receiver obviously does not treat the result as a header, etc.

For HTML styled mail, look at the examples at https://docs.python.org/2/library/email-examples.html - basically you must create a proper text/html MIME encoded message in order to send your message.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!