red-mail
red-mail copied to clipboard
ENH: Displaying status_code of the email status would be good for better error handling
I tried to find this feature but I didn't find it. so I am trying to get status_code
of the email's status like "If the email was successfully sent or failed" and "If failed then How email failed", I mean what reason effected the email failure. If the console is showing error like -
smtplib.SMTPRecipientsRefused: {'user': (553, b'5.1.3 The recipient address
is not a valid RFC-5321 address. Learn\n5.1.3 more at\n5.1.3 https://support.google.com/mail/answer/6596 ik12-20020a170902ab0cm2094481plb.177 - gsmtp')}
or
socket.gaierror: [Errno 11001] getaddrinfo failed
so, A developer will want to show or pass the appropriate message to the user in frontend on "What went wrong" or "What causes the email failure" ? but in this case It will only be seen as "something went wrong" (No status code or a clean message) to pass forward.
For Example -
email = EmailSender(
host="smtp.gmail.com", port=587,
username="xyz123", password="xyz123"
)
email.send(
sender="[email protected],
receivers=["[email protected]"],
subject="An example email",
)
and If I try to print the EmailSender class calling variable
print(email)
. Then it prints Class object Binary
.
so It would be better to show more information about email just sent.
Red Mail should not do any silencing on the error: whatever the smtplib
raises will be passed through Red Mail to the user of the library. And the user can process the exception however needed.
Do you mean you would like to have better errors than what smtplib
produces? Perhaps a valid point but it should be done with extreme care so that no information is lost. Error silencing for the sake of a status code is something I'm very against.
Furthermore, your example produces quite an intuitive exception though (you are missing a quote):
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials q3-20020a0565123a8300b00498fc3d4d15sm1004920lfu.190 - gsmtp')
It's not just about 'status_code', it can be done by passing status_code with appropriate message. But that's all upto you.