sending emails with python - subject of message missing
Question: all. I have encountered a bit of a problem while trying to send emails with
python's `email` package along with `smtplib`. 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
[documentation](https://docs.python.org/2/library/email-examples.html).

    
    
    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()
    

Calling the function as `send_mail('john@doe.com',['john@doe.com'],'This is
the subject','Hello, World!')` results in an email sent correctly but without
a subject.

The output with or without files is the same. Also, reading the docs also
didn't help me.

I appreciate any help.


Answer: Instead of passing subject as an argument to MIMEMultipart, try assigning the
value to msg:

`msg['Subject'] = subject`

Great examples in the docs: <https://docs.python.org/3/library/email-
examples.html>

