This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib SMTP.sendmail (TypeError: expected string or buffer)
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: honyczek, r.david.murray
Priority: normal Keywords:

Created on 2010-03-03 15:00 by Allison.Vollmann, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg100346 - (view) Author: Allison Vollmann (Allison.Vollmann) Date: 2010-03-03 15:00
When call SMTP.sendmail (with simple sendmail local sent and with smtp auth), the follow exception be raised (with debug output):

send: 'mail FROM:<xxx> size=5\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO:<xxx>\r\n'
reply: '250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: 2.1.5 Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    s.sendmail(to, to, msg)
  File "C:\Python26\lib\smtplib.py", line 710, in sendmail
    (code,resp) = self.data(msg)
  File "C:\Python26\lib\smtplib.py", line 474, in data
    q = quotedata(msg)
  File "C:\Python26\lib\smtplib.py", line 157, in quotedata
    re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data))
  File "C:\Python26\lib\re.py", line 151, in sub
    return _compile(pattern, 0).sub(repl, string, count)
TypeError: expected string or buffer

This error appear because 'data' isn't an string object, just replacing "q = quotedata(msg)" for "q = quotedata(str(msg))" (in Python26\lib\smtplib.py:474) solves the problem, but i can't understand how this simple mistake does not be noticed

Teste in: 
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
[GCC 4.3.2] on linux2,
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
msg100641 - (view) Author: Allison Vollmann (Allison.Vollmann) Date: 2010-03-08 13:46
i believe which the parameter "msg" must be referenced as a string type when called "sendmail" nor as an MIME[Message, Multipart, Text, Base, Audio] 

and this is obvious way to do it. but i'm not Dutch :-)
msg100653 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-03-08 15:48
On the other hand, having a facility somewhere in the stdlib to pass a Message object to SMTP sendmail would be handy.  I've made a note of this in my working notes for email6 to see if there's something we want to do about it.
msg120477 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-05 09:11
See issue 10321 for a proposal to add Message support to smtplib.
msg137472 - (view) Author: Jan Papež (honyczek) Date: 2011-06-02 11:13
Did you try to use this?
q = quotedata(msg.as_string())
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52298
2011-06-02 11:13:05honyczeksetnosy: + honyczek
messages: + msg137472
2010-11-05 09:11:06r.david.murraysetnosy: - Allison.Vollmann
messages: + msg120477
2010-03-08 15:48:38r.david.murraysetpriority: normal

nosy: + r.david.murray
messages: + msg100653

resolution: not a bug
stage: resolved
2010-03-08 13:46:43Allison.Vollmannsetstatus: open -> closed

messages: + msg100641
2010-03-03 15:24:07eric.smithsettype: crash -> behavior
components: + Library (Lib), - Regular Expressions
2010-03-03 15:00:15Allison.Vollmanncreate