pythonEmail
version 0.1
Requires Mac OS X 10.3 or higher
HAMSoft Engineering allows free use of this code and/or software in its "as is" condition. HAMSoft Engineering disclaims any liability of any kind for any damages whatsoever resulting from the use of this code and/or software. If you find it useful please consider making a donation to help HAMSoft Engineering stay in business.
download the python text file
NOTE: After downloading, make the text file executable using "chmod a+x pythonEmail.py"
This is a command line script, written in python, for sending a text email. It can use smtp authentication if needed. I wrote this by following an example found here. I basically just upgraded that script by adding the smtp authentication portion. The script needs 8 parameters passed to it as defined at the top of the script.
Usage:
pythonEmail.py "sender" "receiver" "subject" "bodyText" "smptHost" "username" "password" "port"
Python Code: #!/usr/bin/env python # send a text email from the command line using python # # created 6/25/09 by Hank McShane # version 1.0 # # The code at the following URL was modified to create this script: # http://www.cs.cmu.edu/~benhdj/Mac/unix.html#smtpScript # # NOTE: if smtp username is "" then code will not use the smtp authentication method # # input parameters # sys.argv[1] is the sender email address # sys.argv[2] is the reciever email address, # this can be a comma separated string for multiple recievers # sys.argv[3] is the subject text # sys.argv[4] is the body text # sys.argv[5] is the smtp host # sys.argv[6] is the smtp username # sys.argv[7] is the smtp password # sys.argv[8] is the smtp port # import smtplib, email, sys, time # check to make sure the number of arguments is correct if len(sys.argv) != 9: print 'Usage: pythonEmail.py <sender> <receiver> <subject> <bodyText> <smptHost> <username> <password> <port>' sys.exit(1) # get the argv variables sender = sys.argv[1] receiver = sys.argv[2] subj = sys.argv[3] bodyText = sys.argv[4] smtpHost = sys.argv[5] username = sys.argv[6] # use "" if no SMTP authentication is required passwd = sys.argv[7] # ignored if no SMTP authentication is required port = sys.argv[8] # ignored if no SMTP authentication is required # create a list from the receiver in case we have a comma separated string of multiple receivers rList = [] rList = receiver.split(','); # setup the message header timegmt = time.gmtime(time.time( )) fmt = '%a, %d %b %Y %H:%M:%S GMT' datestr = time.strftime(fmt, timegmt) msg = 'From: %s\nTo: %s\nDate: %s\nSubject: %s\n%s' \ % (sender, receiver, datestr, subj, bodyText) # determine if a passworded smpt host is being used and connect as necessary if username == "": server = smtplib.SMTP(smtpHost) # smtp server is not password protected else: server = smtplib.SMTP(smtpHost, port) server.login(username, passwd) failed = server.sendmail(sender, rList, msg) server.quit() # return the status if failed: print 'pythonEmail.py: Failed:', failed else: print 'pythonEmail.py: Finished with no errors.'
Example usage of the tool with applescript:
-- this uses the pythonEmail.py script to send a text email from the command line
-- setup the path to the python script
set pythonScriptPath to (path to desktop folder as text) & "pythonEmail.py"
-- setup the python script input parameters
set theSender to "sender@email.com"
set theReceiver to "firstReceiver@email.com, secondReceiver@email.com"
set theSubject to "Python Email Test"
set theBody to "Some body text" & return & return & "complete with multiple paragraphs"
set smtpHost to "smtp.email.com"
set smtpUserName to "smtpUserName"
set smtpPassword to "smtpPassword"
set smtpPort to "587"
-- send the email
set theResult to do shell script (quoted form of POSIX path of pythonScriptPath & space & quoted form of theSender & space & quoted form of theReceiver & space & quoted form of theSubject & space & quoted form of theBody & space & quoted form of smtpHost & space & quoted form of smtpUserName & space & quoted form of smtpPassword & space & quoted form of smtpPort)
-- display the results
tell me
activate
display dialog theResult buttons {"OK"} default button 1 with icon note
end tell
