简体   繁体   English

Python - 如何在不立即将整个文件加载到内存中的情况下发送带有附件的电子邮件?

[英]Python - How to send an email with attachment without loading the whole file into memory at once?

I would like to send emails with attachments of 10MB or more in a VPS with low RAM;我想在低 RAM 的 VPS 中发送带有 10MB 或更多附件的电子邮件; the usual way to send an email with attachments in Python 3 (that I have found) is this:在 Python 3(我发现)中发送带有附件的电子邮件的常用方法是:

from email.message import EmailMessage
# import other needed stuff here omitted for simplicity
attachment = 'some_file.tar'
msg = EmailMessage()
# set from, to, subject here
# set maintype, subtype here
with open(attachment, 'rb') as fd:
    msg.add_attachment(fd.read(),  # this is the problem, the whole file is loaded
                       maintype=maintype,
                       subtype=subtype,
                       filename=attachment)
# smtp_serv is an instance of smtplib.SMTP
smtp_serv.send_message(msg)

With this approach the whole file is loaded into memory and then the EmailMessage object is sent with smtplib.SMTP.send_message, what I am expecting is a way to give to add_attachment a file descriptor(or an iterable), instead of the file content, that is read in a lazy approach(ex. line by line or by some fixed amount of bytes) while the attachment is sent to the server, something like:使用这种方法将整个文件加载到内存中,然后使用 smtplib.SMTP.send_message 发送 EmailMessage 对象,我期望的是一种为 add_attachment 提供文件描述符(或可迭代)而不是文件内容的方法,在将附件发送到服务器时,以惰性方法(例如,逐行或按某些固定数量的字节)读取,例如:

with open('somefile') as fd:
    msg.add_attachment(fd, maintype=mt, subtype=st, filename=fn)
    smtp_serv.send_message(msg)

Is there a way to do this(sending an attachment without loading the whole file at once) with the standard library (email and smtplib)????有没有办法用标准库(电子邮件和 smtplib)来做到这一点(发送附件而不立即加载整个文件)???? I can't find any clue in the python documentation.我在 python 文档中找不到任何线索。

Thanks in advance.提前致谢。

My recommendation is to upload the attachments to an S3 bucket or Google Storage bucket, and then provide a URL in the email for the recipient to download it.我的建议是将附件上传到 S3 存储桶或 Google Storage 存储桶,然后在电子邮件中提供一个 URL 供收件人下载。 Most mail servers will restrict the size of the attachments, so they are very unlikely to get through to most mail clients.大多数邮件服务器会限制附件的大小,因此它们不太可能通过大多数邮件客户端。

You don't have to use a public bucket, you can obfuscate the attachment names, and add a "presigned" url - that only works for a finite amount of time: https://docs.aws.amazon.com/code-samples/latest/catalog/python-s3-generate_presigned_url.py.html您不必使用公共存储桶,您可以混淆附件名称,并添加一个“预签名”网址 - 仅在有限的时间内有效: https ://docs.aws.amazon.com/code- 样本/最新/目录/python-s3-generate_presigned_url.py.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python转置/枢转csv文件,而无需将整个文件加载到内存中? - How do I transpose/pivot a csv file with python *without* loading the whole file into memory? 如何在Python中发送带有Excel文件(xlsx)附件的电子邮件 - how to send email with Excel file (xlsx)attachment in Python 为python中的每个txt文件发送附件电子邮件 - send attachment email for each txt file in python Python发送电子邮件,但我的附件文件不起作用 - Python send email but my attachment file is not working Python无法找到要作为电子邮件附件发送的文件 - Python cannot find file to send as email attachment Python Sendgrid 发送带有 PDF 附件文件的电子邮件 - Python Sendgrid send email with PDF attachment file 如何将 python 中的 stream 数据一次全部加载到 memory 中? - How to stream data in python without loading it all into memory at once? Python发送带有附件的电子邮件 - Python send email with an attachment python + smtp如何发送带有附件的电子邮件? - python+smtp how to send email with attachment? 如何在 python 中打开一个 csv 文件,一次读取一行,而不将整个 csv 文件加载到内存中? - How can I open a csv file in python, and read one line at a time, without loading the whole csv file in memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM