title: Testing python smtp email service
body: <p>So I wrote a simple email service using smtplib to send welcome emails when user first logs in. I was wondering what's the best way to test out my code. </p>
  
  <pre><code>def send_email(nickname, email):
      msg = MIMEMultipart('alternative')
  
      config = read_config_file(MAIL_CONFIG_PATH)
      sen = config.get('gmail_credentials', 'email')
      pwd = config.get('gmail_credentials', 'password')
  
      server = None
      try:
          server = smtplib.SMTP('smtp.gmail.com', 587) 
          server.ehlo()
          server.starttls()
          server.login(sen, pwd)
          server.sendmail(sen, [email], msg.as_string())
          return 'Sent!'
  
      finally:
          if server:
          server.quit()
  </code></pre>
  
  <p>I found out that you can use a fake smtp server:</p>
  
  <pre><code>sudo python -m smtpd -n -c DebuggingServer localhost:25
  </code></pre>
  
  <p>But I am still not sure how I should implement something like this into my code. How do I change my API so that it runs the SMTP server during testing and connects to the GMAIL server for the actual calls? And are there init scripts which will run the SMTP server on our servers (this will be deployed on AWS EC2). </p>