【发布时间】:2021-12-22 08:42:53
【问题描述】:
我正在使用 python 3.7.3 运行 python 脚本。下面是代码。这没有做任何事情。它只是显示消息“已发送电子邮件”,实际上并不发送电子邮件。
此代码以前用于 Python 2.7,只是将“multipart”更改为“Multipart”。我不得不将 M 更改为 m,因为它会引发错误。现在,它不会抛出该错误。
我已检查机器上的 telnet 是否已启动。
telnet localhost 11
Trying 111.1.1.1.......
Connected to localhost.
Escape character is '^]'.
220 abcdef.com ESMTP Exim 4.92 Tue, 09 Nov 2021 11:19:10 -0500
quit
telnet
telnet> quit
请帮忙。提前谢谢你。
#Send email with attachment
sender = 'abc@test.com'
receivers = 'xyz@test.com'
msg = email.mime.multipart.MIMEMultipart()
msg['Subject'] = 'Test script'
msg['From'] = 'abc@test.com'
msg['To'] = 'xyz@test.com'
# The main body is just another attachment
body = email.mime.text.MIMEText("""Please find the updated data in attached csv""")
msg.attach(body)
# CSV attachment
filename='/home/abc/python_scripts/test.csv'
with open(filename, "rb") as fs:
part = MIMEBase('application', "octet-stream")
part.set_payload(fs.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(filename))
msg.attach(part)
try:
server = smtplib.SMTP('localhost',11)
server.sendmail(sender, receivers, msg.as_string())
server.close()
print('Email Sent')
except SMTPException:
print('Something went wrong...')
【问题讨论】:
标签: python python-3.7 smtplib