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

regression from 2.6: smtplib.py requiring ascii for sending messages #48653

Closed
ccgus mannequin opened this issue Nov 24, 2008 · 7 comments
Closed

regression from 2.6: smtplib.py requiring ascii for sending messages #48653

ccgus mannequin opened this issue Nov 24, 2008 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ccgus
Copy link
Mannequin

ccgus mannequin commented Nov 24, 2008

BPO 4403
Nosy @loewis, @devdanzin, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-11-05.09:08:30.008>
created_at = <Date 2008-11-24.03:59:03.018>
labels = ['invalid', 'type-bug', 'library']
title = 'regression from 2.6: smtplib.py requiring ascii for sending messages'
updated_at = <Date 2010-11-05.09:12:26.587>
user = 'https://bugs.python.org/ccgus'

bugs.python.org fields:

activity = <Date 2010-11-05.09:12:26.587>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2010-11-05.09:08:30.008>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2008-11-24.03:59:03.018>
creator = 'ccgus'
dependencies = []
files = []
hgrepos = []
issue_num = 4403
keywords = []
message_count = 7.0
messages = ['76295', '76301', '76303', '76306', '76425', '86592', '120478']
nosy_count = 4.0
nosy_names = ['loewis', 'ajaksu2', 'ccgus', 'r.david.murray']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue4403'
versions = ['Python 3.0', 'Python 3.1']

@ccgus
Copy link
Mannequin Author

ccgus mannequin commented Nov 24, 2008

smtplib requires that messages being sent be in ascii, and throws an exception otherwise.
Python 2.6 doesn't require this. Here's the diff where it was introduced:
http://svn.python.org/view/python/branches/py3k/Lib/smtplib.py?rev=59102&r1=58495&r2=59102

Is there a good reason for this? I use python for a webstore, and send out emails for folks
with multibyte names (for instance, if a name has an umlaut).

Here's a code snippit + exception:

Python 3.0rc3 (r30rc3:67312, Nov 22 2008, 18:45:57) 
[GCC 3.4.6 [FreeBSD] 20060305] on freebsd6
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> server = smtplib.SMTP("localhost")
>>> server.sendmail("gus@flyingmeat.com", "gus@flyingmeat.com", "Ümlaut")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/smtplib.py", line 713, in sendmail
    (code,resp) = self.data(msg)
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/smtplib.py", line 481, in data
    self.send(q)
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/smtplib.py", line 305, in send
    s = s.encode("ascii")
UnicodeEncodeError: 'ascii' codec can't encode character '\xdc' in position 0: ordinal not in 
range(128)

Is there a workaround or a new way of using it? I couldn't seem to find it.

Thanks!

@ccgus ccgus mannequin added the stdlib Python modules in the Lib dir label Nov 24, 2008
@loewis
Copy link
Mannequin

loewis mannequin commented Nov 24, 2008

Is there a good reason for this?

Most definitely. In Python 2.x, the string literal denotes
a byte string, whereas in 3.x, it is a character string.
It's not possible to send a character string directly over
the network; try encoding it.

It might be considered a bug that sendmail accepts a string
at all as long as the string only consists of ASCII characters;
it should reject such strings as well.

@ccgus
Copy link
Mannequin Author

ccgus mannequin commented Nov 24, 2008

Encoding the message first doesn't work either:

>>> import smtplib
>>> server = smtplib.SMTP("localhost")
>>> server.sendmail("gus@flyingmeat.com", "gus@flyingmeat.com", "Ümlaut".encode("UTF-8"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/smtplib.py", line 713, in sendmail
    (code,resp) = self.data(msg)
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/smtplib.py", line 477, in data
    q = quotedata(msg)
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/smtplib.py", line 157, in quotedata
    re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data))
  File "/home/mu.org/home/gus/unix/python3/lib/python3.0/re.py", line 165, in sub
    return _compile(pattern, 0).sub(repl, string, count)
TypeError: can't use a string pattern on a bytes-like object

Should a check be done in data() first, before it tries to try string operations on it?

@loewis
Copy link
Mannequin

loewis mannequin commented Nov 24, 2008

I see. It seems Python 3.0 just won't support that usage, then.

You still should be able to use MIME to send non-ASCII characters.

@ccgus
Copy link
Mannequin Author

ccgus mannequin commented Nov 25, 2008

For completeness, if anyone runs across this in the future, the following seems to work for sending utf-8 mail
in python 3:

import smtplib
import email.mime.text

msg = email.mime.text.MIMEText("Ümlaut", _charset="UTF-8")

smtp = smtplib.SMTP('localhost')
smtp.sendmail('gus@flyingmeat.com', 'gus@flyingmeat.com', "Subject: This is your mail\n" + msg.as_string())
smtp.quit()

@devdanzin
Copy link
Mannequin

devdanzin mannequin commented Apr 26, 2009

We might want to add the workaround to docs or even to extend smtplib to
support it.

@devdanzin devdanzin mannequin added the type-bug An unexpected behavior, bug, or error label Apr 26, 2009
@bitdancer
Copy link
Member

I'm closing this issue as invalid, since as Martin pointed out you can't send unicode over the wire.

However, see bpo-10321 where I've attached a patch that adds support for sending binary data as a by-product of adding support for Message objects.

From Martin's msg76301 I gather he initially expected sendmail to take only binary data, which would it seems to me probably be the better API. However at this point we are stuck with supporting ASCII-only strings in the API as well.

As a further note, however, the original example in this issue would have produced a non-RFC-conformant message when used in python 2.x. You can't just send 8bit data willy-nilly, there are rules that should be followed. Which is why the bpo-10321 patch adds support for using Message objects, since the email package knows what those rules are...

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant