I would like to send emails with attachments of 10MB or more in a VPS with low RAM; the usual way to send an email with attachments in Python 3 (that I have found) is this:
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:
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)???? I can't find any clue in the python documentation.
Thanks in advance.