Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
email address input
#1
making an app to send reminder emails. i want to set up a way to make sure that in the email entry widget the input should always be in a format of [email protected] and any input that does not match that pattern then should be rejected. Some kind of reg ex wizardry really


EDIT:
i found out about the email_validator module and i installed it through pip install. however when i try to import it i get a ModuleNotFoundError. usually with that i can hover over the module name and click install to make it work right however i get a rename reference option instead. can anyone help me fix this issue please?
Reply
#2
Hi Friend!

it sounds like we all may profit from a bit more details - e.g. what is the exact output of error ModuleNotFound, which module it complains about, where do you think you have installed what you think you have installed

also as you mentioned, you can allways fallback to regex check. definitely adding some clumsy dependency just for testing string to conform to email format may be a bit overkill.
Reply
#3
(Jun-26-2024, 05:13 AM)rodiongork Wrote: Hi Friend!

it sounds like we all may profit from a bit more details - e.g. what is the exact output of error ModuleNotFound, which module it complains about, where do you think you have installed what you think you have installed

also as you mentioned, you can allways fallback to regex check. definitely adding some clumsy dependency just for testing string to conform to email format may be a bit overkill.

hey buddy. your timely response is much appreciated. basically the output just says the following



Error:
ModuleNotFoundError: No module named 'email_validator'
i have already ran pip install email_validator in my command prompt a couple of times and it looks like the package is installed however it when i try to import it i get the above error code. obviously i need to use this package instead of regex as the package itself also checks the spelling of the domain name which is something that cant happen by using regex
Reply
#4
Make your own re expression! State explicitly what you allow, or state explicitly what you disallow. Here I've gone with disallow.

Email format is governed by several RFC documents like RFC 5322. What exactly is disallowed in the email address is not quite clear to me. Exclude all the ugly bits like this:

This, ^, at the beginning in [ ] means NOT, so NOT any of these following symbols. If you want to exclude the letter a, put that in the square brackets too!

[^=+!#$%&~ ?,<>;:\/()[\]{}]
Put anything you want excluded in the [ ] but after the ^. If the character is a special character in re, you will need to escape it, like ] must be escaped: \] or re will complain.

Thus e has the format (if no banned stuff)@(if no banned stuff) then the email is good.

import re

email = input('Enter what you think is a valid email ... ')
email1 = '[email protected]'
email2 = 'King(Charles)[email protected]'  # no match
email3 = 'King-[Charles][email protected]' # no match
email4 = 'King/Charles/[email protected]' # no match
email5 = 'King\Charles\[email protected]' # no match
email6 = 'King_Charles_III@royal?.email.com.co.uk' # no match

"""
Maybe some of the things in e are not officially banned, but banned by me here!
As a whole, the email address cannot be more that 254 characters long I read.
"""

e = re.compile(r'([^=+!#$%&~ ?,<>;:\/()[\]{}*])@([^=+!#$%&~ ?,<>;:\/()[\]{}*]\Z)')
if not e.match(email):
    print(f'This email: {email} contains banned characters, try again ... ')
Example:

Output:
email = input('Enter what you think is a valid email ... ') Enter what you think is a valid email ... King*[email protected] if not e.match(email): print(f'This email: {email} is the wrong format, try again ... ') This email: King*[email protected] is the wrong format, try again ...
Verifying the email is a different kettle of fish! I made some Python to send emails to a list of customers for the girlfriend.

I've been trying with openssl s_client to verify the list of emails, but I have not yet succeeded! At the moment, all I can do is send an email and look for "Email not sent", then take that email off the list

If you have a clue about that, please let me know!

But if the email is just for login, it does not actually need to really exist!
Reply
#5
I got the email-validator working from here
https://stackabuse.com/validate-email-ad...validator/

I'm not going to show my email but, real emails do work

from email_validator import validate_email, EmailNotValidError

testemails = ['[email protected]', '[email protected]', 'user@[email protected]']

for index, email in enumerate(testemails):
    try:
        validate_email(email)
        print(f'email {index+1}: Valid email')
    except EmailNotValidError as error:
        print(f'email {index+1}: {error}')
Output
Output:
email 1: The domain name site.org does not exist. email 2: Valid email email 3: The part after the @-sign contains invalid characters: '@'.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
Doing the regex right is difficult. Much corner cases resulting in a longer regex.
This problem has been solved many times over and over.

One possible shortcut is the use of Pydantic.
It allows you to create models and those models are verified.


# py -m pip install pydantic[email]
from pydantic import BaseModel, EmailStr, ValidationError


class User(BaseModel):
    email: EmailStr



def test():
    while True:
        user_input = input("Please enter an email address: ")
        
        try:
            user = User(email=user_input)
        except ValidationError:
            print(f"[❌] Email '{user_input}' is invalid")
        else:
            print(f"[✓] Email '{user_input}' is valid")


if __name__ == "__main__":
    test()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in entering address of a file in input akbarza 0 779 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Extracing unique email address from a folder of emails jehoshua 6 3,040 Oct-14-2020, 12:43 AM
Last Post: jehoshua
  Grabing email from a class with input from name Nickd12 2 1,923 Oct-13-2020, 06:22 PM
Last Post: Nickd12
  Slicing and printing two halfs of an email address (Udemy) Drone4four 5 2,994 Aug-26-2019, 09:01 AM
Last Post: wavic
  An email with inline jpg cannot be read by all email clients fpiraneo 4 4,236 Feb-25-2018, 07:17 PM
Last Post: fpiraneo
  Email - Send email using Windows Live Mail cyberzen 2 6,083 Apr-13-2017, 03:14 AM
Last Post: cyberzen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020