Binary file email attachment problem

Discussion in 'Python' started by Alan Harris-Reid, May 10, 2010.

  1. Hi there,

    Using Python 3.1.2 I am having a problem sending binary attachment files
    (jpeg, pdf, etc.) - MIMEText attachments work fine. The code in question
    is as follows...

    for file in self.attachments:
    part = MIMEBase('application', "octet-stream")
    part.set_payload(open(file,"rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' %
    file)
    msg.attach(part) # msg is an instance of MIMEMultipart()

    server = smtplib.SMTP(host, port)
    server.login(username, password)
    server.sendmail(from_addr, all_recipients, msg.as_string())

    However, way down in the calling-stack (see traceback below), it looks
    as though msg.as_string() has received an attachment which creates a
    payload of 'bytes' type instead of string. I suspect the error lies in
    the encoding somewhere, but I've no idea where. I have also tried
    MIMEApplication and MIMEImage, but the error still remains. I have seen
    many similar code examples on the web, all of which fail for me, so I
    wonder if there is something wrong with my environment.

    Has anyone any idea what might be causing the problem? Any help would be
    appreciated.

    Alan

    ________________________________________________________________

    builtins.TypeError: string payload expected: <class 'bytes'>
    File "c:\Dev\CommonPY\Scripts\email_send.py", line 147, in send
    server.sendmail(self.from_addr, all_recipients, msg.as_string())
    File "c:\Program Files\Python31\Lib\email\message.py", line 136, in
    as_string
    g.flatten(self, unixfrom=unixfrom)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 76, in flatten
    self._write(msg)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 101, in _write
    self._dispatch(msg)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 127, in
    _dispatch
    meth(msg)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 181, in
    _handle_multipart
    g.flatten(part, unixfrom=False)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 76, in flatten
    self._write(msg)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 101, in _write
    self._dispatch(msg)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 127, in
    _dispatch
    meth(msg)
    File "c:\Program Files\Python31\Lib\email\generator.py", line 155, in
    _handle_text
    raise TypeError('string payload expected: %s' % type(payload))

    ____________________________________________________________
     
    Alan Harris-Reid, May 10, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2liQW5kZWxh?=

    uploaded file as attachment to email generate on server

    =?Utf-8?B?U2liQW5kZWxh?=, Jul 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    =?Utf-8?B?U2liQW5kZWxh?=
    Jul 29, 2004
  2. stephan
    Replies:
    2
    Views:
    625
    steph
    Nov 18, 2004
  3. Gordon H.
    Replies:
    5
    Views:
    13,033
    Daniel R. Tobias
    May 1, 2004
  4. Replies:
    3
    Views:
    3,618
    M.J. Dance
    Dec 11, 2006
  5. ecureuil
    Replies:
    0
    Views:
    364
    ecureuil
    May 28, 2006
Loading...

Share This Page