title: Python - send bulk emails
body: <p>I've been tasked with sending around 250 emails with around 30kb attachments per email. Each attachment is unique to the recipient. My below code works, although I feel it's too slow, it sends an email around every 7 seconds which across 250 emails will take 29 mins. Obviously parallelising it would help move things along however, I'm curious as to whether my code can be improved. Please note that I haven't yet implemented the targeted attachment and emails as that shouldn't cause such a large performance hit.</p>
  
  <pre><code>import os,datetime
  def send_mail(recipient, subject, message, files=None):
  
      import smtplib,email,os
      from email.mime.multipart import MIMEMultipart
      from email.mime.text import MIMEText
      from email.mime.application import MIMEApplication
      from os.path import basename
  
      username = "myemail"
      password ="mypass"
  
      mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
      mailServer.ehlo()
      mailServer.starttls()
      mailServer.ehlo()
      mailServer.login(username, password)
  
      for i in range(1,15):
          try:
              msg = MIMEMultipart()
              msg['From'] = username
              msg['To'] = recipient
              msg['Subject'] = subject
              msg.attach(MIMEText(message))
              for f in files or []:
                  with open(f, "rb") as fil:
                      part = MIMEApplication(
                          fil.read(),
                          Name=basename(f)
                      )
                      part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
                      msg.attach(part)
  
  
              print('sending mail to ' + recipient + ' on ' + subject)
  
  
              mailServer.sendmail(username, recipient, msg.as_string())
  
  
          except error as e:
              print(str(e))
  
      mailServer.close()
  
  print(datetime.datetime.now())
  send_mail('recipent, 'Sent using Python', 'May the force be with you.',["colours.xls"])
  print(datetime.datetime.now())
  </code></pre>