Skip to content
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

Open
cool-RR opened this issue Aug 19, 2013 · 5 comments
Open

Allow adding attachment from memory #7

cool-RR opened this issue Aug 19, 2013 · 5 comments
Assignees

Comments

@cool-RR
Copy link

cool-RR commented Aug 19, 2013

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.

@ghost ghost assigned tomekwojcik Aug 20, 2013
@tomekwojcik
Copy link
Owner

Hello,
this is a nice idea. Tell me, by "in-memory files" do you mean StringIO objects or just plain strings?

I'll be working on the feature in the following days.

Thanks for your input.

@cool-RR
Copy link
Author

cool-RR commented Aug 20, 2013

I meant plain strings, but I think it's trivial to have a function that
looks at the argument and checks whether it's a string or a StringIO and
does the right thing.

On Tue, Aug 20, 2013 at 10:34 AM, Tomek Wójcik notifications@github.comwrote:

Hello,
this is a nice idea. Tell me, by "in-memory files" do you mean StringIOobjects or just plain strings?

I'll be working on the feature in the following days.

Thanks for your input.


Reply to this email directly or view it on GitHubhttps://github.com//issues/7#issuecomment-22927373
.

@timtadh
Copy link

timtadh commented Sep 2, 2013

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 add_file can be used which has the old api.

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)

@tomekwojcik
Copy link
Owner

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

@cool-RR
Copy link
Author

cool-RR commented Jul 7, 2014

Has this been implemented yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants