在三重报价中打印一个列表

时间:2014-11-06 20:00:57

标签: python

我想在电子邮件中打印列表元素。

我在我的python脚本中定义了我的列表,如:

exceptions = []

在脚本末尾,列表将包含以下元素:

This is a boy.
This is a girl.
This is a dog.

我想打印名为'例外'的列表元素。在python脚本中设计的电子邮件中。

fromaddr = 'xxx@xxx.com'
toaddrs  = 'xxx@xxx.com'

msg = """Subject:Big Bro FTP issue.
From: xxx@xxx.com
To:  xxx@xxx.com

!!!PRINT THE EXCEPTION LIST HERE!!!
"""
print "Message length is " + repr(len(msg))


smtp_server = 'xxxxx.com'
smtp_username = 'xxxxxx'
smtp_password = 'xxxxxx'
smtp_port = '587'
smtp_do_tls = True

server = smtplib.SMTP(
host = smtp_server,
port = smtp_port,
timeout = 10
)

server.starttls()
server.ehlo()
server.login(smtp_username, smtp_password)
server.sendmail(fromaddr, toaddrs, msg)
print server.quit()

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

我更喜欢MIMEText

import smtplib
from email.mime.multipart import MIMEText
msg = MIMEText("your_maeeage")
msg['From'] = <sender-email>
msg['To'] = <reciver-email>
msg['Subject'] = "your_subject"
server = smtplib.SMTP(<server>,<port>)
server.starttls()
server.login(Username,password)
server.sendmail(from_addr,to_addr,msg.as_string())

答案 1 :(得分:1)

Format是一个很好的方法,可以让您将占位符放在列表中。当然,在这里你可以简单地附加到列表中,但它很有用。使用join将字符串列表格式化为单个字符串,此处用新行(\n)分隔。

msg = """Subject:Big Bro FTP issue.
From: xxx@xxx.com
To:  xxx@xxx.com

{}
""".format('\n'.join(exceptions))

或:

msg = """Subject:Big Bro FTP issue.
    From: xxx@xxx.com
    To:  xxx@xxx.com
""" + '\n'.join(exceptions)