title: Using HTML templates to send emails in python
body: <p>I'm trying to write a separate mail service, which is decoupled with our Flask application. I'm looking for a way to send welcome emails when users first log into our Flask application. I'm using Celery and RabbitMQ to do it asynchronously.</p>
  <p>Here is my email function:</p>
  <pre><code>sen = 'example@gmail.com'
  pwd = 'my_password'
  
  @celery.task
  def send_email(nickname, email):
      msg = MIMEMultipart('alternative')
      msg['Subject'] = 'my_sub'
      msg['From'] = sen
      msg['To'] = email
  
      html = &lt;b&gt;test_body&lt;/b&gt;
  
      part1 = MIMEText(html, 'html')
      msg.attach(part1)
  
      server = smtplib.SMTP(&quot;smtp.gmail.com&quot;, 587) 
      server.ehlo()
      server.starttls()
      server.login(sen, pwd)
      server.sendmail(sen, [email], msg.as_string())
      server.close()
  </code></pre>
  <p>Initially I was using Flask's <code>render_template</code> to get the HTML body and subject. But I don't want to use the Flask extension (I have my reasons). So my questions are:</p>
  <ol>
  <li><p>How can I use email templates so that the subject and body fields can be configured easily?</p>
  </li>
  <li><p>How can I put the default email sender and password in a <code>config</code> file/email template (might be related to q1)?</p>
  </li>
  <li><p>It seems to be that I have a lot of code to send a simple email. Can you suggest some optimization techniques (omitting steps)?</p>
  </li>
  </ol>