-2

I want to know how to write 'if' statement while checking email. If it is returning True and email is valid I want to write it to the text file + comparing if the email is existing already duplicated in the save output file.

from validate_email import validate_email
import os

def email_validator():
    global reqs, _lock, success, fails

    with open(os.path.join("./emails.txt"), "r") as f:
        for line in f:
            line.strip()
            print("checking email: "+ line)
            is_valid = validate_email(
                email_address=line,
                check_format=True,
                check_blacklist=False,
                check_dns=True,
                dns_timeout=10,
                check_smtp=True,
                smtp_timeout=10,
                smtp_helo_host='my.host.name',
                smtp_from_address='my@from.addr.ess',
                smtp_skip_tls=False,
                smtp_tls_context=None,
                smtp_debug=False
            )
            

            if validate_email.__code__ == 200:
                print(f'Email {is_valid} is valid : {success} ')
                success += 1

                with open("./validated_emails.txt", 'r+') as f:
                    valid_emails = f.read()
                    if line not in valid_emails:
                        f.write(line.strip() + '\n')
                    else:
                        print("Not valid Email!")
                        fails +=1                                              
            elif validate_email == False:
                fails += 1
                continue
nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12
Pawel
  • 1
  • 4
  • you clobber something important here `validate_email == False`. – JonSG Jan 19 '23 at 19:55
  • @JonSG I dont speak good English excuse me I dont understand you – Pawel Jan 19 '23 at 19:56
  • @Pawel by "clobber" they mean that you're overwriting something important. But I don't think that's actually happening. The actual issue is that `validate_email` is a function, but you are comparing it to a boolean. You have to compare the _result_ of a _function **call**_ in order to be able to compare it that way. Such as, `elif is_valid == False` - we already know that `is_valid` is the result of calling `validate_email`. – Random Davis Jan 19 '23 at 19:58
  • for example this email is exist and it return FALSE why? checking email: Jason@theJasonFleagle.com – Pawel Jan 19 '23 at 19:58
  • @Pawel I edited my previous comment to explain what I think the issue is. – Random Davis Jan 19 '23 at 19:59
  • @RandomDavis but I dont know how – Pawel Jan 19 '23 at 19:59
  • `validate_email` is a function. `validate_email.__code__` is fetching the compiled code for the Python function. What you need to look at is the value `validate_email` returned, which is `is_valid`. So, `if is_valid:` ... then `else:`. – Tim Roberts Jan 19 '23 at 20:00
  • @RandomDavis I also tried "ELSE:" but still it return FALSE even if the email exist – Pawel Jan 19 '23 at 20:01
  • @TimRoberts if I write "is_valid() == True: I get this: 'bool' object is not callable – Pawel Jan 19 '23 at 20:02
  • Because that's not what I wrote, is it? – Tim Roberts Jan 19 '23 at 20:05
  • @TimRoberts Tim if I write 'if is_valid:" then all emails return False something is not working in that module – Pawel Jan 19 '23 at 20:06
  • `line.strip()` does not modify the line in place, because you cannot modify a string. You need `line = line.strip()`. Did you not notice the output was double-spaced? – Tim Roberts Jan 19 '23 at 20:11
  • @TimRoberts Tim even if I write this as they suggesting : is_valid = validate_email( email_address="contact@gmail.com", check_format=True, check_blacklist=False, check_dns=True, dns_timeout=10, check_smtp=True, smtp_timeout=10, smtp_helo_host='my.host.name', smtp_from_address='my@from.addr.ess', smtp_skip_tls=False, smtp_tls_context=None, smtp_debug=False ) print(is_valid) IT STILL keep returnign the email is not valid and print FALSE – Pawel Jan 19 '23 at 20:16
  • @TimRoberts any help for this please? The answer might be correct for my issue but all emails saddenly return FALSE :-( – Pawel Jan 19 '23 at 20:49
  • Please update your question to show your current code. – Tim Roberts Jan 20 '23 at 06:57

2 Answers2

1

A couple of important misunderstandings to clear up first:

  1. validate_email is a function. To get the result of a function, we call it with () following the function name. You already do this on line 11 with is_valid = validate_email(*args).
    The variable is_valid is now storing the result of validate_email() This is probably either True, False or None. I couldn't figure out exactly what module you're using for validation, as the validate_email module I installed from pip only has 5 parameters in the function definition.

  2. On line 27, you have validate_email.__code__ == 200. This will ALWAYS be False.
    __code__ is an attribute of of the validate_email function and represents the literal code of the function as a code object. It does not represent the last return value of the function.

  3. Likewise, on line 38, you have if validate_email == False. This will also ALWAYS be a false comparision. validate_email is a object, and will never == False.

Here's my take on correcting your code, but without being able to verify the exact validate_email module you are using, it might not be correct:

from validate_email import validate_email
import os

def email_validator():
    global reqs, _lock, success, fails

    with open(os.path.join("./emails.txt"), "r") as f:
        for line in f:
            line.strip()
            print("checking email: "+ line)
            is_valid = validate_email(
                email_address=line,
                check_format=True,
                check_blacklist=False,
                check_dns=True,
                dns_timeout=10,
                check_smtp=True,
                smtp_timeout=10,
                smtp_helo_host='my.host.name',
                smtp_from_address='my@from.addr.ess',
                smtp_skip_tls=False,
                smtp_tls_context=None,
                smtp_debug=False
            )
            

            if is_valid:
                success += 1
                print(f'Email {is_valid} is valid : {success} ')
                with open("./validated_emails.txt", 'r+') as f:
                    valid_emails = f.read()
                    if line not in valid_emails:
                        f.write(line.strip() + '\n')
                                                         
            else:
                fails += 1
                print(f"{line} is not a valid email : {fails}")
                # continue  # This is not needed when we're at the end of the loop

nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12
  • thx but all emails return always FALSE it was working like 30min ago do you think I need to run proxies? I was able to test for about 1h now Im not able to do anything always shows FALSE even the real email source: https://gitea.ksol.io/karolyi/py3-validate-email – Pawel Jan 19 '23 at 20:28
  • It looks like the py3_validate_email module should be writing the reason for validation errors to a log file, so checking that log file for more information is probably the next step. It is possible that your connection is being blocked when running the smtp_check or dns_check. You could try setting either or both of those parameters to False to skip those checks and determine if that's where the problem exists. – nigh_anxiety Jan 19 '23 at 20:50
  • @night_anxiety I let only check_dns=True, and still unable to validate emails I am really tired this should work the nwhy it is not? Why always those modules they offer are buggy? I work hard my own job and 100% they should also + I wanted to ask new question and they just banned me from asking is this a way how Microsoft work? They should help people as they advertise and NOT SLOWING them and FRUSTRATING THEM DOWN! – Pawel Jan 19 '23 at 20:55
  • I don't understand how Microsoft comes into this. You may want to read the FAQ.md file on the py3_validate_email repository, which states `This module is a tool; every tool can become a weapon if not used properly. In my case, I use this module to check email address validity at registration time, so not thousands at once. Doing so might make you (your IP) end up in one of the aforementioned blocklists, as providers will detect you as a possible spammer. In short, I would advise against your use case.` – nigh_anxiety Jan 19 '23 at 21:11
  • The FAQ also explains that it may give false positives in some cases : `Other SMTP servers accept emails even for nonexistent recipient addresses and forward them to a different server which will create a bounce message in a second step. This is the case for many email domains hosted at Microsoft.` – nigh_anxiety Jan 19 '23 at 21:11
  • If you're trying to check thousands of emails at once, I would limit your check to regex only, and possibly the DNS check. Running SMTP validation is going to get you blocked by email servers who want to avoid spambots and DDOS attacks – nigh_anxiety Jan 19 '23 at 21:13
-2

The problem located so while you are under VPN this module doesn't work, so I do not recommend to use this CRAP email check because I'm working on my own checker and I will check 1mil list daily so to not use proxies or VPN is just stupid!

Pawel
  • 1
  • 4