yagmail icon indicating copy to clipboard operation
yagmail copied to clipboard

Can sending attachments be a file stream? Don't want absolute paths, reduce file generation and deletion

Open wuqiang-001 opened this issue 2 years ago • 2 comments

发送附件可以是文件流吗? 不想要绝对路径,减少文件的生成与删除

wuqiang-001 avatar Apr 15 '22 05:04 wuqiang-001

It actually already works!

import yagmail
yag = yagmail.SMTP(username, password)
with open("data.parquet.snappy", "rb") as f: 
    yag.send(attachments=f)

or even:

import yagmail
with yagmail.SMTP(user, pw) as yag, open("data.parquet.snappy", "rb") as f:
    yag.send(attachments=f)

kootenpv avatar Apr 15 '22 08:04 kootenpv

It actually already works!

import yagmail
yag = yagmail.SMTP(username, password)
with open("data.parquet.snappy", "rb") as f: 
    yag.send(attachments=f)

or even:

import yagmail
with yagmail.SMTP(user, pw) as yag, open("data.parquet.snappy", "rb") as f:
    yag.send(attachments=f)

open method return a io.BufferedReader object, but how can i send attachments like a io.BytesIO object?

leonlinxs avatar Sep 16 '22 17:09 leonlinxs

use StringIO as an example:

from io import StringIO
import yagmail

buffer = StringIO("some string")
buffer.name = 'file.txt'

yag = yagmail.SMTP(username, password)
yag.send(attachments=buffer)

flatflax avatar Mar 17 '23 16:03 flatflax

Thanks @flatflax! Closing as I think it has been answered

kootenpv avatar Mar 19 '23 09:03 kootenpv