tools:mailer

Mailer

Jednoduchý scriptík v pythonu nad jedním pěkným modulem pro odesílání mailů bez nutnosti mít nakonfigurovaný server, nebo se někam logovat.

Script umožňuje odesílání i větších souborů a je poměrně blbuvzdorný.

Ukázka nápovědy

Usage: mailer [options] file1 file2 ...

Simple wrapper over mailer module, which allows you send emails with files.

Options:
  -h, --help            show this help message and exit
  -s SUBJECT, --subject=SUBJECT
  -f FROMW, --from=FROMW
                        your email address
  -t TO, --to=TO        recipients email address
  -m MSG, --msg=MSG     if set, script doesn't read from stdin
  -r, --html            for html emails

Zdroják a download

mailer.py
#! /usr/bin/env python
# MailerWrapper v1.0.0 (20.05.2011) by Bystroushaak (bystrousak@kitakitsune.org)
 
from optparse import OptionParser
import sys
import os.path
 
try:
        import DNS
except:
        print "This script requires python-dns module!"
        sys.exit(1)
 
try:
        import mailer
except:
        print "This script requires mailer module!"
        print "You can get it from: http://ginstrom.com/scribbles/2009/03/15/a-module-to-send-email-simply-in-python/"
 
parser = OptionParser(usage="usage: %prog [options] file1 file2 ...",
                description = "Simple wrapper over mailer module, which allows you send emails with files.")
parser.add_option("-s", "--subject", action = "store", default = "")
parser.add_option("-f", "--from", action = "store", help = "your email address", dest = "fromw") 
parser.add_option("-t", "--to", action = "store", help = "recipients email address")
parser.add_option("-m", "--msg", action = "store", default = None, help = "if set, script doesn't read from stdin")
parser.add_option("-r", "--html", action = "store_true", default = False, help = "for html emails")
 
 
options, args = parser.parse_args()
 
if options.fromw == None or "@" not in options.fromw:
        print "You have to specify FROM address!"
        sys.exit(1)
 
if options.to == None or "@" not in options.to:
        print "You have to specify TO address"
        sys.exit(1)
 
# =-=-=-=-=
# main code
# =-=-=-=-=
 
# create message
message = mailer.Message()
message.From = options.fromw
message.To = options.to
message.Subject = options.subject
 
if options.msg == None:
        try:
                if options.html:
                        message.Html = "".join(sys.stdin.readlines())
                else:
                        message.Body = "".join(sys.stdin.readlines())
        except:
                sys.exit(1)
else:
        if options.html:
                message.Html = options.msg
        else:
                message.Body = options.msg
 
# add attachments
for filename in args:
        if os.path.exists(filename):
                message.attach(filename)
        else:
                print filename, "doesn't exist!"
 
# get mx record
try:
        m = mailer.Mailer(DNS.mxlookup(options.to.split("@")[1])[1][1])
except:
        print "Can't find MX record for", options.to.split("@")[1]
        sys.exit(1)
 
# send email
m.send(message)