title: Is there a way to prevent SMTP Connection Timeout? smtplib, python
body: <p>I have a script that I need to run indefinitely.  The script is set to e-mail me confirmations of certain steps being completed on a daily basis.  I am trying to use smtplib for this.</p>
  
  <p>The initial connection is set up so that I will enter my login (written into the script) and a password using getpass at the very initiation of the script.  I do not want to leave my password written into the script or even reference by the script say in a config file.  Therefore, I want to enter the password at initiation and leave the smtp connection in place.</p>
  
  <p>Re-connecting to the smtp connection as required in the script would defeat the point of being able to step away from the script entirely and leave it running indefinitely.</p>
  
  <p>The example code that I am working with at the moment looks like this:</p>
  
  <pre><code>import smtplib
  import getpass
  
  smtpObj = smtplib.SMTP('smtp.gmail.com',587)
  smtpObj.ehlo()
  smtpObj.starttls()
  smtpObj.login('myemail@gmail.com',password = getpass.getpass('Enter Password: '))
  </code></pre>
  
  <p>Then I enter the password and the output is:</p>
  
  <pre><code>(235, b'2.7.0 Accepted')
  </code></pre>
  
  <p>So this all works fine.</p>
  
  <p>The problem is then that the script needs to pause for anywhere from a few minutes to potentially a few days depending on the time.  This is achieved using a while loop with time conditions until a certain time when the send function will be called:</p>
  
  <pre><code>smtpObj.sendmail('myemail@gmail.com','recipient@gmail.com','This is a test')
  </code></pre>
  
  <p>However, after a period of about 20/30 mins it seems (i.e. if the pause is sufficient).  Then the smptObj.sendmail call will fail due to a timeout error.</p>
  
  <p>The specific error is as follows:</p>
  
  <pre><code>SMTPSenderRefused: (451, b'4.4.2 Timeout - closing connection. l22sm2469172wre.52 - gsmtp', 'myemail@gmail.com')
  </code></pre>
  
  <p>I have so far tried the following:</p>
  
  <p>Instantiating the connection object with the following timeout parameterisation:</p>
  
  <pre><code>smtpObj = smtplib.SMTP('smtp.gmail.com',587,timeout=None)
  smtpObj = smtplib.SMTP('smtp.gmail.com',587,timeout=86400)
  </code></pre>
  
  <p>Neither of these seem to supress the 'timeout' of the connection (i.e. the same problem persists).</p>
  
  <p>I have also tried this solution approach suggested in this post:</p>
  
  <p><a href="https://stackoverflow.com/questions/3978115/how-can-i-hold-a-smtp-connection-open-with-smtplib-and-python">How can I hold a SMTP connection open with smtplib and Python?
  </a></p>
  
  <p>However, this has not worked either!</p>
  
  <p>I do want to try and avoid the solution where I would have to re-connect each time when I wanted to send the e-mail, because I only want to enter the password for the connection the once manually, rather than writing it into the script either directly or indirectly.</p>
  
  <p>There surely is a way to deal with the timeout issue!  If anyone can help here, then please let me know!  Though, if you think that the more 'obvious' solution of re-connecting just before the script needs to send an e-mail is the better way to go, then please let me know.</p>
  
  <p>Thank you!...</p>