So recently, in MDN, we changed the setting
WELCOME_EMAIL_FROM. Seems harmless right? Wrong, it failed horribly in runtime and we didn't notice until it was in production. Here's the traceback:
SMTPSenderRefused: (552, b"5.1.7 The sender's address was syntactically invalid.\n5.1.7 see : http://support.socketlabs.com/kb/84 for more information.", '=?utf-8?q?Janet?=') (8 additional frame(s) were not displayed) ... File "newrelic/api/function_trace.py", line 151, in literal_wrapper return wrapped(*args, **kwargs) File "django/core/mail/message.py", line 291, in send return self.get_connection(fail_silently).send_messages([self]) File "django/core/mail/backends/smtp.py", line 110, in send_messages sent = self._send(message) File "django/core/mail/backends/smtp.py", line 126, in _send self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n')) File "python3.8/smtplib.py", line 871, in sendmail raise SMTPSenderRefused(code, resp, from_addr) SMTPSenderRefused: (552, b"5.1.7 The sender's address was syntactically invalid.\n5.1.7 see : http://support.socketlabs.com/kb/84 for more information.", '=?utf-8?q?Janet?=')
So, to prevent this from happening every again we're putting this check in:
from email.utils import parseaddr WELCOME_EMAIL_FROM = config("WELCOME_EMAIL_FROM", ...) # If this fails, SMTP will probably also fail. assert parseaddr(WELCOME_EMAIL_FROM).count('@') == 1, parseaddr(WELCOME_EMAIL_FROM)
You could go to town even more on this. Perhaps use the email validator within
django but for now I'd call that overkill. This is just a decent check before anything gets a chance to go wrong.