Send an e-mail and attachments with Python

  • Fri 16 March 2012

  • #! /usr/bin/python
    import os
    import fnmatch
    import smtplib
    import string
    from email import Encoders
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEImage import MIMEImage
    from email.Utils import formatdate
    COMMASPACE = ', '
    msg = MIMEMultipart()
    msg['Subject'] = 'Weekly Graphs'
    msg['From'] = ''
    msg['To'] = ''
    msg.preamble = 'Weekly Graphs blah'
    PATH = '/home/bmorrison/graphs'
     for path, dirs, files in os.walk(PATH):
      for file in files:
       fullpath = os.path.join(path,file)
       png = open(fullpath, 'r')
       img = MIMEImage(
       s = smtplib.SMTP('')
       s.sendmail('', '',\_string())

    So basically I wrote this to gather files in a directory and e-mail those files. This is a VERY raw script, but it does work. I still need to do validation/error checking to ensure I don't send anything I don't want to. I also have a shell script that actually gets/creates those files, I'd REALLY like to do it all in Python. I don't get much time to dev at work anymore and I'm more into my Django project at home than anything else. I also need to shift my focus of my home project(s).

    Comments !