[英]printing a list inside a triple quote
I would like to print elements of a list inside an email message. 我想在电子邮件中打印列表的元素。
I defined my list in my python script like: 我在python脚本中定义了列表,如下所示:
exceptions = []
At the end of the script the list will have the following elements: 在脚本的最后,列表将包含以下元素:
This is a boy.
This is a girl.
This is a dog.
I would like to print the elements of the list called 'exceptions' inside an email that is contrived in the python script. 我想在python脚本设计的电子邮件中打印名为“ exceptions”的列表的元素。
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()
Any help will be greatly appreciated. 任何帮助将不胜感激。
i would prefer MIMEText
我更喜欢
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())
Format is a nice method to know which allows you to put place holders in a list. 格式是一种很好的了解方法,它允许您将占位符放在列表中。 Of course, here you could simple append to the list, but it's useful.
当然,您可以在此处简单地追加到列表,但这很有用。 Use join to format the list of strings into a single string, here separated by a new line (
\\n
). 使用join将字符串列表格式化为单个字符串,此处用换行符(
\\n
)分隔。
msg = """Subject:Big Bro FTP issue.
From: xxx@xxx.com
To: xxx@xxx.com
{}
""".format('\n'.join(exceptions))
or: 要么:
msg = """Subject:Big Bro FTP issue.
From: xxx@xxx.com
To: xxx@xxx.com
""" + '\n'.join(exceptions)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.