Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created December 8, 2008 16:42
Show Gist options
  • Save atifaziz/33509 to your computer and use it in GitHub Desktop.
Save atifaziz/33509 to your computer and use it in GitHub Desktop.
IronPython program to send mail
# Copyright (c) 2008, Atif Aziz.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# - Neither the name of the original author (Atif Aziz) nor the names
# of its contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
from System import Console
from System.IO import File
from System.Net.Mail import SmtpClient, MailMessage, MailAddress
from System.Net import NetworkCredential
def parse_options(args, names, flags):
args = list(args) # copy for r/w
required = [name[:-1] for name in names if '!' == name[-1:]]
all = [name.rstrip('!') for name in names]
all.extend(flags)
options = {}
anon = []
while args:
arg = args.pop(0)
if arg[:2] == '--':
name = arg[2:]
if not name: # comment
break
if not name in all:
raise Exception('Unknown argument: %s' % name)
options[name] = name in flags and True or args.pop(0)
else:
anon.append(arg)
for name in required:
if not name in options:
raise Exception('Missing required argument: %s' % name)
return options, anon
def main(args):
options, tail_args = parse_options(args,
('from!', 'to!', 'cc', 'bcc', 'subject!', 'body', 'host!', 'port', 'username', 'password'),
('ssl', 'html'))
if tail_args:
raise Exception('Unexpected argument: %s' % tail_args[0])
smtp = SmtpClient(
options.get('host'),
int(options.get('port', 25)),
EnableSsl = options.get('ssl', False))
username, password = options.get('username'), options.get('password')
if username:
if username == '*':
smtp.UseDefaultCredentials = True
else:
smtp.Credentials = NetworkCredential(username, password)
mail = MailMessage(
From = MailAddress(options.get('from')),
Subject = options.get('subject'))
mail.To.Add(options.get('to'))
cc = options.get('cc')
if cc:
mail.CC.Add(cc)
bcc = options.get('bcc')
if bcc:
mail.Bcc.Add(bcc)
mail.IsBodyHtml = options.get('html', False)
body = options.get('body', '-')
mail.Body = '-' == body and Console.In.ReadToEnd() or File.ReadAllText(body)
smtp.Send(mail)
if __name__ == '__main__':
try:
main(sys.argv[1:])
except Exception, e:
print >> sys.stderr, e
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment