From e5e8b98163805e8ece3a131b175713bfccc7a102 Mon Sep 17 00:00:00 2001 From: Xavier Izard Date: Wed, 19 Sep 2012 09:34:25 +0200 Subject: [PATCH] Support for sending reports via SMTP --- ChangeLog | 4 ++++ lib/urlwatch/mailer.py | 15 +++++++++++++++ urlwatch | 47 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 lib/urlwatch/mailer.py diff --git a/ChangeLog b/ChangeLog index b17fbbd..eaaa712 100644 --- a/ChangeLog +++ b/ChangeLog @@ -120,3 +120,7 @@ and html2txt, this has been tested on Debian-based systems * urlwatch 1.15 released +2012-09-13 Xavier Izard + * Added basic support for email delivery, using internal SMTP lib. + (see options --mailto, --mailfrom and --smtp) + diff --git a/lib/urlwatch/mailer.py b/lib/urlwatch/mailer.py new file mode 100644 index 0000000..ff6adc3 --- /dev/null +++ b/lib/urlwatch/mailer.py @@ -0,0 +1,15 @@ + +import smtplib +from email.mime.text import MIMEText + +def send(smtp_server, from_email, to_email, subject, body): + msg = MIMEText(body, 'plain', 'utf_8') + msg['Subject'] = subject + msg['From'] = from_email + msg['To'] = to_email + + s = smtplib.SMTP() + s.connect(smtp_server, 25) + s.sendmail(from_email, [to_email], msg.as_string()) + s.quit() + diff --git a/urlwatch b/urlwatch index 088949f..3d441d5 100755 --- a/urlwatch +++ b/urlwatch @@ -91,6 +91,7 @@ import imp import concurrent.futures from urlwatch import handler +from urlwatch import mailer # One minute (=60 seconds) timeout for each request to avoid hanging socket.setdefaulttimeout(60) @@ -154,6 +155,9 @@ if __name__ == '__main__': parser.add_option('', '--urls', dest='urls', metavar='FILE', help='Read URLs from the specified file') parser.add_option('', '--hooks', dest='hooks', metavar='FILE', help='Use specified file as hooks.py module') parser.add_option('-e', '--display-errors', action='store_true', dest='display_errors', help='Include HTTP errors (404, etc..) in the output') + parser.add_option('-t', '--mailto', dest='email', metavar='ADDRESS', help='Send results via e-mail to ADDRESS') + parser.add_option('-f', '--mailfrom', dest='email_from', metavar='ADDRESS', help='Alternate From: address for e-mail (--mailto)') + parser.add_option('-s', '--smtp', dest='email_smtp', metavar='SERVER', help='SMTP server for e-mail (--mailto)') parser.set_defaults(verbose=False, display_errors=False) @@ -172,6 +176,25 @@ if __name__ == '__main__': log.info('turning display of errors ON') display_errors = True + if options.email: + log.info('Send emails enabled') + enable_emails = True + email_smtp_server = options.email_smtp or 'localhost' + email_sender_address = options.email_from or options.email + email_receiver_address = options.email + else: + if options.email_from: + log.error('--mailfrom without --mailto') + print 'Error: --mailfrom needs --mailto' + sys.exit(1) + + if options.email_smtp: + log.error('--smtp without --mailto') + print 'Error: --smtp needs --mailto' + sys.exit(1) + + enable_emails = False + if options.urls: if os.path.isfile(options.urls): urls_txt = options.urls @@ -328,16 +351,18 @@ if __name__ == '__main__': end = datetime.datetime.now() + short_summary = '' + # Output everything if len(summary) > 1: log.info('printing summary with %d items' % len(summary)) - print '-'*line_length - print 'summary: %d changes' % (len(summary),) - print '' + short_summary = '-'*line_length + '\n' + short_summary += 'summary: %d changes' % (len(summary),) + '\n\n' for id, line in enumerate(summary): - print '%02d. %s' % (id+1, line) - print '-'*line_length - print '\n\n\n' + short_summary += '%02d. %s' % (id+1, line) + '\n' + short_summary += '-'*line_length + '\n' + short_summary += '\n\n\n' + print short_summary else: log.info('summary is too short - not printing') if len(details) > 1: @@ -347,6 +372,16 @@ if __name__ == '__main__': print '%s %s, %s' % (pkgname, __version__, __copyright__) print 'Website: %s' % (__homepage__,) print 'watched %d URLs in %d seconds\n' % (count, (end-start).seconds) + + if enable_emails: + try: + subject = 'Changes detected (%d)' % len(summary) + mailer.send(email_smtp_server, email_sender_address, + email_receiver_address, subject, + short_summary + '\n' + '\n'.join(details)) + log.info('E-Mail to %s sent.', email_receiver_address) + except Exception, e: + log.warning('E-Mail delivery error: %s', e) else: log.info('no details collected - not printing') -- 2.11.4.GIT