I am developing an application with Django. In forms.py, where the classes for my forms are stored, I have written a clean function to verify that all the emails typed into a textbox adhere to the proper format (person@site.com).
In this clean function, I build the email message with an EmailMessage object:
def clean_recipients(self):
rec = self.data['recipients'].split(",")
recList = []
for recipient in rec:
reci = str.strip(str(recipient))
recList.append(reci)
message = (self.data['subject'], self.data['message'], 'hi@world.com', recList)
mail = EmailMessage(self.data['subject'], self.data['message'], 'from@somebody.com', ['email_list@mysite.org'], recList)
try:
mail.send(fail_silently=False)
except Exception:
raise forms.ValidationError('Please check inputted emails for validity.')
return self.data['recipients']
However, the exception 'Please check inputted emails for validity.' is never raised on the form regardless of what I input into the textbox. If I input random characters into the textbox, simply no message is sent.
What is the proper way to catch if the email was not sent properly?
Thank you.