title: Gmail Python multiple attachments
body: <p>I am trying to create a little script that will email multiple attachments using gmail. The code below sends the email but not the attachments. The intended use is to cron a couple db queries and email the results. There will always be 2 files and the file names will be different each day as the <code>date</code> for the report is in the file name. Otherwise I would have just used:</p>
  
  <pre><code>part.add_header('Content-Disposition', 
      'attachment; filename="absolute Path for the file/s"')
  </code></pre>
  
  <p>Any help greatly appreciated.</p>
  
  <pre><code>import os
  import smtplib
  from email.MIMEMultipart import MIMEMultipart
  from email.MIMEText import MIMEText
  from email.MIMEImage import MIMEImage
  from email.MIMEBase import MIMEBase
  from email import Encoders
  
  
  #Set up crap for the attachments
  files = "/tmp/test/dbfiles"
  filenames = [os.path.join(files, f) for f in os.listdir(files)]
  #print filenames
  
  
  #Set up users for email
  gmail_user = "joe@email.com"
  gmail_pwd = "somepasswd"
  recipients = ['recipient1','recipient2']
  
  #Create Module
  def mail(to, subject, text, attach):
     msg = MIMEMultipart()
     msg['From'] = gmail_user
     msg['To'] = ", ".join(recipients)
     msg['Subject'] = subject
  
     msg.attach(MIMEText(text))
  
     mailServer = smtplib.SMTP("smtp.gmail.com", 587)
     mailServer.ehlo()
     mailServer.starttls()
     mailServer.ehlo()
     mailServer.login(gmail_user, gmail_pwd)
     mailServer.sendmail(gmail_user, to, msg.as_string())
     # Should be mailServer.quit(), but that crashes...
     mailServer.close()
  
  #get all the attachments
     for file in filenames:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(file, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                     % os.path.basename(file))
        msg.attach(part)
  #send it
  mail(recipients,
     "Todays report",
     "Test email",
     filenames)
  </code></pre>