yagmail
yagmail copied to clipboard
[Feature Request] Attach Image from io.BytesIO() file-like object
It'll be great to have a feature to enable attaching files from not just the path, but also from file-byte objects.
My program uses PIL to generate images and then emails them using yagmail, it would be great if I can email them without saving them to the disk (by directly attaching the image object).
Example:
from io import BytesIO
image=Image.new("RGBA",(300,400),(255,255,255))
image_base=ImageDraw.Draw(image)
pic=image_base.text((150,200),pass,(0,0,0))
stream = BytesIO()
image.save(stream, format="png")
stream.seek(0)
imgObj=stream.read()
References: Stackoverflow
Thanks for the feature request.
The problem with a BytesIO object is that we do not know the mimetype of it, so you'd have to specify that. Which would require an extra object and not something we can just determine from the BytesIO object itself.
Let's wait and see if others see this as useful too.
At least I am now adding support for detecting pandas dataframes (to html) and dictionary (to json).
The mimetype can be specified as an argument.
I think perhaps we could do something with base64 conversion. Dont' have currently the time to look into it, but if there are smtplib example with base64, then I am sure it would be easy to integrate.
+1 on this one. Very handy for saving lifetime on SD-cards when developing camera applications on RPI for instance. Quite common usecase is to take a photo and send it via mail. Yagmail is perfect for this!