【问题标题】:Python send email but my attachment file is not workingPython 发送电子邮件但我的附件文件不起作用
【发布时间】:2016-08-26 10:59:44
【问题描述】:

我正在尝试从我的 Python 脚本发送一封带有附件的电子邮件。 在正文中发送文本似乎可以,但我不确定文件附件的语法。

我的代码 sn-p 是:

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

def send_email(self):
    fromaddress = "rladhani@gmail.com"
    toaddress = "riaz.ladhani@company.com"
    text = "subject line test"
    username = "rladhani@gmail.com"
    password = "-"
    msg = MIMEMultipart()
    msg['from'] =fromaddress
    msg['to'] = toaddress
    msg['subject'] = text
    msg.attach(MIMEText (text))
    attachment = open(r"C:\Webdriver\ClearCoreRegression Test\TestReport\TestReport_01052016.html", "rb")

    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

    msg.attach(part)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(username, password)
    server.sendmail(fromaddress, toaddress, msg.as_string())
    server.quit()

如何将文件附件添加到电子邮件中? 此外,我在

上收到未解决的参考错误
encoders.encode_base64(part)

谢谢,里亚兹

【问题讨论】:

    标签: python python-2.7 selenium-webdriver pycharm


    【解决方案1】:

    【讨论】:

    • 感谢您提供的链接,非常有帮助。
    猜你喜欢
    • 2018-09-29
    • 2018-02-06
    • 1970-01-01
    • 2015-06-06
    • 2015-07-08
    • 1970-01-01
    • 2015-06-19
    • 2020-11-11
    • 2012-06-15
    相关资源
    最近更新 更多