Example - send email.py

From PeformIQ Upgrade
Jump to navigation Jump to search
#!/usr/bin/env python
#
#
#
#-------------------------------------------------------------------------------

"""
Send email using outlook...
...
"""

#-------------------------------------------------------------------------------

import os
import re
import csv
import sys
import getopt
import random

#-------------------------------------------------------------------------------

debug_level     = 0
verbose_flg     = False

details         = None
mailing_list    = 'mailing_list.txt'

END             = re.compile('^#--')
BLANK           = re.compile('^$')
COMMENT         = re.compile('^#')

#===============================================================================

class Person:
   pass

   def __init__(self, l):
      self.Surname      = l[0]
      self.Firstname    = l[1]
      self.Email        = l[2]

      try:
         self.AltEmail  = l[3]
      except:
         self.AltEmail  = None

   def __str__(self):
      if self.AltEmail:
         return "%s/%s" % (self.Email, self.AltEmail)
      else:
         return self.Email

#-------------------------------------------------------------------------------

def read_details(fname):
   global details

   try:
      f = open(fname, 'r')
   except IOError, e:
      sys.stderr.write('[dserver]  Open failed: %s\n' % str(e))
      sys.exit(1)

   lines   = []

   while True:
      line = f.readline()

      if not line: break

      line = line.strip()

      if END.match(line):
         break

      if COMMENT.match(line):
         continue

      if BLANK.match(line):
         continue

      lines.append(line)

   f.close()

   details = []

   reader = csv.reader(lines, delimiter=',', quotechar='"')

   for row in reader:
      person = Person(row)
      details.append(person)

   if debug_level > 2: print "Read in %d email addresses from %s" % (len(details), fname)

   return details

#-------------------------------------------------------------------------------

def list(details):
   if details:
      for p in details:
         print p

#-------------------------------------------------------------------------------

def usage():
   USAGE = """
   
     Usage:
     
       $ dt.py
    
   """
   
   sys.stderr.write(USAGE)
   
#-------------------------------------------------------------------------------

def main(argv):
   global debug_flg, verbose_flg, no_orders, sample_period

   loop_cnt    = 1
   examine_flg = False

   #----- Process command line arguments ----------------------------

   try:
      opts, args = getopt.getopt(argv, "dehl:n:p:v",
              ["debug", "examine", "help", "loop", "no=", "period", "verbose"])
   except getopt.GetoptError:
      usage()
      sys.exit(2)
   else:
      for opt, arg in opts:
         if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
         elif opt in ("-e", "--examine"):
            scrape(SCRAPE)
            return 0
         elif opt in ("-l", "--loop"):
            loop_cnt = int(arg)
         elif opt in ("-n", "--no"):
            no_orders = int(arg)
         elif opt in ("-p", "--period"):
            sample_period = int(arg)
         elif opt in ("-v", "--verbose"):
            verbose_flg = True

   people = read_details(mailing_list)

   list(people)

#---------------------------------------------------------------------

if __name__ == "__main__":
   main(sys.argv[1:])

#---------------------------------------------------------------------

[[Category:Example}} {{Category:Python]]