Пример #1
0
def email_validator(email):
    try:
        e = Email()
        e.to_python(email)
        return True
    except Invalid:
        return False
Пример #2
0
class TestUnicodeEmailWithResolveDomain(TestCase):
    
    def setUp(self):
        self.validator = Email(resolve_domain=True)

    def test_unicode_ascii_subgroup(self):
        self.assertEqual(self.validator.to_python(u'*****@*****.**'), '*****@*****.**')
    
    def test_cyrillic_email(self):
        self.assertEqual(self.validator.to_python(u'me@письмо.рф'), u'me@письмо.рф')
class TestUnicodeEmailWithResolveDomain(unittest.TestCase):
    def setUp(self):
        self.validator = Email(resolve_domain=False)

    def test_unicode_ascii_subgroup(self):
        self.assertEqual(self.validator.to_python(u'*****@*****.**'),
                         '*****@*****.**')

    def test_cyrillic_email(self):
        self.assertEqual(self.validator.to_python(u'me@письмо.рф'),
                         u'me@письмо.рф')
Пример #4
0
class TestUnicodeEmailWithResolveDomain(unittest.TestCase):
    def setUp(self):
        self.validator = Email(resolve_domain=True)

    def test_unicode_ascii_subgroup(self):
        self.assertEqual(self.validator.to_python('*****@*****.**'),
                         '*****@*****.**')

    def test_cyrillic_email(self):
        return
        # NOTE test failing because domain is expired. Need a new example domain
        self.assertEqual(self.validator.to_python('me@письмо.рф'),
                         'me@письмо.рф')
Пример #5
0
def send_mail(subject, plain_text, html=None, from_addr=None, passwd=None,
              to_addr=None):
    """Send an email with Gmail using provided details and content.

    Thanks to StackOverflow answer:
        https://stackoverflow.com/questions/882712/sending-html-email-using-python

    @param subject: Subject for mail.
    @param plain_text: Body of mail in plain text.
    @param html: Optional body of mail in HTML form.
    @param from_addr: Email address to send from. If not supplied, configured
        default value is used.
    @param passwd: Password of email address to send from. If not the supplied,
        the configured default value is used.
    @param to_addr: Email address to send to. If not supplied, the configured
        default is used.

    @return: None
    """
    if not from_addr:
        to_addr = conf.get('Email', 'to')
        from_addr = conf.get('Email', 'from')
        passwd = conf.get('Email', 'password')

    # Raise Invalid error for bad email formatting.
    Email.to_python(to_addr)
    Email.to_python(from_addr)

    assert from_addr.endswith("@gmail.com"), "Configured from address must be"\
        " a Gmail account."

    msg = MIMEMultipart('alternative')
    msg['To'] = to_addr
    msg['From'] = from_addr
    msg['Subject'] = subject

    msg.attach(MIMEText(plain_text, 'plain'))
    if html:
        msg.attach(MIMEText(html, 'html'))

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    try:
        server.login(from_addr, passwd)
    except smtplib.SMTPAuthenticationError:
        raise ValueError("Invalid Username and Password. Update conf file and"
                         " try again.")
    text = msg.as_string()
    server.sendmail(from_addr, to_addr, text)
    server.quit()
Пример #6
0
class TestEmailDomainBlacklist(unittest.TestCase):

    def setUp(self):
        self.validator = Email(domain_blacklist=[
            'example.com',
            '123.net'
        ])

    def validate(self, *args):
        return self.validator.to_python(*args)

    def test_blacklisted_domains(self):
        email_addresses = [
            # (email address, expected email address),
            (' [email protected] ', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('o*[email protected]', 'o*[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('[email protected]', '[email protected]'),
            ('foo{bar}@example.com', 'foo{bar}@example.com'),
            ('customer/[email protected]',
                'customer/[email protected]'),
            ('[email protected]', '[email protected]'),
            ('!def!xyz%[email protected]', '!def!xyz%[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**')]

        for email, expected in email_addresses:
            if 'example.com' in email or '123.net' in email:
                self.assertRaises(Invalid, self.validate, email)
            else:
                self.assertEqual(self.validate(email), expected)
Пример #7
0
class TestEmail(unittest.TestCase):

    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid as e:
            return unicode(e)

    def message(self, message_name, username, domain):
        email = '@'.join((username, domain))
        return self.validator.message(
            message_name, email, username=username, domain=domain)

    def test_invalid_email_addresses(self):
        invalid_usernames = [
            # (username, domain, message_name),
            ('foo\tbar', 'formencode.org', 'badUsername'),
            ('foo\nbar', 'formencode.org', 'badUsername'),
            ('test', '', 'noAt'),
            ('test', 'foobar', 'badDomain'),
            ('test', 'foobar.5', 'badDomain'),
            ('test', 'foo..bar.com', 'badDomain'),
            ('test', '.foo.bar.com', 'badDomain'),
            ('foo,bar', 'formencode.org', 'badUsername')]

        for username, domain, message_name in invalid_usernames:
            email = '@'.join(el for el in (username, domain) if el)
            error = self.validate(email)
            expected = self.message(message_name, username, domain)
            self.assertEqual(error, expected)

    def test_valid_email_addresses(self):
        valid_email_addresses = [
            # (email address, expected email address),
            (' [email protected] ', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('o*[email protected]', 'o*[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('[email protected]', '[email protected]'),
            ('foo{bar}@example.com', 'foo{bar}@example.com'),
            # examples from RFC 3696
            #   punting on the difficult and extremely uncommon ones
            #('"Abc\@def"@example.com', '"Abc\@def"@example.com'),
            #('"Fred Bloggs"@example.com', '"Fred Bloggs"@example.com'),
            #('"Joe\\Blow"@example.com', '"Joe\\Blow"@example.com'),
            #('"Abc@def"@example.com', '"Abc@def"@example.com'),
            ('customer/[email protected]',
                'customer/[email protected]'),
            ('[email protected]', '[email protected]'),
            ('!def!xyz%[email protected]', '!def!xyz%[email protected]'),
            ('*****@*****.**', '*****@*****.**')]

        for email, expected in valid_email_addresses:
            self.assertEqual(self.validate(email), expected)
Пример #8
0
 def send(self, recipient):
     if recipient in self.ignored_recipients:
         return
     if hasattr(self, "subject") and hasattr(self, "text"):
         if isinstance(recipient, basestring):
             try:
                 EmailValidator.to_python(recipient)
                 recipient.encode('ascii')
                 send_email(self.sender, recipient, self.subject, self.text,
                            html_body=self.html,
                            attachments=self.attachments)
             except (Invalid, UnicodeEncodeError):
                 log.debug("Invalid email %(email)s" % dict(email=recipient))
         else:
             Message.send(self, recipient=recipient)
     else:
         raise RuntimeError("The message must have a subject and a text to be sent.")
Пример #9
0
class TestEmail(unittest.TestCase):
    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid, e:
            return unicode(e)
Пример #10
0
class TestEmail(unittest.TestCase):

    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid, e:
            return unicode(e)
Пример #11
0
 def split_email_to(self, email_to):
     lines = email_to.splitlines()
     errors = []
     validator = Email()
     emails = []
     for line in lines:
         line = line.strip()
         if not line:
             continue
         try:
             emails.append(validator.to_python(line))
         except Invalid, e:
             errors.append(str(e))
class TestEmail(unittest.TestCase):
    def setUp(self):
        self.validator = Email()

    def validate(self, *args):
        try:
            return self.validator.to_python(*args)
        except Invalid as e:
            return unicode(e)

    def message(self, message_name, username, domain):
        email = '@'.join((username, domain))
        return self.validator.message(message_name,
                                      email,
                                      username=username,
                                      domain=domain)

    def test_invalid_email_addresses(self):
        invalid_usernames = [
            # (username, domain, message_name),
            ('foo\tbar', 'formencode.org', 'badUsername'),
            ('foo\nbar', 'formencode.org', 'badUsername'),
            ('test', '', 'noAt'),
            ('test', 'foobar', 'badDomain'),
            ('test', 'foobar.5', 'badDomain'),
            ('test', 'foo..bar.com', 'badDomain'),
            ('test', '.foo.bar.com', 'badDomain'),
            ('foo,bar', 'formencode.org', 'badUsername')
        ]

        for username, domain, message_name in invalid_usernames:
            email = '@'.join(el for el in (username, domain) if el)
            error = self.validate(email)
            expected = self.message(message_name, username, domain)
            self.assertEqual(error, expected)

    def test_valid_email_addresses(self):
        valid_email_addresses = [
            # (email address, expected email address),
            (' [email protected] ', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('o*[email protected]', 'o*[email protected]'),
            ('*****@*****.**', '*****@*****.**'),
            ('*****@*****.**', '*****@*****.**'),
            ('[email protected]', '[email protected]'),
            ('foo{bar}@example.com', 'foo{bar}@example.com'),
            # examples from RFC 3696
            #   punting on the difficult and extremely uncommon ones
            #('"Abc\@def"@example.com', '"Abc\@def"@example.com'),
            #('"Fred Bloggs"@example.com', '"Fred Bloggs"@example.com'),
            #('"Joe\\Blow"@example.com', '"Joe\\Blow"@example.com'),
            #('"Abc@def"@example.com', '"Abc@def"@example.com'),
            ('customer/[email protected]',
             'customer/[email protected]'),
            ('[email protected]', '[email protected]'),
            ('!def!xyz%[email protected]', '!def!xyz%[email protected]'),
            ('*****@*****.**', '*****@*****.**')
        ]

        for email, expected in valid_email_addresses:
            self.assertEqual(self.validate(email), expected)