Django에서 이메일 보내기

Django에서 이메일을 보내기 위한 구성:-

YouTube에서 내 동영상 보기



Gmail 계정의 설정




  • 브라우저에서 이메일 계정 열기
  • 'Google 계정 관리'
  • 로 이동합니다.
  • 왼쪽 사이드바의 '보안' 탭으로 이동합니다
  • .
  • 보안 탭의 '보안 수준이 낮은 앱 액세스' 옵션에서 토글 버튼을 켭니다.

  • 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>
    

    좋은 웹페이지 즐겨찾기