Python email.Utils.formataddr() Examples

The following are 30 code examples for showing how to use email.Utils.formataddr(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module email.Utils , or try the search function .

Example 1
Project: pagure   Author: Pagure   File: git_multimail_upstream.py    License: GNU General Public License v2.0 6 votes vote down vote up
def addr_header_encode(text, header_name=None):
    """Encode and line-wrap the value of an email header field containing
    email addresses."""

    # Convert to unicode, if required.
    if not isinstance(text, unicode):
        text = unicode(text, "utf-8")

    text = ", ".join(
        formataddr((header_encode(name), emailaddr))
        for name, emailaddr in getaddresses([text])
    )

    if is_ascii(text):
        charset = "ascii"
    else:
        charset = "utf-8"

    return Header(
        text, header_name=header_name, charset=Charset(charset)
    ).encode() 
Example 2
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: message.py    License: Apache License 2.0 6 votes vote down vote up
def sanitize_address(addr, encoding):
    if isinstance(addr, basestring):
        addr = parseaddr(force_unicode(addr))
    nm, addr = addr
    nm = str(Header(nm, encoding))
    try:
        addr = addr.encode('ascii')
    except UnicodeEncodeError:  # IDN
        if u'@' in addr:
            localpart, domain = addr.split(u'@', 1)
            localpart = str(Header(localpart, encoding))
            domain = domain.encode('idna')
            addr = '@'.join([localpart, domain])
        else:
            addr = str(Header(addr, encoding))
    return formataddr((nm, addr)) 
Example 3
Project: pagure   Author: Pagure   File: git_multimail_upstream.py    License: GNU General Public License v2.0 5 votes vote down vote up
def set_recipients(self, name, value):
        self.unset_all(name)
        for pair in getaddresses([value]):
            self.add(name, formataddr(pair)) 
Example 4
Project: pagure   Author: Pagure   File: git_multimail_upstream.py    License: GNU General Public License v2.0 5 votes vote down vote up
def get_fromaddr(self, change=None):
        config = Config("user")
        fromname = config.get("name", default="")
        fromemail = config.get("email", default="")
        if fromemail:
            return formataddr([fromname, fromemail])
        return self.get_sender() 
Example 5
Project: ironpython2   Author: IronLanguages   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_parseaddr_empty(self):
        self.assertEqual(Utils.parseaddr('<>'), ('', ''))
        self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '') 
Example 6
Project: ironpython2   Author: IronLanguages   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_noquote_dump(self):
        self.assertEqual(
            Utils.formataddr(('A Silly Person', 'person@dom.ain')),
            'A Silly Person <person@dom.ain>') 
Example 7
Project: ironpython2   Author: IronLanguages   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_escape_dump(self):
        self.assertEqual(
            Utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
            r'"A \(Very\) Silly Person" <person@dom.ain>')
        a = r'A \(Special\) Person'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 8
Project: ironpython2   Author: IronLanguages   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_escape_backslashes(self):
        self.assertEqual(
            Utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
            r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
        a = r'Arthur \Backslash\ Foobar'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 9
Project: ironpython2   Author: IronLanguages   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_name_with_dot(self):
        x = 'John X. Doe <jxd@example.com>'
        y = '"John X. Doe" <jxd@example.com>'
        a, b = ('John X. Doe', 'jxd@example.com')
        self.assertEqual(Utils.parseaddr(x), (a, b))
        self.assertEqual(Utils.parseaddr(y), (a, b))
        # formataddr() quotes the name if there's a dot in it
        self.assertEqual(Utils.formataddr((a, b)), y) 
Example 10
Project: BinderFilter   Author: dxwu   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_parseaddr_empty(self):
        self.assertEqual(Utils.parseaddr('<>'), ('', ''))
        self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '') 
Example 11
Project: BinderFilter   Author: dxwu   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_noquote_dump(self):
        self.assertEqual(
            Utils.formataddr(('A Silly Person', 'person@dom.ain')),
            'A Silly Person <person@dom.ain>') 
Example 12
Project: BinderFilter   Author: dxwu   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_escape_dump(self):
        self.assertEqual(
            Utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
            r'"A \(Very\) Silly Person" <person@dom.ain>')
        a = r'A \(Special\) Person'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 13
Project: BinderFilter   Author: dxwu   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_escape_backslashes(self):
        self.assertEqual(
            Utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
            r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
        a = r'Arthur \Backslash\ Foobar'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 14
Project: BinderFilter   Author: dxwu   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_name_with_dot(self):
        x = 'John X. Doe <jxd@example.com>'
        y = '"John X. Doe" <jxd@example.com>'
        a, b = ('John X. Doe', 'jxd@example.com')
        self.assertEqual(Utils.parseaddr(x), (a, b))
        self.assertEqual(Utils.parseaddr(y), (a, b))
        # formataddr() quotes the name if there's a dot in it
        self.assertEqual(Utils.formataddr((a, b)), y) 
Example 15
Project: oss-ftp   Author: aliyun   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_parseaddr_empty(self):
        self.assertEqual(Utils.parseaddr('<>'), ('', ''))
        self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '') 
Example 16
Project: oss-ftp   Author: aliyun   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_noquote_dump(self):
        self.assertEqual(
            Utils.formataddr(('A Silly Person', 'person@dom.ain')),
            'A Silly Person <person@dom.ain>') 
Example 17
Project: oss-ftp   Author: aliyun   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_escape_dump(self):
        self.assertEqual(
            Utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
            r'"A \(Very\) Silly Person" <person@dom.ain>')
        a = r'A \(Special\) Person'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 18
Project: oss-ftp   Author: aliyun   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_escape_backslashes(self):
        self.assertEqual(
            Utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
            r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
        a = r'Arthur \Backslash\ Foobar'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 19
Project: oss-ftp   Author: aliyun   File: test_email.py    License: MIT License 5 votes vote down vote up
def test_name_with_dot(self):
        x = 'John X. Doe <jxd@example.com>'
        y = '"John X. Doe" <jxd@example.com>'
        a, b = ('John X. Doe', 'jxd@example.com')
        self.assertEqual(Utils.parseaddr(x), (a, b))
        self.assertEqual(Utils.parseaddr(y), (a, b))
        # formataddr() quotes the name if there's a dot in it
        self.assertEqual(Utils.formataddr((a, b)), y) 
Example 20
Project: datafari   Author: francelabs   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_parseaddr_empty(self):
        self.assertEqual(Utils.parseaddr('<>'), ('', ''))
        self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '') 
Example 21
Project: datafari   Author: francelabs   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_noquote_dump(self):
        self.assertEqual(
            Utils.formataddr(('A Silly Person', 'person@dom.ain')),
            'A Silly Person <person@dom.ain>') 
Example 22
Project: datafari   Author: francelabs   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_escape_dump(self):
        self.assertEqual(
            Utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
            r'"A \(Very\) Silly Person" <person@dom.ain>')
        a = r'A \(Special\) Person'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 23
Project: datafari   Author: francelabs   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_escape_backslashes(self):
        self.assertEqual(
            Utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
            r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
        a = r'Arthur \Backslash\ Foobar'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 24
Project: datafari   Author: francelabs   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_name_with_dot(self):
        x = 'John X. Doe <jxd@example.com>'
        y = '"John X. Doe" <jxd@example.com>'
        a, b = ('John X. Doe', 'jxd@example.com')
        self.assertEqual(Utils.parseaddr(x), (a, b))
        self.assertEqual(Utils.parseaddr(y), (a, b))
        # formataddr() quotes the name if there's a dot in it
        self.assertEqual(Utils.formataddr((a, b)), y) 
Example 25
Project: medicare-demo   Author: ofermend   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_parseaddr_empty(self):
        self.assertEqual(Utils.parseaddr('<>'), ('', ''))
        self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '') 
Example 26
Project: medicare-demo   Author: ofermend   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_noquote_dump(self):
        self.assertEqual(
            Utils.formataddr(('A Silly Person', 'person@dom.ain')),
            'A Silly Person <person@dom.ain>') 
Example 27
Project: medicare-demo   Author: ofermend   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_escape_dump(self):
        self.assertEqual(
            Utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')),
            r'"A \(Very\) Silly Person" <person@dom.ain>')
        a = r'A \(Special\) Person'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 28
Project: medicare-demo   Author: ofermend   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_escape_backslashes(self):
        self.assertEqual(
            Utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')),
            r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>')
        a = r'Arthur \Backslash\ Foobar'
        b = 'person@dom.ain'
        self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) 
Example 29
Project: medicare-demo   Author: ofermend   File: test_email.py    License: Apache License 2.0 5 votes vote down vote up
def test_name_with_dot(self):
        x = 'John X. Doe <jxd@example.com>'
        y = '"John X. Doe" <jxd@example.com>'
        a, b = ('John X. Doe', 'jxd@example.com')
        self.assertEqual(Utils.parseaddr(x), (a, b))
        self.assertEqual(Utils.parseaddr(y), (a, b))
        # formataddr() quotes the name if there's a dot in it
        self.assertEqual(Utils.formataddr((a, b)), y) 
Example 30
Project: CTFCrackTools-V2   Author: Acmesec   File: test_email.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_parseaddr_empty(self):
        self.assertEqual(Utils.parseaddr('<>'), ('', ''))
        self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '')