如何通过电子邮件正文中的列表通过电子邮件发送值

时间:2019-12-12 19:35:24

标签: python smtplib

我正在将一些JSON值保存在列表中,并且由于查询,我尝试通过电子邮件将其发送出去。我尝试将JSON值转换为具有\ t和\ n值的大字符串。在打印时可以使用,但在电子邮件中效果不佳。我正在寻找一种方法,可以将具有正确格式的变量打印为html(用于电子邮件),也可以在电子邮件内部的循环中打印每个列表项。我该如何实现?

my_list = ['one', 'two', 'three']
host = "smtp.gmail.com"
port = 587
email_username = 'EMAIL'
email_password = 'PASSWORD'
from_email = email_username
to_list = 'destination@mail.com'
try:
    email_conn = smtplib.SMTP(host,port)
    email_conn.ehlo()
    email_conn.starttls()
    email_conn.login(email_username, email_password)
    the_msg = MIMEMultipart("alternative")
    the_msg['Subject'] = "Test Report"
    the_msg["From"] = from_email

    plain_txt = "This is a test Message"
    html_txt = 'Test number %s' % (i for i in my_list)

    part_1 = MIMEText(plain_txt, 'plain')
    part_2 = MIMEText(html_txt, 'html')
    the_msg.attach(part_1)
    the_msg.attach(part_2)
    email_conn.sendmail(from_email, to_list, the_msg.as_string())
    email_conn.quit()
except smtplib.SMTPException:
    print('Error Sending Message')

2 个答案:

答案 0 :(得分:0)

这里是一个示例,我对您的代码做了一些更改...格式化html正文的方式:

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

my_list = ['one', 'two', 'three']
host = "smtp.gmail.com"
port = 587
email_username = 'mail@gmail.com'
email_password = 'xxxxxx'
from_email = email_username
to_list = 'mail@gmail.com'
str1 = ','.join(my_list)
# Create the plain-text and HTML version of your message

try:
    email_conn = smtplib.SMTP(host,port)
    email_conn.ehlo()
    email_conn.starttls()
    email_conn.login(email_username, email_password)
    the_msg = MIMEMultipart("alternative")
    the_msg['Subject'] = "Test Report"
    the_msg["From"] = from_email

    plain_txt = """\
    Subject: Your Title"""

    html_txt = """\
    <html>
      <body>
        <p>Test number:  {str1}</p>
      </body>
    </html>
    """.format(str1=str1)

    part_1 = MIMEText(plain_txt, 'plain')
    part_2 = MIMEText(html_txt, 'html')
    the_msg.attach(part_1)
    the_msg.attach(part_2)
    email_conn.sendmail(from_email, to_list, the_msg.as_string())
    email_conn.quit()
except smtplib.SMTPException:
    print('Error Sending Message')

答案 1 :(得分:0)

我可以建议使用smtplib.SMPT_SSL,它可以用更少的代码行来完成工作,这些代码行有时更具可读性和灵活性

import smtplib
from email.message import EmailMessage


msg = EmailMessage()

my_list = ['one', 'two', 'three']
host = "smtp.gmail.com"
port = 465
email_username = 'EMAIL'
email_password = 'PASSWORD'
to_list = 'destination@mail.com'

msg['From'] = email_username
msg['Subject'] = 'this is the subject'
msg['To'] = to_list #you can loop through a list of emails to send this mail to everyone on the list

msg.set_content('plain text email')
msg.add_alternative(f"""\
    <!DOCTYPE html>
    <html>
       <body>
          <p>Test number {''.join(my_list)}</p>
       </body>
    </html>
   """, subtype='html')

with smtplib.SMPT_SSL(host, port) as smtp:
     smtp.login(email_username, email_password)
     smtp.send_message(msg)

按照我的经验,用这种方式做事往往会更容易