I am using Python and smtplib to send email via Amazon SES. How do I catch specific sending errors?
For example, Amazon SES may tell me "this address is blacklisted" or "you've exceeded your rate" or "you've exceeded your volume quota", and I want to act on those messages.
I have a snippet which catches blacklisting (I think), as follows. I don't really know how to debug these things, since the exceptions will only show up in heavy-duty environments, and if I trigger the exceptions then I'm worried Amazon will ding my quota.
try: msg = EmailMultiAlternatives(subject, plain, from_address, [to_address]) msg.attach_alternative(html, "text/html") msg.send() except smtplib.SMTPResponseException as e: error_code,error_msg = e.smtp_code, e.smtp_error if error_code==554 and error_msg=='Message rejected: Address blacklisted.': # do appropriate action for blacklisting else: # do appropriate action for throttling else: # log any other SMTP exceptions