kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.64k stars 263 forks source link

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

Closed wuqiang-001 closed 1 year ago

wuqiang-001 commented 2 years ago

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

kootenpv commented 2 years ago

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)
leonlinxs commented 1 year ago

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?

flatflax commented 1 year ago

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)
kootenpv commented 1 year ago

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