def encode_address(addr):
    """
    Given a unicode address *addr*, return a (byte, on
    Python 3) string that is suitable to be passed as an
    argument to sendmail.
    """
    try:
        addr = addr.encode('ascii')
    except UnicodeEncodeError:
        if '@' in addr:
            localpart, domain = addr.split('@', 1)
            addr = b'@'.join([
                localpart.encode('utf-8'),
                domain.encode('idna'),
            ])
        else:
            addr = addr.encode('utf-8')
    return addr.decode('utf-8')