Django에서 이메일 보내기
YouTube에서 내 동영상 보기
Gmail 계정의 설정
settings.py
이 파일에 일부 코드 줄을 추가합니다.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS =True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your email id'
EMAIL_HOST_PASSWORD = 'password'
프로젝트/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
]
앱/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('send_mail/', views.sendMail, name='send_mail')
]
views.py
from django.shortcuts import render, redirect
from django.conf import settings
from django.core.mail import send_mail
from django.contrib import messages
# Create your views here.
def home(request):
return render(request, 'home_page.html')
def sendMail(request):
if request.method == 'POST':
sender = settings.EMAIL_HOST_USER
receiver = request.POST['receiver']
subject = request.POST['sub']
content = request.POST['content']
mail = send_mail(subject, content, sender, [receiver], fail_silently=False)
if mail:
messages.success(request, 'Email has been sent.')
return redirect('home')
else:
return HttpResponse('message not sent')
else:
return redirect('home')
템플릿/home.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<title>Sending Email in Django</title>
</head>
<body class="bg-dark text-light">
<div class="container my-3" style="width: 500px;">
<h2 class="alert alert-success">Sending Email in Django</h2>
<hr>
{% if messages %}
{% for message in messages %}
<div class="alert alert-dark alert-dismissible fade show my-4" role="alert">
{{message}}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
{% endif %}
<form action="{% url 'send_mail' %}" method="post">
{% csrf_token %}
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label"><b>To</label>
<input type="email" class="form-control" name="receiver" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="subject" class="form-label"><b>Subject</label>
<input type="text" class="form-control" name="sub" id="subject">
</div>
<div class="mb-3">
<label for="content" class="form-label">Body</label>
<textarea class="form-control" name="content" style="height: 250px;"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
</div>
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</body>
</html>
Reference
이 문제에 관하여(Django에서 이메일 보내기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/madhubankhatri/sending-emails-in-django-2b22텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)