WHERE cao.conference_id=%(id)s GROUP BY name ORDER BY 1 """, { 'id': confid }) s.writelines("\n") s.writelines("Additional options\n") s.writelines("------------------\n") s.writelines("%-50s %13s %13s\n" % ('Type', 'Confirmed', 'Unconfirmed')) rows = curs.fetchall() s.writelines(["%-50s %13s %13s\n" % (r[0][:50],r[1],r[2]) for r in rows]) s.writelines("%-50s %13s %13s\n" % ("", "------", "------")) s.writelines("%-50s %13s %13s\n" % (" Total", sum([r[1] for r in rows]), sum([r[2] for r in rows]))) if len(sys.argv) == 4: # send email msg = MIMEMultipart() msg['Subject'] = "%s registration report" % confname msg['From'] = '*****@*****.**' msg['To'] = sys.argv[3] msg.attach(MIMEText(s.getvalue().encode('utf-8'), 'plain', 'UTF-8')) s = smtplib.SMTP() s.connect() s.sendmail("*****@*****.**", [sys.argv[3]], msg.as_string()) s.quit() else: # Don't send email, so just print it print s.getvalue()