Python email.Utils.getaddresses() Examples

The following are 29 code examples of email.Utils.getaddresses(). 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 also want to check out all available functions/classes of the module email.Utils , or try the search function .
Example #1
Source File: git_multimail_upstream.py    From pagure with 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
Source File: message.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def forbid_multi_line_headers(name, val, encoding):
    """Forbids multi-line headers, to prevent header injection."""
    encoding = encoding or settings.DEFAULT_CHARSET
    val = force_unicode(val)
    if '\n' in val or '\r' in val:
        raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))
    try:
        val = val.encode('ascii')
    except UnicodeEncodeError:
        if name.lower() in ADDRESS_HEADERS:
            val = ', '.join(sanitize_address(addr, encoding)
                for addr in getaddresses((val,)))
        else:
            val = str(Header(val, encoding))
    else:
        if name.lower() == 'subject':
            val = Header(val)
    return name, val 
Example #3
Source File: test_email.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #4
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #5
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #6
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #7
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #8
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #9
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #10
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #11
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #12
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #13
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #14
Source File: test_email.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #15
Source File: test_email.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #16
Source File: test_email.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #17
Source File: test_email.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #18
Source File: test_email.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #19
Source File: test_email.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #20
Source File: test_email.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #21
Source File: test_email.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #22
Source File: test_email.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #23
Source File: test_email.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #24
Source File: test_email.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #25
Source File: test_email.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_getaddresses_embedded_comment(self):
        """Test proper handling of a nested comment"""
        eq = self.assertEqual
        addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
        eq(addrs[0][1], 'foo@bar.com') 
Example #26
Source File: test_email.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_getaddresses_nasty(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['foo: ;']), [('', '')])
        eq(Utils.getaddresses(
           ['[]*-- =~$']),
           [('', ''), ('', ''), ('', '*--')])
        eq(Utils.getaddresses(
           ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
           [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) 
Example #27
Source File: test_email.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_getaddresses(self):
        eq = self.assertEqual
        eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
                               'Bud Person <bperson@dom.ain>']),
           [('Al Person', 'aperson@dom.ain'),
            ('Bud Person', 'bperson@dom.ain')]) 
Example #28
Source File: git_multimail_upstream.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def send(self, lines, to_addrs):
        try:
            if self.username or self.password:
                self.smtp.login(self.username, self.password)
            msg = "".join(lines)
            # turn comma-separated list into Python list if needed.
            if is_string(to_addrs):
                to_addrs = [
                    email for (name, email) in getaddresses([to_addrs])
                ]
            self.smtp.sendmail(self.envelopesender, to_addrs, msg)
        except smtplib.SMTPResponseException:
            err = sys.exc_info()[1]
            self.environment.get_logger().error(
                "*** Error sending email ***\n"
                "*** Error %d: %s\n"
                % (err.smtp_code, bytes_to_str(err.smtp_error))
            )
            try:
                smtp = self.smtp
                # delete the field before quit() so that in case of
                # error, self.smtp is deleted anyway.
                del self.smtp
                smtp.quit()
            except:
                self.environment.get_logger().error(
                    "*** Error closing the SMTP connection ***\n"
                    "*** Exiting anyway ... ***\n"
                    "*** %s\n" % sys.exc_info()[1]
                )
            sys.exit(1) 
Example #29
Source File: git_multimail_upstream.py    From pagure with 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))