发送邮件 :return: 是否发送成功 :exception 发送失败异常
(self)
| 1106 | return True |
| 1107 | |
| 1108 | def send(self): |
| 1109 | """ |
| 1110 | 发送邮件 |
| 1111 | :return: 是否发送成功 |
| 1112 | :exception 发送失败异常 |
| 1113 | """ |
| 1114 | email = self.data.get("email") |
| 1115 | state = self.data.get("type") |
| 1116 | # 生成随机验证码 |
| 1117 | code = "".join(list(map(lambda i: random.choice(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0' |
| 1118 | ]), range(6)))) |
| 1119 | # 获取邮件模板 |
| 1120 | language = get_language() |
| 1121 | file = open( |
| 1122 | os.path.join(PROJECT_DIR, "apps", "common", 'template', f'email_template_{language}.html'), "r", |
| 1123 | encoding='utf-8') |
| 1124 | content = file.read() |
| 1125 | file.close() |
| 1126 | code_cache_key = email + ":" + state |
| 1127 | code_cache_key_lock = code_cache_key + "_lock" |
| 1128 | # 设置缓存 |
| 1129 | cache.set(get_key(code_cache_key_lock), code, timeout=60, version=version) |
| 1130 | system_setting = QuerySet(SystemSetting).filter(type=SettingType.EMAIL.value).first() |
| 1131 | if system_setting is None: |
| 1132 | cache.delete(get_key(code_cache_key_lock), version=version) |
| 1133 | raise AppApiException(1004, |
| 1134 | _("The email service has not been set up. Please contact the administrator to set up the email service in [Email Settings].")) |
| 1135 | try: |
| 1136 | connection = EmailBackend(system_setting.meta.get("email_host"), |
| 1137 | system_setting.meta.get('email_port'), |
| 1138 | system_setting.meta.get('email_host_user'), |
| 1139 | system_setting.meta.get('email_host_password'), |
| 1140 | system_setting.meta.get('email_use_tls'), |
| 1141 | False, |
| 1142 | system_setting.meta.get('email_use_ssl') |
| 1143 | ) |
| 1144 | # 发送邮件 |
| 1145 | send_mail(_('【Intelligent knowledge base question and answer system-{action}】').format( |
| 1146 | action=_('User registration') if state == 'register' else _('Change password')), |
| 1147 | '', |
| 1148 | html_message=f'{content.replace("${code}", code)}', |
| 1149 | from_email=system_setting.meta.get('from_email'), |
| 1150 | recipient_list=[email], fail_silently=False, connection=connection) |
| 1151 | except Exception as e: |
| 1152 | cache.delete(get_key(code_cache_key_lock)) |
| 1153 | return True |
| 1154 | cache.set(get_key(code_cache_key), code, timeout=60 * 30, version=version) |
| 1155 | return True |
| 1156 | |
| 1157 | |
| 1158 | class CheckCodeSerializer(serializers.Serializer): |