如何使用Python电子邮件模块在电子邮件中发送文件?

如何使用Python电子邮件模块在电子邮件中发送文件?,python,file,email,Python,File,Email,我听说,你可以使用电子邮件模块发送每封电子邮件的文件。我该怎么做呢?看看: 来自站点的示例 # Import smtplib for the actual sending function import smtplib # Here are the email package modules we'll need from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart COMM

我听说,你可以使用电子邮件模块发送每封电子邮件的文件。我该怎么做呢?

看看:

来自站点的示例

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

COMMASPACE = ', '

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class 
    automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()
看看:

来自站点的示例

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

COMMASPACE = ', '

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class 
    automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()

鉴于您已经创建了多部分:

msg = MIMEMultipart()
然后,您可以通过执行以下操作添加图像:

filename = "..."
with open(filename, "rb") as f:
    attachment = MIMEImage(f.read())
filename = "test.txt"
attachment = MIMEText("Hello World")
attachment.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(attachment)
或者您可以通过执行以下操作将字符串
“Hello World”
添加为
test.txt

filename = "..."
with open(filename, "rb") as f:
    attachment = MIMEImage(f.read())
filename = "test.txt"
attachment = MIMEText("Hello World")
attachment.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(attachment)
对于二进制文件或一般情况,您可以执行以下操作:

filename = "..."
ctype, encoding = mimetypes.guess_type(filename)
maintype, subtype = ctype.split("/", 1)
attachment = MIMEBase(maintype, subtype)
with open(filename, "rb") as f:
    attachment.set_payload(f.read())
encoders.encode_base64(attachment)
请记住电子邮件导入编码器中的

最后,通过执行以下操作将其添加到电子邮件中:

filename = "..."
with open(filename, "rb") as f:
    attachment = MIMEImage(f.read())
filename = "test.txt"
attachment = MIMEText("Hello World")
attachment.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(attachment)
需要记住的一件重要事情是,必须首先添加所有文件。信息必须排在最后才能符合,这说明:

接收用户代理应选择并显示其能够显示的最后一种格式


鉴于您已经创建了多部分:

msg = MIMEMultipart()
然后,您可以通过执行以下操作添加图像:

filename = "..."
with open(filename, "rb") as f:
    attachment = MIMEImage(f.read())
filename = "test.txt"
attachment = MIMEText("Hello World")
attachment.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(attachment)
或者您可以通过执行以下操作将字符串
“Hello World”
添加为
test.txt

filename = "..."
with open(filename, "rb") as f:
    attachment = MIMEImage(f.read())
filename = "test.txt"
attachment = MIMEText("Hello World")
attachment.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(attachment)
对于二进制文件或一般情况,您可以执行以下操作:

filename = "..."
ctype, encoding = mimetypes.guess_type(filename)
maintype, subtype = ctype.split("/", 1)
attachment = MIMEBase(maintype, subtype)
with open(filename, "rb") as f:
    attachment.set_payload(f.read())
encoders.encode_base64(attachment)
请记住电子邮件导入编码器中的

最后,通过执行以下操作将其添加到电子邮件中:

filename = "..."
with open(filename, "rb") as f:
    attachment = MIMEImage(f.read())
filename = "test.txt"
attachment = MIMEText("Hello World")
attachment.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(attachment)
需要记住的一件重要事情是,必须首先添加所有文件。信息必须排在最后才能符合,这说明:

接收用户代理应选择并显示其能够显示的最后一种格式


我可以用这个打开图像或其他文件吗?我可以用这个打开图像或其他文件吗?
MIMEImage
有一些与图像相关的特定内容,对比一下
MIMEBase
MIMEImage
基本上是在幕后进行的,简单地说。
MIMEImage
有一些与图像相关的特定内容,对比一下
MIMEBase
<简单地说,代码>MIMEImage基本上是在幕后完成的。