title: Launchd and cron can't send emails from automated python scripts and fail with: smtplib.SMTPServerDisconnected: please run connect() first
body: <p>I want to set up automatic backups for my website and created a working python script for that. For scheduling first I tried <code>launchd</code> and then <code>cron</code> and for both of them I got the same result, which is the backup works, but the email notification fails.</p>
  <p>The script is 100% working as intended when launched from <code>Terminal</code> or <code>Sublime Text 3</code> and I'm using macOS Catalina Version 10.15.7.</p>
  <p>The error log for both <code>launchd</code> and <code>cron</code> is this:</p>
  <pre><code>Exception in thread Thread-1:
  Traceback (most recent call last):
    File &quot;/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py&quot;, line 926, in _bootstrap_inner
      self.run()
    File &quot;/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py&quot;, line 870, in run
      self._target(*self._args, **self._kwargs)
    File &quot;/usr/local/lib/python3.7/site-packages/fast_email/fast_email.py&quot;, line 95, in send
      server.ehlo()
    File &quot;/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py&quot;, line 440, in ehlo
      self.putcmd(self.ehlo_msg, name or self.local_hostname)
    File &quot;/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py&quot;, line 367, in putcmd
      self.send(str)
    File &quot;/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py&quot;, line 359, in send
      raise SMTPServerDisconnected('please run connect() first')
  smtplib.SMTPServerDisconnected: please run connect() first
  </code></pre>
  <p>The part of my fast_email package where the error happens (the <code>server.ehlo()</code> line):</p>
  <pre class="lang-py prettyprint-override"><code>def send(msg_data, d):
      server = smtplib.SMTP_SSL(d['EMAIL_SERVER'], d['EMAIL_PORT'])
      server.ehlo()
      server.login(d['EMAIL_SENDER'], d['EMAIL_PASSWORD'])
  
      message = MIMEMultipart(&quot;alternative&quot;)
      message[&quot;From&quot;] = d['EMAIL_SENDER']
  </code></pre>
  <p>If that may be relevant, I'm using threads (<code>threading.Thread</code>) in my fast_email package, because sometimes I want to send emails fast to people in my company.</p>
  <p>The problem is very frustrating and I've spent a full day on it alone, not to mention the other problems with launchd... Please, if you have any hints, I will be very thankful.</p>
  <p>P.S. my cron job line is:</p>
  <pre><code>0 9 * * * (/usr/local/bin/python3 my/script/pathname.py &gt; /Users/my_username/Desktop/cronjob.log 2&gt;&amp;1)
  </code></pre>
  <p>and I have</p>
  <pre><code>&lt;key&gt;AbandonProcessGroup&lt;/key&gt;
  &lt;true/&gt;
  </code></pre>
  <p>in my launchd .plist file.</p>