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

Unicode sender #94

Closed
decaz opened this issue Oct 11, 2019 · 5 comments
Closed

Unicode sender #94

decaz opened this issue Oct 11, 2019 · 5 comments

Comments

@decaz
Copy link

decaz commented Oct 11, 2019

I'm specifying "From" header at Message object but it's not enough, - aiosmtplib.errors.SMTPNotSupported: An address containing non-ASCII characters was provided, but SMTPUTF8 is not supported by this server is raised.

The code is as follows:

import asyncio
from email.header import Header
from email.message import EmailMessage

import aiosmtplib

async def main():
    message = EmailMessage()
    message['Subject'] = 'Subject'
    sender = Header('Pepé', 'utf-8').encode()
    message['From'] = sender
    message['To'] = ('me@example.com',)
    message.set_content('Body')
    # Working
    await aiosmtplib.send(message, sender, hostname='smtp.host')
    # Not working
    await aiosmtplib.send(message, hostname='smtp.host')

asyncio.run(main())
@cole
Copy link
Owner

cole commented Oct 14, 2019

@decaz does the server support SMTPUTF8? Without that extension, headers are limited to ASCII. EDIT: I think even if SMTPUTF8 is unsupported, this text should be encoded as per RFC 2047. I'll look at this.

@cole
Copy link
Owner

cole commented Nov 3, 2019

Ok, I've added some bugfixes around this to master. You should now be able to use UTF8 chars in the sender or recipient's display name. To have them work in the actual email address, the server needs SMTPUTF8 support.

Note, here you're using Header (legacy python email API) with EmailMessage (new API). If you're using EmailMessage you should be able to do:

    message = EmailMessage()
    message['Subject'] = 'Subject'
    message['From'] = '"Pepé" <pepe@example.com>'

If you want to use the legacy compat32 API for some reason, it's Message, not EmailMessage, and don't encode the header.

    message = Message()
    message['Subject'] = 'Subject'
    message['From'] = Header('"Pepé" <pepe@example.com>', 'utf-8')

@decaz
Copy link
Author

decaz commented Nov 5, 2019

I've changed nothing in my example code but It gives me the following error:

Traceback (most recent call last):
  File "test_utf8_sender.py", line 19, in <module>
    asyncio.run(main())
  File "/home/decaz/.pyenv/versions/3.7.5/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/home/decaz/.pyenv/versions/3.7.5/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
    return future.result()
  File "test_utf8_sender.py", line 15, in main
    await aiosmtplib.send(message, sender, hostname='extra.devel.ifx')
  File "/home/decaz/workspace/aiosmtplib/src/aiosmtplib/api.py", line 397, in send
    message, sender=sender, recipients=recipients
  File "/home/decaz/workspace/aiosmtplib/src/aiosmtplib/smtp.py", line 260, in send_message
    recipients = extract_recipients(message)
  File "/home/decaz/workspace/aiosmtplib/src/aiosmtplib/email.py", line 162, in extract_recipients
    recipients.extend(extract_addresses(recipient))
  File "/home/decaz/workspace/aiosmtplib/src/aiosmtplib/email.py", line 108, in extract_addresses
    return [address.addr_spec for address in header.addresses]
  File "/home/decaz/workspace/aiosmtplib/src/aiosmtplib/email.py", line 108, in <listcomp>
    return [address.addr_spec for address in header.addresses]
AttributeError: 'str' object has no attribute 'addr_spec'

I guess it is because recipient is of string type, not Address instance.

@cole
Copy link
Owner

cole commented Nov 7, 2019

OK, I've added handling for that case as well to master, I was just looking at the encoding thing before.

That error is happening because you're using a tuple of strings to assign multiple addresses. That assigns a string where an email.headerregistry.Address would normally be. It does seem to work, though.

I would still say it's probably not great practice. There are many ways to generate normal Address objects: you can use a tuple of Address objects directly, assign a string multiple times, or assign a comma seperated string (', '.join). In this case just do message['To'] = 'me@example.com'.

@decaz
Copy link
Author

decaz commented Nov 8, 2019

Thanks! I think the issue can be closed now.

@decaz decaz closed this as completed Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants