Created
October 3, 2013 07:45
-
-
Save alcedo/6806504 to your computer and use it in GitHub Desktop.
simple python file that reads an input file and sends out email via unix mail / sendmail command. demonstrates many of the core python functions and features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys | |
| import os | |
| import datetime | |
| from subprocess import Popen, PIPE, call | |
| from datetime import date, datetime, date, time | |
| """ | |
| Sends out an email via the unix mail command, | |
| something like the following: | |
| echo "test" | mail -s subject -c ccPerson -r from to | |
| """ | |
| def unix_mail_helper(to, from_add, cc, subject, body): | |
| pipe = Popen(['mail', '-s', subject, '-c', cc, '-r', from_add, to], stdin=PIPE) | |
| pipe.stdin.write(body) | |
| pipe.communicate()[0] | |
| """ | |
| Alternatively:: | |
| emailAddress = '[email protected]' | |
| title = 'My Email Title' | |
| subject = 'This is the subject of my email' | |
| p1 = Popen(['echo', subject], stdout= PIPE) #Set up the echo command and direct the output to a pipe | |
| p2 = Popen(['mail', '-s' ,title, emailAddress], stdin=p1.stdout) #send p1's output to p2 | |
| p1.stdout.close() #make sure we close the output so p2 doesn't hang waiting for more input | |
| output = p2.communicate()[0] #run our commands | |
| """ | |
| """ | |
| Reads and parse the mail template for email | |
| information and details. | |
| """ | |
| def read_mail_template(mail_template_path): | |
| print "Parsing mail template" | |
| template_file = open(mail_template_path) | |
| to_line = template_file.readline().split(':')[1].strip() | |
| from_line = template_file.readline().split(':')[1].strip() | |
| cc_line = template_file.readline().split(':')[1].strip() | |
| subject = template_file.readline().split(':')[1].strip() | |
| #print "To:%s From:%s cc:%s subject:[%s]" %(to_line, from_line, cc_line, subject) | |
| body_text = "" | |
| for line in template_file: | |
| body_text += line | |
| #print "----\r\n%s" %(body_text) | |
| return to_line, from_line, cc_line, subject, body_text | |
| def send_mail(): | |
| if len(sys.argv) < 2: | |
| print "Please specify FULL PATH to mail template file for processing" | |
| else: | |
| mail_template_path = sys.argv[1] | |
| print "Using<" + mail_template_path + "> as mail template to send mail" | |
| to_line, from_line, cc_line, subject, body_text = read_mail_template( mail_template_path ) | |
| # sends out mail ! | |
| unix_mail_helper(to_line, from_line, cc_line, subject, body_text) | |
| """ | |
| This function prints out all relevant working directory | |
| information for debugging / logging purposes | |
| """ | |
| def print_working_dir_info(): | |
| print("Path at terminal when executing this file") | |
| print(os.getcwd() + "\n") | |
| print("This file path, relative to os.getcwd()") | |
| print(__file__ + "\n") | |
| print("This file full path (following symlinks)") | |
| full_path = os.path.realpath(__file__) | |
| print(full_path + "\n") | |
| print("This file directory and name") | |
| path, file = os.path.split(full_path) | |
| print(path + ' --> ' + file + "\n") | |
| print("This file directory only") | |
| print(os.path.dirname(full_path)) | |
| # START PROGRAM EXECUTION HERE !!! | |
| print "Chaser Script executed on: " + str(date.today()) | |
| send_mail() | |
| """ | |
| sample mail template file as follow. | |
| to: [email protected] | |
| from: [email protected] | |
| cc: [email protected] | |
| subject: test | |
| hello this is a test email.... | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment