title: Trying to send a mail using smtplib in python. Mail body comes alongwith subject line
body: <p>This is a weird issue that is bugging me since the past 2 days. I wrote a simple send mail function that uses smtplib to send emails with image as attachment.</p>
  
  <p>The problem is the body part gets concatenated to the Subject line.
  If I dont use a MIME message and just a string, they are separated out correctly. But, then normal strings don't allow image attachments.</p>
  
  <p>Any library that I am missing here?</p>
  
  <p>Please the code below:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  """
  Created on Tue Jun 14 20:08:00 2016
  
  @author: HOME
  """
  
  import smtplib
  from email.mime.image import MIMEImage
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText 
  import base64
  import time
  import datetime
  
  print str(datetime.datetime.now())
  
  
  def send_mail(pwd):
  
      password = base64.b64decode(pwd)
  
      # in the prod system, ask the mail exchange server and port
  
      server = smtplib.SMTP('smtp.gmail.com:587')
      server.ehlo()
      server.starttls()
  
      server.login('someemail@gmail.com', password)
  
      msg = MIMEMultipart()
  
      body = "\nThe message body is this one. Thanks \n\n"
      subject = "Your daily digest " + str(datetime.datetime.now())
  
      msg['From'] = "someemail@gmail.com"
      msg['To'] = "someemail@gmail.com"
  
      # I was hoping that creating a string in the subject should parse for newline and automatically pick the 2nd line onwards as body
      # If I send mail using the commented code (line 46 t0 line 52), the subject and body are different. But I am unable to attach images
      # but if i use the MIMEMultipart message , then i can attach images, but the body comes in the subject line 
      msg['Subject'] = "\r\n".join([subject,"",body])
  
      #msg.attach(MIMEText(body,'text'))
      msg.preamble = 'Daily Updates'
      """
      msg = "\r\n".join([
      "From: someemail@gmail.com",
      "To: someemail@gmail.com",
      "Subject: Daily digest " + str(datetime.datetime.now()),
      "",
      "Good Morning, How are you ? "
      ])
  
      """
      # Image attachment code
      fp = open("D://sample.png",'rb')
      img = MIMEImage(fp.read())
      msg.attach(img)
  
      print msg
      #try:
      server.sendmail('someemail@gmail.com','someemail@gmail.com',msg.as_string())
      print "Mail send successfully to someemail@gmail.com"
      server.close()
      #except:
      #   print "Mail not sent"
  
  if __name__ == '__main__':
      pwd = base64.b64encode('howdy')
      send_mail(pwd)
  </code></pre>