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')
| 70 | # Helpers |
| 71 | |
| 72 | def 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 | |
| 107 | def _iter_escaped_chars(addr): |
nothing calls this directly
no test coverage detected
searching dependent graphs…