473,569 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Email Verification

1 New Member
what else can done to reach every domain

Expand|Select|Wrap|Line Numbers
  1. @app.route('/<string:email>',methods=['GET','POST'])
  2. def post(email):
  3.     blocked_keywords = ["spamhaus",
  4.             "proofpoint",
  5.             "cloudmark",
  6.             "banned",
  7.             "blacklisted",
  8.             "blocked",
  9.             "block list",
  10.             "denied"]
  11.  
  12.  
  13.  
  14.     proxy = {
  15.         'socks4': socks.SOCKS4,
  16.         'socks5': socks.SOCKS5,
  17.         'http': socks.HTTP # does not guareentee it will work with HTTP
  18.     }
  19.  
  20.     class UnknownProxyError(Exception):
  21.         def __init__(self, proxy_type):
  22.             self.msg = f"The proxy type {proxy_type} is not known\n Try one of socks4, socks5 or http"
  23.  
  24.     class EmailFormatError(Exception):
  25.  
  26.         def __init__(self, msg):
  27.             self.msg = msg
  28.  
  29.     class SMTPRecepientException(Exception): # don't cover
  30.  
  31.         def __init__(self, code, response):
  32.             self.code = code
  33.             self.response = response
  34.  
  35.  
  36.     ####
  37.     # SMTP RCPT error handlers
  38.     ####
  39.     def handle_550(response):
  40.         if any([keyword.encode() in response for keyword in blocked_keywords]):
  41.             return dict(message="Blocked by mail server- couldn't verify", deliverable=False, host_exists=True)
  42.         else:
  43.             return dict(deliverable=False, host_exists=True)
  44.  
  45.  
  46.  
  47.  
  48.     handle_error = {
  49.         # 250 and 251 are not errors
  50.         550: handle_550,
  51.         551: lambda _: dict(deliverable=False, host_exists=True),
  52.         552: lambda _: dict(deliverable=True, host_exists=True, full_inbox=True),
  53.         553: lambda _: dict(deliverable=False, host_exists=True),
  54.         450: lambda _: dict(deliverable=False, host_exists=True),
  55.         451: lambda _: dict(deliverable=False, message="Local error processing, try again later."),
  56.         452: lambda _: dict(deliverable=True, full_inbox=True),
  57.         # Syntax errors
  58.         # 500 (command not recognised)
  59.         # 501 (parameter/argument not recognised)
  60.         # 503 (bad command sequence)
  61.         521: lambda _: dict(deliverable=False, host_exists=False),
  62.         421: lambda _: dict(deliverable=False, host_exists=True, message="Service not available, try again later."),
  63.         441: lambda _: dict(deliverable=True, full_inbox=True, host_exists=True)
  64.     }
  65.  
  66.     handle_unrecognised = lambda a: dict(message=f"Unrecognised error: {a}", deliverable=False)
  67.  
  68.  
  69.     # create a namedtuple to hold the email address
  70.     Address = namedtuple("Address", ["name", "addr", "username", "domain"])
  71.  
  72.     class e_verifier:
  73.  
  74.         def __init__(self,
  75.                     source_addr,
  76.                     proxy_type = None,
  77.                     proxy_addr = None,
  78.                     proxy_port = None,
  79.                     proxy_username = None ,
  80.                     proxy_password = None):
  81.  
  82.  
  83.             if proxy_type:
  84.                 try:
  85.                     self.proxy_type = proxy[proxy_type.lower()]
  86.                 except KeyError as e:
  87.                     raise UnknownProxyError(proxy_type)
  88.             else:
  89.                 self.proxy_type = None
  90.             self.source_addr = source_addr
  91.             self.proxy_addr = proxy_addr
  92.             self.proxy_port = proxy_port
  93.             self.proxy_username = proxy_username
  94.             self.proxy_password = proxy_password
  95.  
  96.         def _parse_address(self, email) -> Address:
  97.  
  98.             name, addr = parseaddr(email)
  99.             if not addr:
  100.                 raise EmailFormatError(f"email does not contain address: {email}")
  101.             try:
  102.                 domain = addr.split('@')[-1]
  103.                 username = addr.split('@')[:-1][0]
  104.             except IndexError:
  105.                 raise EmailFormatError(f"address provided is invalid: {email}")
  106.             return Address(name, addr, username, domain)
  107.  
  108.         def _random_email(self, domain):
  109.  
  110.             return f'{binascii.hexlify(os.urandom(20)).decode()}@{domain}'
  111.  
  112.         def _can_deliver(self,
  113.                         exchange : str,
  114.                         address : str):
  115.  
  116.             host_exists = False
  117.             with SMTP(exchange[1],
  118.                     proxy_type=self.proxy_type,
  119.                     proxy_addr=self.proxy_addr,
  120.                     proxy_port=self.proxy_port,
  121.                     proxy_username=self.proxy_username,
  122.                     proxy_password=self.proxy_password) as smtp:
  123.                 host_exists = True
  124.                 smtp.helo()
  125.                 smtp.mail(self.source_addr)
  126.                 test_resp = smtp.rcpt(address.addr)
  127.                 catch_all_resp = smtp.rcpt(self._random_email(address.domain))
  128.                 if test_resp[0] == 250:
  129.                     deliverable = True
  130.                     if catch_all_resp[0] == 250:
  131.                         catch_all = True
  132.                     else:
  133.                         catch_all = False
  134.                 elif test_resp[0] >= 400:
  135.                     raise SMTPRecepientException(*test_resp)
  136.             return host_exists, deliverable, catch_all
  137.  
  138.         def verify(self, email):
  139.  
  140.             lookup = {
  141.                 'address': None,
  142.                 'valid_format': False,
  143.                 'deliverable': False,
  144.                 'full_inbox': False,
  145.                 'host_exists': False,
  146.                 'catch_all': False,
  147.             }
  148.             try:
  149.                 lookup['address'] = self._parse_address(email)
  150.                 lookup['valid_format'] = True
  151.             except EmailFormatError:
  152.                 lookup['address'] = f"{email}"
  153.                 return lookup
  154.  
  155.             # look for mx record and create a list of mail exchanges
  156.             try:
  157.                 mx_record = resolver.query(lookup['address'].domain, 'MX')
  158.                 mail_exchangers = [exchange.to_text().split() for exchange in mx_record]
  159.                 lookup['host_exists'] = True
  160.             except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
  161.                 lookup['host_exists'] = False
  162.                 return lookup
  163.  
  164.             for exchange in mail_exchangers:
  165.                 try:
  166.                     host_exists, deliverable, catch_all = self._can_deliver(exchange, lookup['address'])
  167.                     if deliverable:
  168.                         lookup['host_exists'] = host_exists
  169.                         lookup['deliverable'] = deliverable
  170.                         lookup['catch_all'] = catch_all
  171.                         break
  172.                 except SMTPRecepientException as err:
  173.                     # Error handlers return a dict that is then merged with 'lookup'
  174.                     kwargs = handle_error.get(err.code, handle_unrecognised)(err.response)
  175.                     # This expression merges the lookup dict with kwargs
  176.                     lookup = {**lookup, **kwargs}
  177.  
  178.                 except smtplib.SMTPServerDisconnected as err:
  179.                     lookup['message'] = "Internal Error"
  180.                 except smtplib.SMTPConnectError as err:
  181.  
  182.                     lookup['message'] = "Internal Error. Maybe blacklisted"
  183.  
  184.             return lookup
  185.  
  186.     v = e_verifier(source_addr='user@example.com')
  187.     email = email
  188.     l = v.verify(email)
  189.     return l  
  190.  
  191. if __name__ == "__main__":
  192.     app.run(debug=True)
  193.  
  194.  
Jul 26 '21 #1
0 4135

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3004
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email User1@here.whatever and User4@here.whatever but if "Loaded" is chosen it needs to email User2@here.whatever and User3@here.whatever, etc, etc... ...
0
1366
by: email verification | last post by:
Is there a code snippet to check if an email address is a valid account or not? When someone subscribes to our newsletter we would like to verify that it's valid and if not we can notify them
7
2873
by: SOR | last post by:
Although this currently defeats the spam bots in some respects - isnt it just a mater of time before the spammers figure out a way to verify a signup via email using rotating disposable email addresses or whatever . And if so , Would it not be a good idea to separate the url and signup verification code in the welcome email *now* rather than...
2
1640
by: toedipper | last post by:
Hello, php and mysql I am looking to start a newsletter on my site. I imagine I will have a text box that users enter their email address into and a button that says 'subscribe' To stop people entering other peeps email address I will need some
0
2179
by: pwilliams | last post by:
NCOALink Change of Address Verification Each year over 40 million Americans change their mailing addresses. This change is equivalent to every person in California deciding to change addresses and not tell you that they have moved. This uncertainty results in wasted spending on undeliverable address mail, returned mail and processing fees...
1
1276
by: Jagadesh | last post by:
Hello for all, how to create an email verification... Not an syntax verification,. whether the email id is availble for the domain or not.. I will gave some link.. check that ...there's an code.. but its completely functioning ... but link available as demo is functioning good// check this link how its doing /... Plz help me... Thanks...
3
2158
by: Jano | last post by:
Hi - Happy New Year! I have a web-site which accepted paypal payment for membership. No-one's buying so I want to make it free. The page which inputs the member details into the database needs verification, and I want to bypass the verification, but I can't figure it out. Can anyone help. - I have pasted the script below. Many thanks, ...
4
2045
by: shalinikonatam | last post by:
Can any one help me? how to verify weather the Email actually exist or not? Thanks in Advance.
4
1737
by: sangam56 | last post by:
I have used cookieless mode of ASPState sql server session mode. I have used the CreateUserWizard to register user in the web site. And users must click the email verification link before being activated. This all works well. But i need to retrieve the whole session when user opens the site by clicking the email verification link. I can not use...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.