-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow adding attachment from memory #7
Comments
Hello, I'll be working on the feature in the following days. Thanks for your input. |
I meant plain strings, but I think it's trivial to have a function that On Tue, Aug 20, 2013 at 10:34 AM, Tomek Wójcik notifications@github.comwrote:
|
I think this enhancement is pretty important. So 👍 for it. Below is how I think it should work. I considered having it use a file like object but there is no advantage as set_payload uses a string. Instead a convience method def add_attachment(self, data, name, mimetype=None):
"""Attaches a file from a string, *data*, and gives it the name
specified in name. If *mimetype* is not specified an attempt to guess it is
made. If nothing is guessed then `application/octet-stream` is used."""
if mimetype is None:
mimetype, _ = mimetypes.guess_type(name)
if mimetype is None:
mimetype = 'application/octet-stream'
type_maj, type_min = mimetype.split('/')
part = MIMEBase(type_maj, type_min)
part.set_payload(data)
email_encoders.encode_base64(part)
encoded_name = self._encoded(name)
part.add_header(
'Content-Disposition', 'attachment; filename="%s"' % encoded_name)
self._parts.append((mimetype, part))
def add_file(self, file_path, mimetype=None):
"""Attaches a file located at *file_path* to the envelope. If *mimetype* is
not specified an attempt to guess it is made. If nothing is guessed then
`application/octet-stream` is used."""
with open(file_path, 'rb') as f:
data = f.read()
filename = os.path.basename(file_path)
self.add_attachment(data, filename, mimetype=mimetype) |
@timtadh I have pretty much the same solution in the pipeline. I've been caught between other things lately so I haven't had a chance finish it and merge into master yet. I should be able to do so over the following days. Thanks for your response :). |
Has this been implemented yet? |
Sometimes I have a file in-memory, not saved to disk, and I'd like to add it as an attachment.
Currently
add_attachment
only works on files that are saved to the filesystem.Please allow attaching files that are in-memory.
The text was updated successfully, but these errors were encountered: