title: sending emails with python - subject of message missing
body: <p>all. I have encountered a bit of a problem while trying to send emails with python's <code>email</code> package along with <code>smtplib</code>. I have set up a function that send an email and it works well, with the exception that the email always comes without the subject. I am not new to python, but I am new to using it for internet-related things such as this one. I have set up the following from several answer in this forum as well as the examples in the <a href="https://docs.python.org/2/library/email-examples.html" rel="nofollow">documentation</a>.</p>
  
  <pre><code>import smtplib
  from os.path import basename
  from email import encoders
  from email.mime.application import MIMEApplication
  from email.mime.base import MIMEBase
  from email.mime.audio import MIMEAudio
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText
  from email.utils import COMMASPACE, formatdate
  def send_mail(send_from, send_to, subject, text, files=None, server="smtp.gmail.com"):
      import mimetypes
      assert isinstance(send_to, list)
      msg = MIMEMultipart(From=send_from, To=COMMASPACE.join(send_to), Date=formatdate(localtime=True), Subject=subject)
      msg.attach(MIMEText(text))
      for f in files or []:
              print f
              ctype,encoding=mimetypes.guess_type(f)
              if ctype is None or encoding is not None:
                      ctype = 'application/octet-stream'
              maintype, subtype = ctype.split('/', 1)
              if maintype == 'text':
                      fp = open(f)
                      msg = MIMEText(fp.read(), _subtype=subtype)
                      fp.close()
              elif maintype == 'image':
                      fp = open(f, 'rb')
                      msg = MIMEImage(fp.read(), _subtype=subtype)
                      fp.close()
              elif maintype == 'audio':
                      fp = open(f, 'rb')
                      msg = MIMEAudio(fp.read(), _subtype=subtype)
                      fp.close()
              else:
                      fp = open(f, 'rb')
                      msg = MIMEBase(maintype, subtype)
                      msg.set_payload(fp.read())
                      fp.close()
                      encoders.encode_base64(msg)
              msg.add_header('Content-Disposition', 'attachment', filename=basename(f))
      smtp = smtplib.SMTP(server)
      smtp.starttls()
      usrname=send_from
      pwd=raw_input("Type your password:")
      smtp.login(usrname,pwd)
      smtp.sendmail(send_from, send_to, msg.as_string())
      smtp.close()
  </code></pre>
  
  <p>Calling the function as <code>send_mail('john@doe.com',['john@doe.com'],'This is the subject','Hello, World!')</code> results in an email sent correctly but without a subject.</p>
  
  <p>The output with or without files is the same. Also, reading the docs also didn't help me.</p>
  
  <p>I appreciate any help.</p>