gpt4 book ai didi

Python 我如何在电子邮件中发送多个文件。我可以发送 1 个文件,但如何发送超过 1 个

转载 作者:行者123 更新时间:2023-11-28 19:48:02 25 4
gpt4 key购买 nike

我有以下代码在 Python 的电子邮件中发送 html 文件 SeleniumTestReport_part1.html。我想在电子邮件中发送 1 个以上的文件。我该怎么做?

我要发送的文件是:SeleniumTestReport_part1.htmlSeleniumTestReport_part2.html
SeleniumTestReport_part3.html

我发送 1 个文件的代码是:

def send_selenium_report():
fileToSend = r"G:\test_runners\selenium_regression_test_5_1_1\TestReport\SeleniumTestReport_part1.html"
with open(fileToSend, "rt") as f:
text = f.read()
msg = MIMEText(text, "html")
msg['Subject'] = "Selenium ClearCore_Regression_Test_Report_Result"
msg['to'] = "4_server_dev@company.com"
msg['From'] = "system@company.com"

s = smtplib.SMTP()
s.connect(host=SMTP_SERVER)
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.close()

谢谢,里亚兹

最佳答案

如果您想将文件附加到电子邮件中,您可以使用遍历文件并将它们附加到邮件中。您可能还想在正文中添加一些文本。这是代码:

import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

def send_selenium_report():
dir_path = "G:/test_runners/selenium_regression_test_5_1_1/TestReport"
files = ["SeleniumTestReport_part1.html", "SeleniumTestReport_part2.html", "SeleniumTestReport_part3.html"]

msg = MIMEMultipart()
msg['To'] = "4_server_dev@company.com"
msg['From'] = "system@company.com"
msg['Subject'] = "Selenium ClearCore_Regression_Test_Report_Result"

body = MIMEText('Test results attached.', 'html', 'utf-8')
msg.attach(body) # add message body (text or html)

for f in files: # add files to the message
file_path = os.path.join(dir_path, f)
attachment = MIMEApplication(open(file_path, "rb").read(), _subtype="txt")
attachment.add_header('Content-Disposition','attachment', filename=f)
msg.attach(attachment)

s = smtplib.SMTP()
s.connect(host=SMTP_SERVER)
s.sendmail(msg['From'], msg['To'], msg.as_string())
print 'done!'
s.close()

关于Python 我如何在电子邮件中发送多个文件。我可以发送 1 个文件,但如何发送超过 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com