Metasend

From EggeWiki
Revision as of 12:22, 8 June 2006 by Brianegge (talk | contribs)

metasend is crude interface for sending non-text mail. Today I ran into a problem where a program expected it to be installed, and failed. Since what metasend is doing is not too terrible difficult, I figured I could create a Python program to do the same.

# $Header: $
# Simulates the unix utility 'metasend'
# Specifically, this was created for Solaris 10 x86, which doesn't have metasend installed.  Some old tcl scripts call metasend, so this should keep them running
# http://www.gsp.com/cgi-bin/man.cgi?section=1&topic=metasend

import sys
import os
import pwd
import string
import smtplib
import re
import traceback
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from optparse import OptionParser

def main(argv):
    parser = OptionParser()
    parser.add_option("-b", "--batch", action="store_true", help="specifies Batch (non-interactive) Mode. Will exit with an error message if all additional needed information is not provided on the command line.")
    parser.add_option("-c", "--cc", help="specifies the CC address")
    parser.add_option("-D", "--description", help="specifies a string to be used as the Content-description value")
    parser.add_option("-e", "--encoding", help="specifies the encoding type. Must be either \"base64\", \"quoted-printable\", \"7bit\", or \"x-uue\". \"7bit\" means no encoding is performed.")
    parser.add_option("-E", action="store_true", help="specifies that the file being included is already a full MIME entity, and does not need to have any Content-* or other header fields added.")
    parser.add_option("-f", "--filename", help="specifies the file containing the data")
    parser.add_option("-F", "--from", dest="_from", help="specifies the From address")
    parser.add_option("-i", "--content-id-mime", help="specifies the content-id value for the MIME entity. Must be a legal content-id value, enclosed in angle brackets.")
    parser.add_option("-I", "--content-id", help="specifies the content-id for the multipart entity being created by metasend, if any. Must be a legal content-id value, enclosed in angle brackets.")
    parser.add_option("-m", "--mime-type", help="specifies the MIME content-type")
    parser.add_option("-n", action="append", help="specifies that an additional file is to be included.")
    parser.add_option("-o", "--outputfile", help="specifies that the output from metasend should go to a named file rather than be delivered as mail.")
    parser.add_option("-P", "--preamblefile", help="specifies a file containing alternative text to be put in the \"preamble\" area of a MIME multipart message.")
    parser.add_option("-s", "--subject", help="specifies the Subject field")
    parser.add_option("-S", "--splitsize", help="specifies the maximum size before splitting into parts")
    parser.add_option("-t", "--to", help="specifies the To address")
    parser.add_option("-z", help="specifies that the temporary files should be deleted EVEN IF DELIVERY FAILS.")
    parser.add_option("-/", "--subtype", help="specifies the use of a MIME multipart subtype other than \"mixed\".")

    parser.set_defaults(_from=pwd.getpwuid(os.getuid())[0])
    (options, args) = parser.parse_args()

    mailhost = smtplib.SMTP()
    mailhost.connect('relay.london.kbcfp.com:25')
    
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = options.subject
    msgRoot['From'] = options._from
    msgRoot['To'] = options.to
    msgRoot['Cc'] = options.cc
    msgRoot.preamble = 'This is a multi-part message in MIME format.'
    
    html = open(options.filename).read()
    msgText = MIMEText(html, 'html')
    msgRoot.attach(msgText)

    sendto = (' '.join([elem for elem in [options.to, options.cc] if elem != None])).split(' ')
    mailhost.sendmail(options._from, sendto, msgRoot.as_string())
    mailhost.quit()


if __name__ == '__main__':
    main(sys.argv)