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
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With