简体   繁体   中英

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:

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.

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

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 ).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM