diff --git a/mail.py b/mail.py index 35bd774c6466efe53e8db78116e11fd51f0659b1..7b461f9577f4333fd2153d4a90daaf177625ad89 100644 --- a/mail.py +++ b/mail.py @@ -14,23 +14,22 @@ def send_message(msgtype, recipients, **kwargs): try: msg['Subject'] = render_template('mails/'+msgtype+'.subject', **kwargs) msg.set_content(render_template('mails/'+msgtype+'.body', **kwargs)) + if 'MAIL_SERVER' not in config: + return + s = smtplib.SMTP(config['MAIL_SERVER']) + s.send_message(msg) + s.quit() except: traceback.print_exc() - return - if 'MAIL_SERVER' not in config: - return - s = smtplib.SMTP(config['MAIL_SERVER']) - s.send_message(msg) - s.quit() def notify_users(msgtype, uids, **kwargs): recipients = [] exclude = kwargs.pop('exclude_uids', []) for uid in uids: user = query('SELECT * FROM users WHERE id = ?', uid) - if user[0]['id'] in exclude: + if not user or user[0]['id'] in exclude: continue - if not user or not user[0]['fsacc'] or not user[0]['mail_notifications']: + if not user[0]['fsacc'] or not user[0]['mail_notifications']: continue if msgtype in user[0] and not user[0][msgtype]: continue @@ -39,14 +38,17 @@ def notify_users(msgtype, uids, **kwargs): config['MAIL_SUFFIX'])) else: recipients.append('%s@%s'%(user[0]['fsacc'], config['MAIL_SUFFIX'])) - if recipients: - if kwargs.pop('importend', False): - kwargs['cc'] = [config['MAIL_DEFAULT']] - else: - if kwargs.pop('importend', False): - recipients = [config['MAIL_DEFAULT']] - else: - return + cc = kwargs.get('cc', []) + if kwargs.pop('importend', False): + cc.append(config['MAIL_DEFAULT']) + if kwargs.pop('notify_admins', False): + cc.append(config['MAIL_ADMINS']) + if not recipients: + recipients = cc + cc = [] + if not recipients: + return + kwargs['cc'] = cc send_message(msgtype, recipients, **kwargs) def notify_mods(msgtype, course_id, **kwargs): @@ -63,5 +65,4 @@ def notify_admins(msgtype, **kwargs): send_message(msgtype, [config['MAIL_ADMINS']], **kwargs) except: traceback.print_exc() - pass