Email Exception Handling in Python



Python


In this article, we show how to handle email exception errors in Python.

When sending emails in Python, there are a number of things that you must do, including connecting to the email host such gmail.

You then need to log in to an actual email account such as "abc@gmail.com" with a password of "abc123"

There are just a number of things that can go wrong during the sending email process.

You may not be able to connect to the email hosting provider.

There may be an incorrect username-password combination.

In other words, there are a number of errors that can occur in the process.

If you're running this in a production environment, you don't want the machine-generated error to be shown, such as a 535 error, which a user may not understand. You want to put a customized message such as, "The username and password combination does not match"

So, we'll show how to handle email exception errors in a program in Python.

So, before, we do that, let's go over a simple Python script that sends an email message.

The code below sends a simple email message, "Hey"



So, this is the generic way, we can send an email, "Hey"

Now, we'll create the code so that email exceptions are handled.


Generic Email Exception Handling

So, the first thing we'll go over is how to handle all exceptions generically instead of each specific error.

What is meant by this is that if any error occurs at all, whether it's a problem connecting to the email service provider, incorrect username/password combination, or any other error, we will handle them all the same. So it's generic.

So, to handle all email errors generically, we can use the following code below.



So, let's go over what we've done in this code.

Now, because we're going to handle exceptions, we import SMTPException.

What this class, SMTPException, does is it is called if an exception error occurs anywhere in our code.

We use try-except statements to try a block of code. If this block of code does not work, what exists after except will be executed, which is our error message, "An error occurred"

So, this is pretty much a catch-all error exception handling for emails.

So, let's now go into how to handle a specific email error.


Specific Email Exception Error Handling

So, let's say we want to go a little more specifically into handling errors.

Let's say we want to handle an error if the incorrect username-password combination was entered, which is a SMTPAuthenticationError.

So, the following code below will specifically handle an authentification error in the event that a wrong username-password combination was entered.



So, now in this code, we import the generic SMTPException class to handle any generic error that occurs, as well as the SMTPAuthenticationError class which handles an error with username-password combinations.

In our code, we have a try statement. This will execute if everything connects properly. In this case, the email will be sent.

If there is an error with the user authentication, then the line, "The username and/or password you entered is incorrect", will be output.

If there is any other type of error other than user authentication, the line, "An error occurred", will be output.

So, hopefully, this gives you a very good idea of how email exception error handling can be dealt with.

And you don't have to simply print out a statement with exception handling. We've printed out statements above, but you literally can do anything. You can run the whole script all over if you have a backup email on file. For example, the user submitted 2 emails. The first was rejected, and so you run the whole script over with another email address. You can redirect the user to another page. The options are limitless.

And there are plenty of other specific errors that you could write scripts for, including SMTPConnectError, an error that occurs during establishment of a connection with the server.

There is SMTPServerDisconnected error, which is an error that is raised when the server unexpectedly disconnecxts, or when an attempt is made to use the SMTP instance before connecting it to a server.

To see all the exceptions associated with SMTP, see the official documentation of SMTP exception handling on python.org at the following link: https://docs.python.org/3/library/smtplib.html.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...