initial version, working to send to smtp.gmail.com using SSL

This commit is contained in:
Josh Holtrop 2012-03-14 11:55:51 -04:00
commit 53096c9380

87
pygtk-smtp-send-note.py Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env python
import os
import sys
import gtk
import smtplib
message = ''
def get_config(fname):
config = {}
fh = open(fname, 'r')
script = fh.read()
fh.close()
exec(script, config)
return config
def error_popup(err_msg):
win = gtk.Window()
lbl = gtk.Label(err_msg)
button = gtk.Button('Close')
vbox = gtk.VBox()
vbox.pack_start(lbl)
vbox.pack_start(button)
win.add(vbox)
win.connect('destroy', lambda x: gtk.main_quit())
button.connect('clicked', lambda x: gtk.main_quit())
win.show_all()
gtk.main()
win.hide()
def main(argv):
config = {}
for arg in argv[1:]:
config = get_config(arg)
if not ('server' in config and 'port' in config and 'user' in config
and 'password' in config and 'to_addr' in config
and 'from_addr' in config):
sys.stderr.write('Missing a config field!\n')
return 1
win = gtk.Window()
entry = gtk.Entry()
entry.set_width_chars(80)
win.add(entry)
def activate_cb(*args):
global message
message = entry.get_text()
gtk.main_quit()
def cancel_cb(*args):
gtk.main_quit()
win.connect('destroy', cancel_cb)
entry.connect('activate', activate_cb)
win.show_all()
gtk.main()
win.hide()
if message == '':
return 0
msg = '''From: <%s>
To: <%s>
Subject: %s
<EOM>
''' % (config['from_addr'], config['to_addr'], message)
try:
smtp = smtplib.SMTP_SSL(config['server'], config['port'])
smtp.login(config['user'], config['password'])
smtp.sendmail(config['from_addr'], config['to_addr'], msg)
smtp.quit()
except smtplib.SMTPHeloError:
error_popup('Unable to connect to SMTP server')
except smtplib.SMTPAuthenticationError:
error_popup('Unable to authenticate to SMTP server')
except smtplib.SMTPRecipientsRefused:
error_popup('Recipients refused')
except smtplib.SMTPSenderRefused:
error_popup('Sender refused')
except smtplib.SMTPDataError:
error_popup('SMTP data error')
# except:
# error_popup('Unknown SMTP error')
return 0
sys.exit(main(sys.argv))