Don't like ads? PRO users don't see any ads ;-)
Guest

comprookie2000

By: a guest on Apr 3rd, 2009  |  syntax: Python  |  size: 1.34 KB  |  hits: 335  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2.  
  3. import subprocess
  4. import smtplib, sys, MimeWriter, StringIO, base64
  5. import os
  6.  
  7. def get_bugzs():
  8.         p = subprocess.Popen('searchbugz > /tmp/bugz.txt', shell=True, stdout=subprocess.PIPE)
  9.         return p.stdout.readlines()
  10.        
  11. def mail(serverURL=None, port=None, sender='', to='', subject='', text=''):
  12.         """
  13.         Usage:
  14.         mail('smtp.gmail.com', 587, 'yourname@gmail.com', 'fromaddress@someaddress.com', 'Bug Wranglers', 'Current Bugs:')
  15.         """
  16.         message = StringIO.StringIO()
  17.         writer = MimeWriter.MimeWriter(message)
  18.         writer.addheader('Subject', subject)
  19.         writer.startmultipartbody('mixed')
  20.  
  21.         # start off with a text/plain part
  22.         part = writer.nextpart()
  23.         body = part.startbody('text/plain')
  24.         body.write(text)
  25.  
  26.         # now add an attachment
  27.         part = writer.nextpart()
  28.         part.addheader('Content-Transfer-Encoding', 'base64')
  29.         body = part.startbody('text/plain')
  30.         base64.encode(open('/tmp/bugz.txt', 'rb'), body)
  31.  
  32.         # finish off
  33.         writer.lastpart()
  34.  
  35.         # send the mail
  36.         smtp = smtplib.SMTP('smtp.gmail.com', 587)
  37.         smtp.ehlo()
  38.         smtp.starttls()
  39.         smtp.ehlo()
  40.         smtp.login('yourname@gmail.com', 'passwd')
  41.         smtp.sendmail(sender, to, message.getvalue())
  42.         smtp.quit()
  43.  
  44. if __name__ == "__main__":
  45.         get_bugzs()
  46.         mail('smtp.gmail.com', 587, 'yourname@gmail.com','fromaddress@someaddress.com', 'Bug Wranglers', 'Current Bugs:')
  47.         os.remove('/tmp/bugz.txt')