Python rfc822.parseaddr() Examples

The following are 7 code examples of rfc822.parseaddr(). 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 rfc822 , or try the search function .
Example #1
Source File: user.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def canon_email(address):
    address = to_utf8(address)
    address = parseaddr(address)[1]
    if not address:
        raise ValueError("email address must be non-empty")
    if '@' not in address:
        raise ValueError("email address does not contain the required '@' character")
    localpart, hostname = address.split('@')
    hostname = hostname.rstrip('.')
    if not hostname:
        raise ValueError("email address hostname must be non-empty")
    if '.' not in hostname:
        raise ValueError("email address hostname must contain the '.' character and a domain name")
    if not localpart:
        raise ValueError("email address local part must be non-empty")
#   if '+' in localpart:
#       raise ValueError("email address local part must not contain the '+' character.")
    hostname = encode_idna(hostname)
    hostname = hostname.decode('utf-8').lower().encode('utf-8')
    address = '%s@%s' % (localpart, hostname)
    if not _valid_email_re.match(address):
        raise ValueError("email address does not match the permitted pattern")
    return address 
Example #2
Source File: relaymanager.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _checkStateMX(self):
        nextMessages = self.queue.getWaiting()
        nextMessages.reverse()

        exchanges = {}
        for msg in nextMessages:
            from_, to = self.queue.getEnvelope(msg)
            name, addr = rfc822.parseaddr(to)
            parts = addr.split('@', 1)
            if len(parts) != 2:
                log.err("Illegal message destination: " + to)
                continue
            domain = parts[1]

            self.queue.setRelaying(msg)
            exchanges.setdefault(domain, []).append(self.queue.getPath(msg))
            if len(exchanges) >= (self.maxConnections - len(self.managed)):
                break

        if self.mxcalc is None:
            self.mxcalc = MXCalculator()

        relays = []
        for (domain, msgs) in exchanges.iteritems():
            manager = _AttemptManager(self)
            factory = self.factory(msgs, manager, *self.fArgs, **self.fKwArgs)
            self.managed[factory] = map(os.path.basename, msgs)
            relayAttemptDeferred = manager.getCompletionDeferred()
            connectSetupDeferred = self.mxcalc.getMX(domain)
            connectSetupDeferred.addCallback(lambda mx: str(mx.name))
            connectSetupDeferred.addCallback(self._cbExchange, self.PORT, factory)
            connectSetupDeferred.addErrback(lambda err: (relayAttemptDeferred.errback(err), err)[1])
            connectSetupDeferred.addErrback(self._ebExchange, factory, domain)
            relays.append(relayAttemptDeferred)
        return DeferredList(relays) 
Example #3
Source File: smtp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def quoteaddr(addr):
    """Turn an email address, possibly with realname part etc, into
    a form suitable for and SMTP envelope.
    """

    if isinstance(addr, Address):
        return '<%s>' % str(addr)

    res = rfc822.parseaddr(addr)

    if res == (None, None):
        # It didn't parse, use it as-is
        return '<%s>' % str(addr)
    else:
        return '<%s>' % str(res[1]) 
Example #4
Source File: test_rfc822.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_parseaddr(self):
        eq = self.assertEqual
        eq(rfc822.parseaddr('<>'), ('', ''))
        eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain'))
        eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'),
           ('Bea A. Person', 'bperson@dom.ain'))
        eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'),
           ('Cynthia Person', 'cperson@dom.ain')) 
Example #5
Source File: relaymanager.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def _checkStateMX(self):
        nextMessages = self.queue.getWaiting()
        nextMessages.reverse()
        
        exchanges = {}
        for msg in nextMessages:
            from_, to = self.queue.getEnvelope(msg)
            name, addr = rfc822.parseaddr(to)
            parts = addr.split('@', 1)
            if len(parts) != 2:
                log.err("Illegal message destination: " + to)
                continue
            domain = parts[1]
            
            self.queue.setRelaying(msg)
            exchanges.setdefault(domain, []).append(self.queue.getPath(msg))
            if len(exchanges) >= (self.maxConnections - len(self.managed)):
                break
        
        if self.mxcalc is None:
            self.mxcalc = MXCalculator()
        
        for (domain, msgs) in exchanges.iteritems():
            factory = self.factory(msgs, self, *self.fArgs, **self.fKwArgs)
            self.managed[factory] = map(os.path.basename, msgs)
            self.mxcalc.getMX(domain
            ).addCallback(lambda mx: str(mx.name),
            ).addCallback(self._cbExchange, self.PORT, factory
            ).addErrback(self._ebExchange, factory, domain
            ) 
Example #6
Source File: smtp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def quoteaddr(addr):
    """Turn an email address, possibly with realname part etc, into
    a form suitable for and SMTP envelope.
    """

    if isinstance(addr, Address):
        return '<%s>' % str(addr)

    res = rfc822.parseaddr(addr)

    if res == (None, None):
        # It didn't parse, use it as-is
        return '<%s>' % str(addr)
    else:
        return '<%s>' % str(res[1]) 
Example #7
Source File: mail.py    From sendgrid-django with MIT License 5 votes vote down vote up
def _process_email_addr(self, email_addr):
        from_name, from_email = rfc822.parseaddr(email_addr)

        # Python sendgrid client should improve
        # sendgrid/helpers/mail/mail.py:164
        if not from_name:
            from_name = None

        return Email(from_email, from_name)