#!/usr/bin/python # -*- mode: python; coding: utf-8 -*- # Copyright © 2008 Jeffrey C. Ollie # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see # . import sys import smtplib from optparse import OptionParser from email.charset import Charset, SHORTEST from email.message import Message from email import utils from email.header import Header from email.generator import Generator from cStringIO import StringIO default_recipients = ["Fedora Development List ", "Fedora Test List "], default_sender = "Rawhide Report " utf8 = Charset('utf-8') utf8.body_encoding = None utf8.header_encoding = SHORTEST parser = OptionParser() parser.add_option("--subject", action = "store", dest = "subject", help = "subject for email") parser.add_option("--from", action = "store", dest = "sender", default = default_sender, help = "report will be from EMAIL", metavar = "EMAIL") parser.add_option("--to", action = "append", dest = "recipients", default = [], help="add EMAIL to list of recipients", metavar="EMAIL") (options, args) = parser.parse_args() if options.subject is None: sys.stderr.write('Must specify subject!\n') sys.exit(1) if len(options.recipients) == 0: options.recipients = default_recipients subject = options.subject.decode('utf-8') sender_realname, sender_email = utils.parseaddr(options.sender.decode('utf-8')) sender = utils.formataddr((sender_realname, sender_email)) recipients_email_only = [] recipients = [] for recipient in options.recipients: recipient_realname, recipient_email = utils.parseaddr(recipient.decode('utf-8')) recipients_email_only.append(recipient_email) recipients.append(utils.formataddr((recipient_realname, recipient_email))) body_parts = [] for arg in args: body_parts.append(file(arg, 'r').read().decode('utf-8', 'replace').encode('utf-8')) body = ''.join(body_parts) msg = Message() msg.set_charset(utf8) msg.set_payload(body) msg['From'] = Header(sender, utf8) msg['To'] = Header(u', '.join(recipients), utf8) msg['Subject'] = Header(subject, utf8) msg['Date'] = Header(unicode(utils.formatdate()), utf8) msg_fp = StringIO() msg_g = Generator(msg_fp, False) msg_g.flatten(msg) smtp = smtplib.SMTP() smtp.connect() smtp.sendmail(sender_email, recipients_email_only, msg_fp.getvalue()) smtp.close()