def mail_member_del(sender): ''' mail member del ''' tplenv = Environment(loader=FileSystemLoader('./templates/mail')) template = tplenv.get_template('./base_member_del.html') team_member_change_db = TeamMemberChangedDB() awsses = AWSSES( aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=setting.AWS_SES_FROM) for raw in team_member_change_db.find( {'done.mail': {'$exists': False}, 'case': 'del'}, sort=(('create_at', 1), )): team = Team.get(raw['pid'], raw['tid']) user = User.get_info(uids=(raw['uid'], ))[raw['uid']] body = template.render( name=user['profile']['badge_name'], team_name=team['name'], ) raw_mail = awsses.raw_mail( to_addresses=( dict(name=user['profile']['badge_name'], mail=user['oauth']['email']), ), subject=f"您已被移除 {team['name']} 的組員資格!", body=body, ) resp = mail_member_send.apply_async( kwargs={'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id'])}) logger.info(resp)
def mail_member_welcome(sender, **kwargs): TPLENV = Environment(loader=FileSystemLoader('./templates/mail')) template = TPLENV.get_template('./welcome.html') awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=setting.AWS_SES_FROM) uids = [] for u in MailLetterDB().need_to_send(code='welcome'): uids.append(u['_id']) if not uids: return service_sync_mattermost_invite.apply_async(kwargs={'uids': uids}) users = User.get_info(uids=uids) for uid in uids: logger.info('uid: %s' % uid) body = template.render(name=users[uid]['profile']['badge_name'], ) raw_mail = awsses.raw_mail( to_addresses=(dict(name=users[uid]['profile']['badge_name'], mail=users[uid]['oauth']['email']), ), subject=u'歡迎使用志工服務系統 - %s' % users[uid]['profile']['badge_name'], body=body, ) resp = awsses.send_raw_email(data=raw_mail) logger.info(resp) if resp['ResponseMetadata']['HTTPStatusCode'] != 200: raise Exception('HTTPStatusCode not `200`, do retry') MailLetterDB().make_sent(uid=uid, code='welcome')
def on_failure(**kwargs): ses = AWSSES(setting.AWS_ID, setting.AWS_KEY, setting.AWS_SES_FROM) raw_mail = ses.raw_mail( to_addresses=[setting.ADMIN_To, ], subject='[COSCUP-SECRETARY] %s [%s]' % (kwargs['sender'].name, kwargs['sender'].request.id), body='kwargs: <pre>%s</pre><br>einfo: <pre>%s</pre><br>request: <pre>%s</pre>' % ( kwargs['kwargs'], kwargs['einfo'].traceback, kwargs['sender'].request), ) ses.send_raw_email(data=raw_mail)
def mail_sys_weberror(sender, **kwargs): ''' mail sys weberror ''' ses = AWSSES(setting.AWS_ID, setting.AWS_KEY, setting.AWS_SES_FROM) raw_mail = ses.raw_mail( to_addresses=[setting.ADMIN_To, ], subject=f"[COSCUP-SECRETARY] {kwargs['title']}", body=kwargs['body'], ) ses.send_raw_email(data=raw_mail)
def mail_member_waiting(sender): ''' mail member waiting ''' template = Environment(loader=FileSystemLoader( './templates/mail')).get_template('./base_member_waiting.html') team_member_change_db = TeamMemberChangedDB() awsses = AWSSES( aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=setting.AWS_SES_FROM) for raw in team_member_change_db.find( {'done.mail': {'$exists': False}, 'case': 'waiting'}, sort=(('create_at', 1), )): team = Team.get(raw['pid'], raw['tid']) uids = [] uids.extend(team['chiefs']) uids.append(raw['uid']) users = User.get_info(uids=uids) mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN, base_url=setting.MATTERMOST_BASEURL) for uid in team['chiefs']: body = template.render( name=users[uid]['profile']['badge_name'], uid=raw['uid'], apply_name=users[raw['uid']]['profile']['badge_name'], team_name=team['name'], pid=team['pid'], tid=team['tid'], ) raw_mail = awsses.raw_mail( to_addresses=(dict( name=users[uid]['profile']['badge_name'], mail=users[uid]['oauth']['email']), ), subject=f"申請加入通知信 - {users[raw['uid']]['profile']['badge_name']}", body=body, ) resp = mail_member_send.apply_async( kwargs={'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id'])}) logger.info(resp) mid = mmt.find_possible_mid(uid=uid) if mid: channel_info = mmt.create_a_direct_message( users=(mid, setting.MATTERMOST_BOT_ID)).json() resp = mmt.posts( channel_id=channel_info['id'], message=f"收到 **{users[raw['uid']]['profile']['badge_name']}** 申請加入 **{team['name']}**,前往 [管理組員](https://volunteer.coscup.org/team/{team['pid']}/{team['tid']}/edit_user)", # pylint: disable=line-too-long ) logger.info(resp.json())
def __init__(self, template_path, subject, content, source=None): body = SandboxedEnvironment().from_string( open(template_path, 'r').read()).render(**content) self.tpl = SandboxedEnvironment().from_string(body) self.subject = SandboxedEnvironment().from_string(subject) if source is None: source = setting.AWS_SES_FROM self.awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=source)
def on_failure(**kwargs): ''' on failure ''' ses = AWSSES(setting.AWS_ID, setting.AWS_KEY, setting.AWS_SES_FROM) raw_mail = ses.raw_mail( to_addresses=[ setting.ADMIN_To, ], subject= f"[COSCUP-SECRETARY] {kwargs['sender'].name} [{kwargs['sender'].request.id}]", body=f"""kwargs: <pre>{kwargs['kwargs']}</pre><br> einfo: <pre>{kwargs['einfo'].traceback}</pre><br> request: <pre>{kwargs['sender'].request}</pre>""", ) ses.send_raw_email(data=raw_mail)
class SenderMailer: ''' Sender Mailer Args: template_path (str): Mail template path. subject (str): Mail subject. source (dict): Mail `FROM`, `{'name': <name>, 'mail': <mail>}`. ''' def __init__(self, template_path: str, subject: str, content: dict[str, Any], source: Optional[dict[str, str]] = None) -> None: with open(template_path, 'r', encoding='UTF8') as files: body = SandboxedEnvironment().from_string(files.read()).render(**content) self.tpl = SandboxedEnvironment().from_string(body) self.subject = SandboxedEnvironment().from_string(subject) if source is None: source = setting.AWS_SES_FROM if 'text_body' in content and content['text_body']: self.text_body = SandboxedEnvironment( ).from_string(content['text_body']) self.awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=source) def send(self, to_list: list[dict[str, str]], data: dict[str, Any], x_coscup: Optional[str] = None) -> Any: ''' Send mail Args: to_list (list): List of user/mail in `{'name': <name>, 'mail': <mail>}`. data (dict): Mail datas. More details: [module.awsses.AWSSES.raw_mail][]. Returns: More details: [module.awsses.AWSSES.send_raw_email][]. ''' raw_mail = self.awsses.raw_mail( to_addresses=to_list, subject=self.subject.render(**data), body=self.tpl.render(**data), text_body=self.text_body.render(**data), x_coscup=x_coscup, list_unsubscribe=setting.AWS_LIST_UNSUBSCRIBE, ) return self.awsses.send_raw_email(data=raw_mail)
def mail_member_send(sender, **kwargs): team_member_change_db = TeamMemberChangedDB() awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=setting.AWS_SES_FROM) resp = awsses.send_raw_email(data_str=kwargs['raw_mail']) logger.info(resp) if resp['ResponseMetadata']['HTTPStatusCode'] != 200: raise Exception('HTTPStatusCode not `200`, do retry') team_member_change_db.find_one_and_update({'_id': ObjectId(kwargs['rid'])}, {'$set': { 'done.mail': True }})
def __init__(self, template_path: str, subject: str, content: dict[str, Any], source: Optional[dict[str, str]] = None) -> None: with open(template_path, 'r', encoding='UTF8') as files: body = SandboxedEnvironment().from_string(files.read()).render(**content) self.tpl = SandboxedEnvironment().from_string(body) self.subject = SandboxedEnvironment().from_string(subject) if source is None: source = setting.AWS_SES_FROM if 'text_body' in content and content['text_body']: self.text_body = SandboxedEnvironment( ).from_string(content['text_body']) self.awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=source)
def mail_member_add(sender, **kwargs): TPLENV = Environment(loader=FileSystemLoader('./templates/mail')) template = TPLENV.get_template('./base_member_add.html') team_member_change_db = TeamMemberChangedDB() awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=setting.AWS_SES_FROM) for raw in team_member_change_db.find( { 'done.mail': { '$exists': False }, 'case': 'add' }, sort=(('create_at', 1), )): team = Team.get(raw['pid'], raw['tid']) user = User.get_info(uids=(raw['uid'], ))[raw['uid']] body = template.render( name=user['profile']['badge_name'], team_name=team['name'], pid=team['pid'], tid=team['tid'], ) raw_mail = awsses.raw_mail( to_addresses=(dict(name=user['profile']['badge_name'], mail=user['oauth']['email']), ), subject=u'申請加入 %s 核准' % team['name'], body=body, ) r = mail_member_send.apply_async(kwargs={ 'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id']) }) service_sync_mattermost_add_channel.apply_async(kwargs={ 'pid': raw['pid'], 'uids': (raw['uid'], ) }) logger.info(r)
def mail_tasks_star_one(sender, **kwargs): logger.info(kwargs) TPLENV = Environment(loader=FileSystemLoader('./templates/mail')) template = TPLENV.get_template('./tasks_star.html') awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=setting.AWS_SES_FROM) body = template.render(name=kwargs['user']['name'], task=kwargs['task']) raw_mail = awsses.raw_mail( to_addresses=(kwargs['user'], ), subject=u'有一筆新志工任務 - %s [%s]' % (kwargs['task']['title'], kwargs['task_id']), body=body, ) resp = awsses.send_raw_email(data=raw_mail) logger.info(resp)
class SenderMailer(object): ''' Sender Mailer :param str template_path: template path :param str subject: subject :param dict source: {'name': str, 'mail': str} ''' def __init__(self, template_path, subject, content, source=None): body = SandboxedEnvironment().from_string( open(template_path, 'r').read()).render(**content) self.tpl = SandboxedEnvironment().from_string(body) self.subject = SandboxedEnvironment().from_string(subject) if source is None: source = setting.AWS_SES_FROM self.awsses = AWSSES(aws_access_key_id=setting.AWS_ID, aws_secret_access_key=setting.AWS_KEY, source=source) def send(self, to_list, data, x_coscup=None): ''' Send mail :param list to_list: [{'name': str, 'mail': str}, ] :param dict data: data for render ''' raw_mail = self.awsses.raw_mail( to_addresses=to_list, subject=self.subject.render(**data), body=self.tpl.render(**data), x_coscup=x_coscup, ) return self.awsses.send_raw_email(data=raw_mail)