首页 > 解决方案 > Python 中的 HTML 消息

问题描述

我对 Python 还很陌生,试图帮助自动化工作中的一些事情。我正在尝试使用它在 Excel 电子表格中向过去三个月内与我们一起购买的客户列表发送消息。我有以下内容,但它目前以纯文本形式发送 html 消息。我之前已经设法让它正确发送 html 消息,但是无法将使用 pandas 从 excel 中提取数据与发送 html 消息结合起来。

任何帮助将不胜感激。

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

your_name = "NAME"
your_email = "EMAIL"
your_password = "PASSWORD"


server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls()
server.ehlo()
server.login(your_email, your_password)


email_list = pd.read_excel("python_test.xlsx")


all_names = email_list['Name']
all_emails = email_list['Email']

for idx in range(len(all_emails)):

    name = all_names[idx]
    email = all_emails[idx]
    subject = ‘Subject’


    message = MIMEMultipart('mixed')


    BODY = html = """\
    <html>
     Html message goes here 
    </html>
    """

    HTML_BODY = MIMEText(BODY, 'html')
    message.attach(HTML_BODY)

    full_email = ("From: {0} <{1}>\n"
                  "To: {2} <{3}>\n"
                  "Subject: {4}\n\n"
                  "{5}"
                  .format(your_name, your_email, name, email, subject, message,))

    try:
        server.sendmail(your_email, [email], full_email)
        print('Email to {} successfully sent!\n\n'.format(email))
    except Exception as e:
        print('Email to {} could not be sent :( because {}\n\n'.format(email, str(e)))

server.close()

标签: pythonpandas

解决方案


# import the necessary components first
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
port = 2525 
smtp_server = "smtp.mailtrap.io"
login = "1a2b3c4d5e6f7g" # paste your login generated by Mailtrap
password = "1a2b3c4d5e6f7g" # paste your password generated by Mailtrap
sender_email = "mailtrap@example.com"
receiver_email = "new@example.com"
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email
# write the plain text part
text = """\
Hi,
Check out the new post on the Mailtrap blog:
SMTP Server for Testing: Cloud-based or Local?
https://blog.mailtrap.io/2018/09/27/cloud-or-local-smtp-server/
Feel free to let us know what content would be useful for you!"""
# write the HTML part
html = """\
<html>
  <body>
    <p>Hi,<br>
       Check out the new post on the Mailtrap blog:</p>
    <p><a href="https://blog.mailtrap.io/2018/09/27/cloud-or-local-smtp-server">SMTP Server for Testing: Cloud-based or Local?</a></p>
    <p> Feel free to <strong>let us</strong> know what content would be useful for you!</p>
  </body>
</html>
"""
# convert both parts to MIMEText objects and add them to the MIMEMultipart message
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
# send your email
with smtplib.SMTP("smtp.mailtrap.io", 2525) as server:
    server.login(login, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )
print('Sent') 

示例在这里 https://blog.mailtrap.io/sending-emails-in-python-tutorial-with-code-examples/


推荐阅读