MCPcopy Index your code
hub / github.com/python/cpython / formataddr

Function formataddr

Lib/email/utils.py:72–104  ·  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 is the

(pair, charset='utf-8')

Source from the content-addressed store, hash-verified

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

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…