#26802: Sending mails with attachment results in 'bytes' object has no attribute
'encode'
-----------------------------+------------------------------------
     Reporter:  Brandl       |                    Owner:  nobody
         Type:  Bug          |                   Status:  new
    Component:  Core (Mail)  |                  Version:  1.9
     Severity:  Normal       |               Resolution:
     Keywords:               |             Triage Stage:  Accepted
    Has patch:  0            |      Needs documentation:  0
  Needs tests:  0            |  Patch needs improvement:  0
Easy pickings:  0            |                    UI/UX:  0
-----------------------------+------------------------------------

Comment (by Brandl):

 That may be naive, but am I really the first one, who encounters a problem
 with saving something in a FileField and then sending it per Mail?

 So this is where the file gets stored:

 {{{
 if isinstance(content, string_types):
     # `content` is a filename - try to open the file
     opened_file = open(content, 'rb')
     content = File(opened_file)

 attachment = Attachment()
 attachment.file.save(filename, content=content, save=True)
 }}}

 This how it gets attached to the mail:

 {{{
 msg.attach(attachment.name, attachment.file.read())
 }}}

 At the moment the Django offers two methods of attaching files:
 {{{
     def attach_file(self, path, mimetype=None):
 }}}
 and
 {{{
     def attach(self, filename=None, content=None, mimetype=None):
 }}}

 The first one has the benefit of some sophisticated Mimetype guessing,
 when reading the file, but does not allow to supply a filename, so when
 the attachment names get hashed, this would destroy the name information.
 The other one does allow this, but seemingly throws an exception, when
 provided with a binary file and no Mimetype.

 So what are my/our options here? Of course me and others, who encounter
 the same problem, would need to replicate the functionality of
 attach_file, but since the implementation is by no means straight forward,
 I would prefer Django would provide a convenience method for this, maybe
 even in the Django FileField.

 Since the Django FileField also has a path attribute, I could also utilize
 the attach_file(), method, but then I would love to have a way of
 overriding the file name.

 Or maybe I forgot something more simple than that? Also I wonder, why my
 quick fix:
 {{{
 MIMEText.__init__(self, _text, _subtype, 'utf-8')
 }}}
 is solving this more complicated problem and in which cases that fix would
 still cause an exception?

--
Ticket URL: <https://code.djangoproject.com/ticket/26802#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.451b600c244b0a1348c7d245d1f88d11%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to