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

时间:2016-05-13 08:26:48

标签: python python-2.7

我有以下代码在Python的电子邮件中发送html文件SeleniumTestReport_part1.html。 我想在电子邮件中发送多个文件。我怎么能这样做?

我要发送的文件是: SeleniumTestReport_part1.html SeleniumTestReport_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()

谢谢, 里亚兹

2 个答案:

答案 0 :(得分:5)

我已实现此功能,可以从gmail发送邮件。

import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders

def send_mail_gmail(username,password,toaddrs_list,msg_text,fromaddr=None,subject="Test mail",attachment_path_list=None):

    s = smtplib.SMTP('smtp.gmail.com:587')
    s.starttls()
    s.login(username, password)
    #s.set_debuglevel(1)
    msg = MIMEMultipart()
    sender = fromaddr
    recipients = toaddrs_list
    msg['Subject'] = subject
    if fromaddr is not None:
        msg['From'] = sender
    msg['To'] = ", ".join(recipients)
    if attachment_path_list is not None:
        for each_file_path in attachment_path_list:
            try:
                file_name=each_file_path.split("/")[-1]
                part = MIMEBase('application', "octet-stream")
                part.set_payload(open(each_file_path, "rb").read())

                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment' ,filename=file_name)
                msg.attach(part)
            except:
                print "could not attache file"
    msg.attach(MIMEText(msg_text,'html'))
    s.sendmail(sender, recipients, msg.as_string())

您可以将多个地址作为要发送邮件的toaddrs_list元素和多个附件文件名及attachment_path_list中的路径传递。

答案 1 :(得分:2)

如果要将文件附加到电子邮件中,可以使用迭代文件并将其附加到邮件中。您还可能需要向正文添加一些文本。 这是代码:

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