cole / aiosmtplib

asyncio smtplib implementation
MIT License
354 stars 48 forks source link

Unicode sender #94

Closed decaz closed 5 years ago

decaz commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Thanks! I think the issue can be closed now.