1

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.

4

1 回答 1

0

尝试仅在 clean_recipients 中清除收件人电子邮件格式。有片段如何检查电子邮件。http://djangosnippets.org/snippets/1093/。如果电子邮件格式不匹配,则引发验证错误。

创建电子邮件并从表单的 clean 方法发送(如果您需要显示发送错误。您需要在发送前检查表单错误。)或从视图发送。

PS。以这种方式获取您的数据 - self.cleaned_data 而不是 self.data。

于 2011-12-25T12:49:53.143 回答