title: Weird problem: sending email from Python fails, but only after(!) some MySQL interaction
body: <p>I'm running into the weirdest of errors. Maybe someone can help?</p>
  <p>I have some (very simple) Python code (see below) which does two things:</p>
  <ol>
  <li>It connects to MySQL (on localhost), does a query, prints the results, then disconnects from MySQL.</li>
  <li>It sends an email through gmail.</li>
  </ol>
  <p>The MySQL portion of the code runs fine.</p>
  <p>The weirdness:</p>
  <ul>
  <li>If I remove the MySQL portion of the code, the email-sending portion of the code works just fine.</li>
  <li>If I do not remove the MySQL portion of the code, the email-sending portion of the code raises the following error:</li>
  </ul>
  <blockquote>
  <p>[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1131)</p>
  </blockquote>
  <p>I'm running Python 3.8 on Linux (Ubuntu 20.04-LTS--kernel version 5.4.0-113-generic)</p>
  <p>The MySQL package I installed was mysql-connector-python</p>
  <p>Note that I'm using a 'gmail app password' (not the regular gmail password) to login to gmail.</p>
  <p>My code (all logins and passwords removed)</p>
  <pre><code>import mysql.connector
  #Connect to MySQL
  try:
    mydb = mysql.connector.connect(
      host = &quot;localhost&quot;,
      user = &quot;db_user&quot;,
      password = &quot;db_password&quot;,
      database = &quot;db_database&quot;)
  except Exception as err:
    print(&quot;\n&quot;, &quot;Failed to connect to database&quot;, &quot;\n&quot;, err)
    exit(1)
  print(&quot;Connected...&quot;)
  
  #Do the query
  cur = mydb.cursor()
  cur.execute(&quot;SHOW DATABASES&quot;)
  for row in cur:
      print(row[0])
  
  #Disconnect from MySQL
  try:
    mydb.disconnect()
  except Exception as err:
    print(&quot;\n&quot;, &quot;Error closing database connection&quot;, &quot;\n&quot;, err)
    exit(1)
  print(&quot;Disconnected...&quot;)
  
  import smtplib, ssl 
  smtp_server = &quot;smtp.gmail.com&quot;
  port = 587 
  gmail_login = &quot;gmail_login&quot;
  gmail_app_password = &quot;gmail_app_password&quot;
  gmail_sender = gmail_login + &quot;@gmail.com&quot;
  email_receiver = &quot;email_receiver&quot;
  
  message = &quot;&quot;&quot;\
  Subject: Hi there
  
  This message came from Python.&quot;&quot;&quot;
  
  # Log in to server and send email
  try:
    # Create a secure SSL context
    my_context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
      #server.ehlo()  # Can be omitted
      server.starttls(context = my_context)
      #server.ehlo()  # Can be omitted
      server.login(gmail_login, gmail_app_password)
      server.sendmail(gmail_sender, email_receiver, message)
  except Exception as err:
    print(&quot;\n&quot;, err, &quot;\n&quot;)
    exit(1)
  
  exit(0)
  </code></pre>