Skip to content
Snippets Groups Projects
Select Git revision
  • d26473c57b3e84f441688cfe26469cef1a3f9216
  • master default protected
2 results

handler.ts

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    mail.py 1.87 KiB
    from server import *
    
    from email.message import EmailMessage
    import smtplib
    import traceback
    
    def send_message(msgtype, recipients, **kwargs):
    	msg = EmailMessage()
    	msg['From'] = config['MAIL_FROM']
    	msg['To'] = ', '.join([r.replace(',', '') for r in recipients])
    	cc = kwargs.pop('cc', [])
    	if cc:
    		msg['Cc'] = ', '.join([r.replace(',', '') for r in cc])
    	try:
    		msg['Subject'] = render_template('mails/'+msgtype+'.subject', **kwargs)
    		msg.set_content(render_template('mails/'+msgtype+'.body', **kwargs))
    	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:
    			continue
    		if not user or not user[0]['fsacc'] or not user[0]['mail_notifications']:
    			continue
    		if msgtype in user[0] and not user[0][msgtype]:
    			continue
    		if user[0]['realname']:
    			recipients.append('%s <%s@%s>'%(user[0]['realname'], user[0]['fsacc'],
    						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
    	send_message(msgtype, recipients, **kwargs)
    
    def notify_mods(msgtype, course_id, **kwargs):
    	users = query('SELECT * FROM responsible WHERE course_id = ?', course_id)
    	uids = []
    	for user in users:
    		if msgtype in user and not user[msgtype]:
    			continue
    		uids.append(user['user_id'])
    	notify_users(msgtype, uids, **kwargs)
    
    def notify_admins(msgtype, **kwargs):
    	try:
    		send_message(msgtype, [config['MAIL_ADMINS']], **kwargs)
    	except:
    		traceback.print_exc()
    		pass