title: Sending email in python (MIMEmultipart)
body: <p>How should I send an email with text format and html format in the same body? What is the use of <code>MIMEmultipart</code>?</p>
  
  <pre><code>MIMEMultipart([MIMEText(msg, 'text'),MIMEtext(html,'html')])
  </code></pre>
  
  <p>I was able to receive an email using this but with a blank body</p>
  
  <p>PS: I am trying to send a text and attach a table in the same body. I don't want to send a table as an attachment.</p>
  
  <pre><code>html = """
    &lt;html&gt;
     &lt;head&gt;
      &lt;style&gt; 
       table, th, td {{ border: 1px solid black; border-collapse: collapse; }} th, td {{ padding: 5px; }}
      &lt;/style&gt;
     &lt;/head&gt;
     &lt;body&gt;&lt;p&gt;Hello, Friend This data is from a data frame.&lt;/p&gt;
      &lt;p&gt;Here is your data:&lt;/p&gt;
      {table}
      &lt;p&gt;Regards,&lt;/p&gt;
      &lt;p&gt;Me&lt;/p&gt;
     &lt;/body&gt;
    &lt;/html&gt; """
  
  text = """
  Hello, Friend.
  
  Here is your data:
  
  {table}
  
  Regards,
  
  Me"""
  text = text.format(table=tabulate(df, headers=list(df.columns), tablefmt="grid"))
  html = html.format(table=tabulate(df, headers=list(df.columns), tablefmt="html"))
  if(df['date'][0].year==1900 and df['date'][0].month==datetime.date.today().month and df['date'][0].day==datetime.date.today().day):
  a2=smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
  a2.starttls()
  myadd='abc@gmail.com'
  passwd=getpass.getpass(prompt='Password: ')
  try :
  
      a2.login(myadd,passwd)
  except Exception :
      print("login unsuccessful")
  def get_contacts(filename):
      name=[]
      email=[]
      with open('email.txt','r') as fl:
           l=fl.readlines()
           print(l)
           print(type(l))
           for i in l:
            try: 
                name.append(i.split('\n')[0].split()[0])
                email.append(i.split('\n')[0].split()[1]) 
            except Exception:
                break
           fl.close()
      return (name,email)
  def temp_message(filename):
      with open(filename,'r') as fl1:
          l2=fl1.read()
      return(Template(l2))
  name,email=get_contacts('email.txt')    
  tmp1=temp_message('temp1.txt')   
  for name,eml in zip(name,email):
      msg=MIMEMultipart([MIMEText(msg, 'text'),MIMEtext(html,'html')])
      message=tmp1.substitute(USER_NAME=name.title())
      print(message)
      msg['FROM']=myadd
      msg['TO']=eml
      msg['Subject']="This is TEST"
      msg.attach(MIMEText(message, 'plain')) 
      #       msg.set_payload([MIMEText(message, 'plain'),MIMEText(html, 'html')])
      # send the message via the server set up earlier.
      a2.send_message(msg)
      del msg
      a2.quit()
  </code></pre>