From 859e4c88bb4a2164e5789d7eb6c927fa3822097f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 17 2017 10:09:11 +0000 Subject: Move where the smtp object is instanciated This should still helps us not running into socket error when running pagure without a SMTP server configured while still allowing us to keep the one connection per notification to send approach (and not one connection per email sent). --- diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index df3d971..b7cde73 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -219,16 +219,6 @@ def send_email(text, subject, to_mail, subject_tag = 'Pagure' smtp = None - if pagure.APP.config.get('EMAIL_SEND', True): - if pagure.APP.config['SMTP_SSL']: - smtp = smtplib.SMTP_SSL( - pagure.APP.config['SMTP_SERVER'], - pagure.APP.config['SMTP_PORT']) - else: - smtp = smtplib.SMTP( - pagure.APP.config['SMTP_SERVER'], - pagure.APP.config['SMTP_PORT']) - for mailto in to_mail.split(','): msg = MIMEText(text.encode('utf-8'), 'plain', 'utf-8') msg['Subject'] = header = Header( @@ -273,6 +263,15 @@ def send_email(text, subject, to_mail, print('*****/EMAIL******') continue try: + if smtp is None: + if pagure.APP.config['SMTP_SSL']: + smtp = smtplib.SMTP_SSL( + pagure.APP.config['SMTP_SERVER'], + pagure.APP.config['SMTP_PORT']) + else: + smtp = smtplib.SMTP( + pagure.APP.config['SMTP_SERVER'], + pagure.APP.config['SMTP_PORT']) if pagure.APP.config['SMTP_USERNAME'] \ and pagure.APP.config['SMTP_PASSWORD']: smtp.login( @@ -286,7 +285,8 @@ def send_email(text, subject, to_mail, msg.as_string()) except smtplib.SMTPException as err: pagure.LOG.exception(err) - smtp.quit() + if smtp: + smtp.quit() return msg