Django 이메일 양식 사용하기

HS L·2023년 6월 26일
0

내일배움캠프

목록 보기
71/73

Django html을 이용한 이메일 보내기

기본설정은 이전 글 참고
django 이메일 보내기 설정

공식문서와 검색글을 보면 다양하게 사용을 할 수 있다. 그중 사용한 법을 기록한다.

작성 코드

작성한 코드

from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags


def send_emoticon_registration_email(recipient_email, subject, emoticon):
    """
    안내 이메일
    """
    html_message = render_to_string(
        "emoticon_registration.html",
        {"username": emoticon.creator, "title": emoticon.title},
    )
    plain_message = strip_tags(html_message)
    send_mail(
        subject,
        plain_message,
        "datescape2306@gmail.com",
        [recipient_email],
        html_message=html_message,
    )
# 기본 폼
def send_notification_email(recipient_email, subject, message):
    html_message = render_to_string('email_template.html', {'message': message})  # HTML 템플릿 렌더링
    plain_message = strip_tags(html_message)  # HTML 태그 제거

    send_mail(subject, plain_message, 'sender@example.com', [recipient_email], html_message=html_message)

메일전송, html렌더, html태그제거를 위해 필요한 것들을 import한다.
사용할 작성코드로 커스텀 하기 전 기본 폼을 보면 recipient_email은 수신할 메일주소, subject는 메일 제목, message는 html렌더링시에 전달할 내용이다.

<!-- emoticon_registration.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>title</title>
</head>

<body>
    <a href="https://datescape.shop/">
        <img src="https://datescape.shop/static/images/waterlogo.png" alt="" style="margin-bottom: 30px;">
    </a>
    <br>
    <p>안녕하세요. "{{username}}"님! 신청하신 "{{title}}" 이모티콘의 상품 등록이 완료되었습니다!</p>
</body>

</html>

메일을 보내게 될 양식이다. 파일의 위치는 manage.py와 같은 경로상에 templates폴더에 만들어 준다.
html렌더링시에 전달받은 변수사용이 가능하고 입맛대로 커스텀 하면 된다.

profile
식이

0개의 댓글