Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking validity of email in django/python [duplicate]

I have written a function for adding emails to newsletter base. Until I've added checking validity of sent email it was working flawlessly. Now each time I'm getting "Wrong email" in return. Can anybody see any errors here ? The regex used is :

\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b and it is 100% valid (http://gskinner.com/RegExr/), but I may be using it wrong, or it may be some logic error :

def newsletter_add(request):     if request.method == "POST":            try:             e = NewsletterEmails.objects.get(email = request.POST['email'])             message = _(u"Email is already added.")             type = "error"         except NewsletterEmails.DoesNotExist:             if validateEmail(request.POST['email']):                 try:                     e = NewsletterEmails(email = request.POST['email'])                 except DoesNotExist:                     pass                 message = _(u"Email added.")                 type = "success"                 e.save()             else:                 message = _(u"Wrong email")                 type = "error"  import re  def validateEmail(email):     if len(email) > 6:         if re.match('\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b', email) != None:             return 1     return 0 
like image 479
muntu Avatar asked Jul 10 '10 03:07

muntu


People also ask

How can I check if a Django email is valid?

To check the domain mx and verify email exists you can install the pyDNS package along with validate_email. Returns True if the email exist in real world else False.

How do I receive emails in Django?

To receive emails in Django, it is better to use the django-mailbox development library if you need to import messages from local mailboxes, POP3, IMAP, or directly receive messages from Postfix or Exim4. While using Django-mailbox, mailbox functions as a message queue that is being gradually processed.


1 Answers

from django.core.exceptions import ValidationError from django.core.validators import validate_email  value = "[email protected]"  try:     validate_email(value) except ValidationError as e:     print("bad email, details:", e) else:     print("good email") 
like image 82
oblalex Avatar answered Oct 02 '22 04:10

oblalex