[英]How to define a directory instead of a single file to send multiple pictures as part of MIME message in Python
I'm following these examples -> https://docs.python.org/3.4/library/email-examples.html to send a MIME message, which can attach pictures.我正在关注这些示例 -> https://docs.python.org/3.4/library/email-examples.html发送可以附加图片的 MIME 消息。
This example code is giving me trouble:这个示例代码给我带来了麻烦:
# 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.
with open(file, 'rb') as fp:
img = MIMEImage(fp.read())
msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
Specifically this:具体是这样的:
for file in pngfiles:
# Open the files in binary mode. Let the MIMEImage class automatically
# guess the specific image type.
with open(file, 'rb') as fp:
img = MIMEImage(fp.read())
msg.attach(img)
pngfiles is not defined and I don't know how to define it. pngfiles 未定义,我不知道如何定义它。 I tried:
我试过:
pngfiles = "C:\\Users\\Public\\Documents\\FamilyPics"
But whenever I run the code it returns:但是每当我运行代码时,它都会返回:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Public\\Documents\\FamilyPics'
I understand that this error appears because I'm giving it a directory instead of a specific file.我知道出现这个错误是因为我给了它一个目录而不是一个特定的文件。 I can send a single picture if I point directly to the picture and its name with the use of:
如果我使用以下命令直接指向图片及其名称,我可以发送一张图片:
path = 'C:\\Users\\Public\\Documents\\FamilyPics\\picture.png'
f = open(path, 'rb')
img = MIMEImage(f.read())
msg.attach(img)
But I really can't figure out, how to point to the directory that contains multiple pictures instead of single specific file.但是我真的想不通,如何指向包含多张图片而不是单个特定文件的目录。 Please help.
请帮忙。 Here is my full code that works for sending a single file:
这是我用于发送单个文件的完整代码:
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = 'my_email'
me_pass = 'my_pass'
family = 'receiver_email'
COMMASPACE = ', '
text = """\
Hi,
How are you?
:)
"""
part1 = MIMEText(text, "plain")
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preable = 'Our family reunion'
path = 'C:\\Users\\Public\\Documents\\FamilyPics\\picture.png'
f = open(path, 'rb')
img = MIMEImage(f.read())
msg.attach(part1)
msg.attach(img)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(me, me_pass)
s.send_message(msg, me, family)
s.quit()
The pngfiles
is the list of png files. pngfiles
是 png 文件的列表。
So you may need this:所以你可能需要这个:
def find_by_postfix(postfix, folder):
for root, _, files in os.walk(folder):
for file in files:
if file.endswith(postfix):
yield os.path.join(root, file)
pngfiles = find_by_postfix('.png', path)
Then you know what png files are:)然后你就知道什么是 png 文件了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.