简体   繁体   English

如何定义目录而不是单个文件以在 Python 中将多张图片作为 MIME 消息的一部分发送

[英]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.

相关问题 Python:带有图片的 Mime 消息 - Python: Mime message with Pictures 从目录中选择一个随机文件并发送(Python、MIME) - Select a random file from a directory and send it (Python, MIME) 如何解码消息的mime部分并在Python 2.7中获取** unicode **字符串? - How to decode a mime part of a message and get a **unicode** string in Python 2.7? 如何将您的长 WhatsApp 消息作为一条消息而不是多条消息发送 - how to send your long WhatsApp message as a single message instead of multiple messages 确定MIME电子邮件部分是文件还是消息文本 - Determining if a MIME email part is a file or message text 如何使用Python将多张图片对角合并为一张图片 - How to merge multiple pictures diagonally into a single one using Python python MIME将多个附件附加到多部分邮件 - python MIME attaching multiple attachments to a multipart message 使用Python读取存储在文本文件中的MIME消息 - Read MIME Message stored in text file with Python FileNotFoundError:[Errno 2] Python 3.7 上的图片没有此类文件或目录 - FileNotFoundError: [Errno 2] No such file or directory for pictures on Python 3.7 如何向服务器发送多条消息,并让该服务器接收多条消息而不是一条大消息 [Python,套接字] - How can I send multiple messages to a server, and have that server receive multiple messages instead of one big message [Python, Sockets]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM