PDA

View Full Version : pyMail - simple mail sending script.


Benny
09-17-2002, 12:13 AM
Hey, whipped this script together so I could send things like logs and text files to specific email addresses easily without the need for an MTA installed on the box.


#!/usr/bin/env python

import smtplib
import sys
import getopt
import string

server = "192.168.4.9"

def main():
try:
optlist, files = getopt.getopt(sys.argv[1:], "f:t:s:")
except getopt.GetoptError:
usage()
sys.exit(1)

if len(files) == 0:
usage()
sys.exit(1)

fromAddr = None
toAddr = None
subject = None

for option, value in optlist:
if option == "-f":
fromAddr = value
elif option == "-t":
toAddr = value.split()
elif option == "-s":
subject = value

if (not fromAddr) or (not toAddr) or (not subject):
usage()
sys.exit(1)

message = messageBody(toAddr, fromAddr, subject, files)
sendMail(toAddr, fromAddr, message)


def usage():
print "\nInvalid Usage!!"
print "\n%s Usage: -t \"ToAddress\" -f fromAdress -s \"Subject\" file1 file2 ..." % sys.argv[0]
print "\nCommands:"
print "\t -t : Specify the email address(s) mails will be sent to*"
print "\t -f : Specify the email address the mail will appear to be sent from"
print "\t -s : Specify the subject line of the email message"
print "\n* Multiple email addesses must be enclosed in \" \" same as subject\
\n lines containing spaces"
print "eg: -t \"email1@email.com email2@email.com\" -s \"Subject with spaces\"\n"


def messageBody(toAddr, fromAddr, subject, fileList):
message = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
% (fromAddr, string.join(toAddr, ", "), subject))

for fileName in fileList:
message = message+"Below is the Contents of File: %s\n\n" % fileName
message = message+"\n----------------START OF %s-------------\n\n" % fileName

try:
file = open(fileName, "r")
lines = file.read()
file.close()
except:
print "\nError, File Access Error: %s\n" % fileName
sys.exit(1)

message = message+lines
message = message+"\n\n-----------------END OF %s---------------\n\n\n" % fileName

return(message)


def sendMail(toAddr, fromAddr, message):
print "\nConnecting to server \t->\t",
mailServer = smtplib.SMTP(server)
print "Connected"
print "Sending Mail \t\t->\t",
mailServer.sendmail(fromAddr, toAddr, message)
print "Sent\n"
mailServer.quit()

main()


It takes all of the options (except mail server) from the command line and can send multiple files and to multiple addresses.

This is a testiment to the great documentation at www.python.org ..... made coding this a breeze!

Cheers
Ben.

Strike
09-17-2002, 03:21 AM
Cool, now just combine that with a Python mail server and you have a complete package :P

No, seriously, it looks pretty good. Only thing I can think of immediately that I might change would be to change the usage() function to simply use one big triple-quoted multiline string instead of messing with formatting with control characters.

I like the use of getopt. Good to see standard libraries used where appropriate (and used correctly! bonus!)

Benny
09-17-2002, 04:26 AM
Originally posted by Strike
Cool, now just combine that with a Python mail server and you have a complete package :P

No, seriously, it looks pretty good. Only thing I can think of immediately that I might change would be to change the usage() function to simply use one big triple-quoted multiline string instead of messing with formatting with control characters.



Ahh yeh, probably should have done that. Coding in C too much lately, probably why I didn't think to.

Cheers.

Strike
09-18-2002, 01:53 PM
New library for Python 2.2 - the "email" module: http://www.python.org/doc/current/lib/module-email.html
So, if you feel so inclined, you can now handle MIME messages more easily