Python HTML电子邮件:如何在HTML电子邮件中插入列表项

Python HTML电子邮件:如何在HTML电子邮件中插入列表项,python,smtplib,Python,Smtplib,我努力想找到解决这个问题的办法,但最后我不得不问你们。我有HTML电子邮件(使用Python的smtplib)。这是密码 Message = """From: abc@abc.com> To: abc@abc.com> MIME-Version: 1.0 Content-type: text/html Subject: test Hello, Following is the message """ + '\n'.join(mail_body) + """ Thank you.

我努力想找到解决这个问题的办法,但最后我不得不问你们。我有HTML电子邮件(使用Python的smtplib)。这是密码

Message = """From: abc@abc.com>
To: abc@abc.com>
MIME-Version: 1.0
Content-type: text/html
Subject: test

Hello,
Following is the message 
""" + '\n'.join(mail_body)  + """
Thank you.
"""
在上面的代码中,mail_body是一个列表,其中包含进程的输出行。现在我想要的是,在HTML电子邮件中显示这些行(逐行)。现在发生的事情只是一行接一行的附加。i、 e

我是这样存储(进程的)输出的:

for line in cmd.stdout.readline()
    mail_body.append()
HTML电子邮件中的当前输出为:

Hello,
abc
Thank you.
我想要的是:

Hello,
a
b
c
Thank you.
我只想在HTML电子邮件中逐行附加我的流程输出。我的产出能以任何方式实现吗


感谢和问候

在HTML中,新行不是
\n
它是

表示“换行符”,但由于您在此电子邮件中也没有使用HTML标记,您还需要知道,在MIME消息中,新行是
\r\n
而不仅仅是
\n

所以你应该写:

'\r\n'.join(mail_body)
对于处理MIME消息的换行符,但如果要使用HTML进行格式化,则需要知道

是换行符,它将是:

'<br>'.join(mail_body)

”。加入(邮件正文)
要全面,您可以尝试:

'\r\n<br>'.join(mail_body) 
'\r\n
'.加入(邮件正文)

但我现在知道这会是什么样子…

您可以使用以下方式生成要发送的电子邮件内容:

#/usr/bin/env python
#-*-编码:utf-8-*-
从cgi导入转义
从email.header导入头
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
从smtplib导入SMTP\U SSL
登录,密码=me@example.com“,“我的密码”
#创建消息
msg=MIMEMultipart('alternative')
msg['Subject']=标题('Subject…','utf-8')
msg['From']=登录
msg['To']=','.加入([登录,])
#创建消息正文(纯文本和HTML版本)。
text=“您好,\n下面是感谢您的消息\n%(somelist)。%dict(
somelist='\n'.join([“-”+邮件正文中的项目])
html=“”您好,以下是消息
谢谢你(
somelist='\n'.join([“
  • ”+转义(邮件正文中的项目))) #根据RFC 2046,在本例中,多部分消息的最后一部分 #HTML消息是最好的首选。 msg.attach(MIMEText(文本'plain','utf-8')) 附加(MIMEText(html,'html','utf-8')) #发送它 s=SMTP_SSL('SMTP.mail.example.com',timeout=10)#Python2上没有证书检查 s、 设置调试级别(0) 尝试: s、 登录(登录,密码) s、 sendmail(msg['From'],msg['To'],msg.as_string()) 最后: s、 退出
  • 我在想,这部作品是否真的是在一些段落标签之后太棒了,你太棒了,愿上帝保佑你有可爱的孩子和很多钱。”“为我工作,我不想要别的。我不知道为什么我没有想到这件简单的事情。非常感谢。您不需要
    “\r\n”
    smtplib
    为您翻译
    \n
    (或
    \r
    )。否则您将需要担心
    \r\n。\r\n
    来自
    ,7位清洁度等问题。
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from cgi import escape
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from smtplib import SMTP_SSL
    
    login, password = 'me@example.com', 'my password'
    
    # create message
    msg = MIMEMultipart('alternative')
    msg['Subject'] = Header('subject…', 'utf-8')
    msg['From'] = login
    msg['To'] = ', '.join([login,])
    
    # Create the body of the message (a plain-text and an HTML version).
    text = "Hello,\nFollowing is the message\n%(somelist)s\n\nThank you." % dict(
        somelist='\n'.join(["- " + item for item in mail_body]))
    
    html = """<!DOCTYPE html><title></title><p>Hello,<p>Following is the message 
    <ul>%(somelist)s</ul><p>Thank you. """ % dict(
        somelist='\n'.join(["<li> " + escape(item) for item in mail_body]))
    
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(MIMEText(text, 'plain', 'utf-8'))
    msg.attach(MIMEText(html, 'html', 'utf-8'))    
    
    # send it
    s = SMTP_SSL('smtp.mail.example.com', timeout=10) # no cert check on Python 2
    
    s.set_debuglevel(0)
    try:
        s.login(login, password)
        s.sendmail(msg['From'], msg['To'], msg.as_string())
    finally:
        s.quit()