예제 #1
0
def format_addresses(addresses: Iterable[AddressOrGroup], encode: bool = False) -> str:
    """
    Convert an iterable of e-mail address strings (of the form
    "``[email protected]``", without angle brackets or a display name),
    `~email.headerregistry.Address` objects, and/or
    `~email.headerregistry.Group` objects into a formatted string.  If
    ``encode`` is `False` (the default), non-ASCII characters are left as-is.
    If it is `True`, non-ASCII display names are converted into :RFC:`2047`
    encoded words, and non-ASCII domain names are encoded using Punycode.
    """
    addrs = []
    for a in addresses:
        if isinstance(a, str):
            a = Address(addr_spec=a)
        if encode:
            if isinstance(a, Address):
                a = idna_address(a)
            else:
                assert isinstance(a, Group)
                a = Group(a.display_name, tuple(map(idna_address, a.addresses)))
        addrs.append(a)
    msg = EmailMessage()
    msg["To"] = addrs
    if encode:
        folded = msg["To"].fold(policy=ENCODED_POLICY)
        assert isinstance(folded, str)
        if folded == "To:\n":
            return ""
        prefix = "To: "
        assert folded.startswith(prefix)
        return folded[len(prefix) :].rstrip("\n")
    else:
        return str(msg["To"])
    def test_set_from_Address_and_Group_list(self):
        h = self.make_header('to', [Address('me', 'foo', 'example.com'),
            Group('bing', [Address('fiz', 'z', 'b.com'), Address('zif', 'f',
            'c.com')]), Address('you', 'bar', 'example.com')])
        self.assertEqual(h,
            'me <*****@*****.**>, bing: fiz <*****@*****.**>, zif <*****@*****.**>;, you <*****@*****.**>'
            )
        self.assertEqual(h.fold(policy=policy.default.clone(max_line_length
            =40)),
            """to: me <*****@*****.**>,
 bing: fiz <*****@*****.**>, zif <*****@*****.**>;,
 you <*****@*****.**>
"""
            )
예제 #3
0
     "Some User <*****@*****.**>, [email protected]",
     [
         Address("Some User", addr_spec="*****@*****.**"),
         Address(addr_spec="*****@*****.**"),
     ],
 ),
 (
     (
         "friends: [email protected], [email protected];,"
         " enemies:[email protected], [email protected];"
     ),
     [
         Group(
             "friends",
             (
                 Address(addr_spec="*****@*****.**"),
                 Address(addr_spec="*****@*****.**"),
             ),
         ),
         Group(
             "enemies",
             (
                 Address(addr_spec="*****@*****.**"),
                 Address(addr_spec="*****@*****.**"),
             ),
         ),
     ],
 ),
 (
     "Zoë Façade <zoe.facade@naïveté.fr>",
     [Address("Zoë Façade", addr_spec="zoe.facade@naïveté.fr")],
예제 #4
0
def add_recipients(msg: EmailMessage):
    """Add the recipients listed in the local config file to the email"""
    with open(email_recipients_filepath, 'r') as recipient_file:
        recipients = json.load(recipient_file)['recipients']
    msg['To'] = Group('Report Group',
                      list(map(lambda x: Address(**x), recipients)))
 def test_set_message_header_from_group(self):
     g = Group('foo bar')
     m = Message(policy=policy.default)
     m['To'] = g
     self.assertEqual(m['to'], 'foo bar:;')
     self.assertEqual(m['to'].addresses, g.addresses)
 def test_display_name_blanks_not_quoted(self):
     g = Group('foo bar')
     self.assertEqual(g.display_name, 'foo bar')
     self.assertEqual(g.addresses, tuple())
     self.assertEqual(str(g), 'foo bar:;')
 def test_display_name_quoting(self):
     g = Group('foo.bar')
     self.assertEqual(g.display_name, 'foo.bar')
     self.assertEqual(g.addresses, tuple())
     self.assertEqual(str(g), '"foo.bar":;')
 def test_group_with_one_address_no_display_name(self):
     addrs = [Address('b', 'b', 'c')]
     g = Group(addresses=addrs)
     self.assertIsNone(g.display_name)
     self.assertEqual(g.addresses, tuple(addrs))
     self.assertEqual(str(g), 'b <b@c>')
 def test_group_with_addresses(self):
     addrs = [Address('b', 'b', 'c'), Address('a', 'b', 'c')]
     g = Group('foo', addrs)
     self.assertEqual(g.display_name, 'foo')
     self.assertEqual(g.addresses, tuple(addrs))
     self.assertEqual(str(g), 'foo: b <b@c>, a <b@c>;')
 def test_null_group(self):
     g = Group()
     self.assertIsNone(g.display_name)
     self.assertEqual(g.addresses, tuple())
     self.assertEqual(str(g), 'None:;')
 def test_empty_group_list(self):
     g = Group('foo', addresses=[])
     self.assertEqual(g.display_name, 'foo')
     self.assertEqual(g.addresses, tuple())
     self.assertEqual(str(g), 'foo:;')
 def test_group_addresses_ro(self):
     self._test_attr_ro(Group('foo'), 'addresses')
 def test_group_display_name_ro(self):
     self._test_attr_ro(Group('foo'), 'display_name')
 def test_set_from_Group_list(self):
     h = self.make_header('to', [Group('bing', [Address('fiz', 'z',
         'b.com'), Address('zif', 'f', 'c.com')])])
     self.assertEqual(h, 'bing: fiz <*****@*****.**>, zif <*****@*****.**>;')
예제 #15
0
         Address("Fabian Oo", addr_spec="*****@*****.**"),
         Address("Bastian Arrr", addr_spec="*****@*****.**"),
     ],
     "Fabian Oo <*****@*****.**>, Bastian Arrr <*****@*****.**>",
 ),
 (
     [Address("Fabian O. Oh", addr_spec="*****@*****.**")],
     '"Fabian O. Oh" <*****@*****.**>',
 ),
 (
     [Address("Zoë Façade", addr_spec="zoe.facade@naïveté.fr")],
     "Zoë Façade <zoe.facade@naïveté.fr>",
 ),
 (
     [
         Group("undisclosed recipients", ()),
         "*****@*****.**",
         Group(
             "friends",
             (
                 Address("", addr_spec="*****@*****.**"),
                 Address("Thaddeus Hem", addr_spec="*****@*****.**"),
             ),
         ),
     ],
     "undisclosed recipients:;, [email protected],"
     " friends: [email protected], Thaddeus Hem <*****@*****.**>;",
 ),
 (
     [
         Address(