首页 > 解决方案 > Python 使用带有 html 内容的 csv 发送多封电子邮件?

问题描述

import csv, smtplib

port = 2525
smtp_server = "smtp.mailtrap.io"
login = "1a2b3c4d5e6f7g" # paste your login generated by Mailtrap
password = "1a2b3c4d5e6f7g" # paste your password generated by Mailtrap

message = """Subject: Order confirmation
To: {recipient}
From: {sender}

Hi {name}, thanks for your order! We are processing it now and will contact you soon"""
sender = "new@example.com"

with smtplib.SMTP("smtp.mailtrap.io", 2525) as server:
    server.login(login, password)
    with open("contacts.csv") as file:
        reader = csv.reader(file)
        next(reader)  # it skips the header row
        for name, email in reader:
            server.sendmail(
               sender,
                email,
                message.format(name=name, recipient=email, sender=sender)
            )
            print(f'Sent to {name}')

在消息 Hi{name} 之后,我需要添加 html 代码,这是我的内容。但是当我粘贴时,我的内容中也有 html 标签

标签: pythonhtmlemail

解决方案


推荐阅读