How to encode international characters in recipient names (NOT addresses) with smtplib.sendmail() in Python 3?

后端 未结 2 377
春和景丽
春和景丽 2021-01-28 10:58

I\'m using a standard smtplib.sendmail() call in my Python 3 program to send emails, as follows:

smtp_session.sendmail(\'The Sender \',          


        
2条回答
  •  心在旅途
    2021-01-28 11:43

    The argument to smtplib.sendmail() should not have human-readable labels, just the address terminus.

    smtp_session.sendmail('sender@domain.com', ['recipient@domain.com'],
        'Simple test body here')
    

    The email.headerregistry module in Python 3.6+ has a facility for extracting just the email terminus, by way of parsing structured headers into objects with attributes.

    from email.headerregistry import AddressHeader
    
    hdr = dict()
    AddressHeader.parse('To: The ÅÄÖ Recipient ', hdr)
    for grp in hdr['groups']:
        for addr in grp.addresses:
            print('{0}@{1}'.format(addr.username, addr.domain))
    

    (I really hope there is a less convoluted way to access this functionality but at the very least this produces the expected result.)

    In the actual message, Python takes care of properly RFC2047-encoding any headers with Unicode content (if you use the correct methods from the email library to construct a prop0er MIME message); but this is pure presentation (RFC5322) not transport (RFC5321). So in the message itself you might see

    From: The Sender 
    To: The =?utf-8?Q?=C3=85=C3=84=C3=96_Recipient?= 
    

    though keep in mind that there is no requirement for the message content to actually reveal the transport sender or recipient headers. (Maybe tangentially see Header "To:" for a Bulk Email Sender)

提交回复
热议问题