HOW to send authentication number by e-mail
I want to send an 8-digit authentication number by e-mail.
No matter how hard I try, I don't know what I'm doing wrong.
First, I set the smtp. Then, I filled out the form. After creating the view, I created a template.
After entering e-mail and username, I pressed the Send button.
WHAT? id:
email: [29/Mar/2022 09:43:39] "GET /recovery/pw/?email=wlgns000%40gmail.com&username=wlgns HTTP/1.1" 200 2030However, only these logs appear, and the authentication number does not come by e-mail.
How can I send an authentication number by e-mail?
#settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.naver.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = #email
EMAIL_HOST_PASSWORD = #password
EMAIL_USE_TLS = True
DEFAULT_FROM_MAIL = EMAIL_HOST_USER
{% load static %}
<!DOCTYPE html>
<html lang="KO">
<head>
....
<link href="{% static 'users/css/recovery_pw.css' %}" rel="stylesheet">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="{% static 'users/js/recovery_pw.js' %}"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<script>
$.ajaxSetup({
headers: { "X-CSRFToken": '{{csrf_token}}' }
});
</script>
</head>
<body>
{% block content %}
<form method="get" enctype="multipart/form-data">
{% csrf_token %}
<div class="container">
<div class="inner-box">
...
<div class="input-box">
<div class="id">
<input type="email" placeholder="등록하신 메일로 인증번호가 발송됩니다." name="email" maxlenth="20" autocomplete="off" value="{{ form.email.value|default_if_none:'' }}" required />
</div>
<div class="password">
<input type="username" placeholder="아이디를 입력하세요" name="username" maxlength="20" value="{{ form.username.value|default_if_none:'' }}" required />
</div>
</div>
<div class="btn">
<div class="btn-white" id="btn_white"><button type="submit">send mail</button></div>
</div>
...
</form>
{% endblock %}
</body>
</html>
# views.py
class RecoveryPwView(View):
template_name = 'users/recovery_pw.html'
recovery_pw = RecoveryPwForm
def get(self, request):
if request.method=='GET':
print("here?")
form = self.recovery_pw(None)
return render(request, self.template_name, { 'form':form, })
@csrf_exempt
def ajax_find_pw_view(request):
username = request.POST.get('username')
email = request.POST.get('email')
target_user = User.objects.get(username=username, email=email)
if target_user:
auth_num = email_auth_num()
print(auth_num, "1111")
target_user.auth = auth_num
print(target_user.auth, "2222")
target_user.save()
send_mail(
'email',
['#email'],
html=render_to_string('users/recovery_email.html', {
'auth_num': auth_num,
}),
)
print(send_mail, "3333")
return HttpResponse(json.dumps({"result": target_user.username}, cls=DjangoJSONEncoder), content_type = "application/json")
#forms.py
class RecoveryPwForm(forms.Form):
username = forms.CharField(label='id')
email = forms.EmailField(label='email')
class Meta:
fields = ['username', 'email']
and last email.html It is an HTML containing the authentication number sent by e-mail.
{% autoescape off %}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="{% static 'users/css/recovery_email.css' %}" rel="stylesheet">
<link href="{% static 'users/css/default.css' %}" rel="stylesheet">
<script>
$.ajaxSetup({
headers: { "X-CSRFToken": '{{csrf_token}}' }
});
</script>
</head>
<body>
...
<div class="auth"><p class="auth-p">{{ auth_num }}</p></div>
...
</body>
</html>
{% endautoescape %}
