MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / formataddr

Function formataddr

tools/python-3.11.9-amd64/Lib/email/utils.py:76–106  ·  view source on GitHub ↗

The inverse of parseaddr(), this takes a 2-tuple of the form (realname, email_address) and returns the string value suitable for an RFC 2822 From, To or Cc header. If the first element of pair is false, then the second element is returned unmodified. The optional charset

(pair, charset='utf-8')

Source from the content-addressed store, hash-verified

74# Helpers
75
76def formataddr(pair, charset='utf-8'):
77 """The inverse of parseaddr(), this takes a 2-tuple of the form
78 (realname, email_address) and returns the string value suitable
79 for an RFC 2822 From, To or Cc header.
80
81 If the first element of pair is false, then the second element is
82 returned unmodified.
83
84 The optional charset is the character set that is used to encode
85 realname in case realname is not ASCII safe. Can be an instance of str or
86 a Charset-like object which has a header_encode method. Default is
87 'utf-8'.
88 """
89 name, address = pair
90 # The address MUST (per RFC) be ascii, so raise a UnicodeError if it isn't.
91 address.encode('ascii')
92 if name:
93 try:
94 name.encode('ascii')
95 except UnicodeEncodeError:
96 if isinstance(charset, str):
97 charset = Charset(charset)
98 encoded_name = charset.header_encode(name)
99 return "%s <%s>" % (encoded_name, address)
100 else:
101 quotes = ''
102 if specialsre.search(name):
103 quotes = '"'
104 name = escapesre.sub(r'\\\g<0>', name)
105 return '%s%s%s <%s>' % (quotes, name, quotes, address)
106 return address
107
108
109

Callers

nothing calls this directly

Calls 5

header_encodeMethod · 0.95
CharsetClass · 0.90
encodeMethod · 0.45
searchMethod · 0.45
subMethod · 0.45

Tested by

no test coverage detected