yagmail icon indicating copy to clipboard operation
yagmail copied to clipboard

[Feature Request] Attach Image from io.BytesIO() file-like object

Open MLpranav opened this issue 5 years ago • 4 comments

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

MLpranav avatar Jul 24 '20 16:07 MLpranav

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).

kootenpv avatar Sep 10 '20 17:09 kootenpv

The mimetype can be specified as an argument.

MLpranav avatar Sep 15 '20 20:09 MLpranav

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.

kootenpv avatar Oct 07 '20 23:10 kootenpv

+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!

ToeTag avatar Mar 31 '21 19:03 ToeTag