def send_error_notification_mail(name, email, subject, message):
    message = loader.render_to_string(
        'mail/send_error_notification_mail.html',
        {
            'name': name,
            'message': message
        }
    )
    if settings.DEBUG:
        logger.debug(message)

    send_ses_email.delay(RECIPIENT=email, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_reset_ssh_key_mail(user):
    subject = _('SSH Key Reset')
    recipient_list = user.email
    message = loader.render_to_string(
        'mail/send_reset_ssh_key_mail.html',
        {
            'name': user.name,
            'login_url': reverse('authentication:login', external=True),
        }
    )
    if settings.DEBUG:
        logger.debug(message)

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_user_expiration_reminder_mail(user):
    subject = _('Expiration notice')
    recipient_list = user.email
    message = loader.render_to_string(
        '',
        {
            'name': user.name,
            'date_expired': datetime.fromtimestamp(datetime.timestamp(
                user.date_expired)).strftime('%Y-%m-%d %H:%M'),
        }
    )
    if settings.DEBUG:
        logger.debug(message)

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_reset_password_mail(user):
    subject = _('Reset password')
    recipient_list = user.email
    message = loader.render_to_string(
        'mail/reset_password_mail.html',
        {
            'name': user.name,
            'rest_password_url': "reverse('users:reset-password', external=True)",
            'rest_password_token': 'user.generate_reset_token()',
            'forget_password_url': "reverse('users:forgot-password', external=True)",
            'email': user.email,
            'login_url': "reverse('authentication:login', external=True)",
        }
    )
    if settings.DEBUG:
        logger.debug(message)

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_user_created_mail(user):
    recipient_list = user.email
    subject = _('Create account successfully')
    if settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT:
        subject = settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT

    message = loader.render_to_string(
        'mail/user_created_mail.html',
        {
            'user': user,
        }
    )
    if settings.DEBUG:
        try:
            print(message)
        except OSError:
            logger.debug('OS Error')

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_password_expiration_reminder_mail(user):
    subject = _('Security notice')
    recipient_list = user.email
    message = loader.render_to_string(
        'mail/send_password_expiration_reminder_mail.html',
        {
            'name': user.name,
            'date_password_expired': datetime.fromtimestamp(datetime.timestamp(
                user.date_password_expired)).strftime('%Y-%m-%d %H:%M'),
            'update_password_url': reverse('users:user-password-update', external=True),
            'forget_password_url': reverse('users:forgot-password', external=True),
            'email': user.email,
            'login_url': reverse('authentication:login', external=True),
        }
    )
    if settings.DEBUG:
        logger.debug(message)

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_user_code_mail(email, code):
    """发送电子邮件验证码"""
    recipient_list = email
    subject = _('Security Code')
    signature = settings.EMAIL_CUSTOM_USER_CREATED_SIGNATURE
    message = loader.render_to_string(
        'mail/send_user_code.html',
        {
            'subject': subject,
            'recipient': email,
            'signature': signature,
            'code': code
        }
    )
    if settings.DEBUG:
        try:
            print(message)
        except OSError:
            logger.debug('OS Error')

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)
def send_user_registered_mail(user):
    recipient_list = user.email
    subject = _('Create account successfully')
    if settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT:
        subject = settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT

    message = loader.render_to_string(
        'mail/user_registered_mail.html',
        {
            'user': user,
            'confirm_verification': reverse('account:confirm-verification', kwargs={
                'token': user.generate_reset_token()
            }, external=True),
        }
    )
    if settings.DEBUG:
        try:
            print(message)
        except OSError:
            logger.debug('OS Error')

    send_ses_email.delay(RECIPIENT=recipient_list, SUBJECT=subject, BODY_TEXT=message, BODY_HTML=message)