Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:通过Python发送邮件会创建未知附件_Python_Python 3.x_Smtplib - Fatal编程技术网

Python:通过Python发送邮件会创建未知附件

Python:通过Python发送邮件会创建未知附件,python,python-3.x,smtplib,Python,Python 3.x,Smtplib,我正在尝试通过python发送邮件+附件(.pdf文件)。 邮件已发送,但附件变为未知附件,而不是.pdf文件 我的代码如下所示: import smtplib import os import ssl import email from email import encoders from email.mime.text import MIMEText from email.mime.base import MIMEBase from email.mime.multipart import

我正在尝试通过python发送邮件+附件(.pdf文件)。 邮件已发送,但附件变为未知附件,而不是.pdf文件

我的代码如下所示:

import smtplib
import os
import ssl
import email

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

port = 465
smtp_server = "smtp.gmail.com"
subject = "An example of txt.file"
sender = "..."
receiver = "..."
password = "..."

message = MIMEMultipart()
message["From"] = sender
message["To"] = receiver
message["Subject"] = subject

filename = '318.pdf'

attachment = open(filename, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
part.add_header('Content Disposition', 'attachment', filename=filename)
encoders.encode_base64(part)
message.attach(part)

message.attach(part)

body = "This is an example  of how to send an email with an .pdf-attachment."
message.attach(MIMEText(body, 'plain'))

text = message.as_string()

context = ssl.create_default_context()

with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
    server.login(sender, password)
    server.sendmail(sender, receiver, text)

    print('Sent')
它有什么问题,或者我有什么不同的做法

我尝试了不同的文件类型,.pdf文件位于python文件目录中,…

您编写的

part.add_header('Content Disposition', 'attachment', filename=filename)
但是“filename”参数名称必须作为字符串提供,并且“Content Disposition”中也缺少连字符。试一试

part.add_header('Content-Disposition', 'attachment; filename=' + filename)
这应该能解决你的问题。只需指出,您在第20行和第22行附加了两个“part”


我想你可能已经在看这篇文章了。但是如果没有,我想你会发现它很有用。

这样做了,仍然使用MIMEApplication而不是MimeBase发送一个未知的附件我也尝试过了。。。meAlso Im使用MIMEMultipart('mixed')也有同样的问题,这是我下面的文章,你是对的。如果我将代码更改为:part.add_header('Content-Disposition','attachment','filename'=filename),我得到的只是一个语法错误:表达式不能包含赋值,我想你的意思是。
add_header('Content-Disposition','attachment;filename='+filename)
是的,它是“Content-Disposition”,而不是“Content-Disposition”。你忘了连字符。另外,在您接下来的文章中,他们使用了一个f字符串来连接第二个参数,您没有这样做。因此,您可以像@geckos所展示的那样实现这一点,或者如果您的python版本支持f-string,您也可以像本文中那样使用它。我很抱歉没有早点发现,我已经编辑了我的答案。我希望它能把事情弄清楚。