Home > Blockchain >  Django email: was connection to SMTP server established?
Django email: was connection to SMTP server established?

Time:10-27

What I try to check if the connection was already opened, and if not, open it. I do

connection = mail.get_connection()

connection.open()

to get the connection and open it. Then I send the emails and close it under some condition. Is there a way to check if the connection has already been opened?

CodePudding user response:

connection = mail.get_connection()

After this line, connection represents an settings.EmailBackend object

Check this

And now your connection object has an attribute connection which represents connection is already open or not

check this and this

So In general terms, logic will be

if connection.connection :
   # already open
else:
   # not open

Also I will add one point,

When you do connection.open(), django itself checks whether the connection is already open or not, and do things accordingly.

See this from django's repo

  • Related