예제 #1
0
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)
예제 #2
0
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)
예제 #3
0
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)
예제 #4
0
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')
예제 #5
0
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)
예제 #6
0
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
                                              }})
예제 #7
0
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)
예제 #8
0
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)