1. IMAP 설정 : https://support.google.com/mail/answer/7126229?hl=ko&rd=3&visit_id=1-636281811566888160-3239280507#ts=1665018
2. 보안수준 설정 : https://support.google.com/accounts/answer/6010255
3. settings.py
-------------------------------------------------------------------------------
# SMTP Settings
EMAIL_HOST = "smtp.gmail.com"
# 메일을 호스트하는 서버
EMAIL_PORT = "587"
# gmail과의 통신하는 포트
EMAIL_HOST_USER = "********@gmail.com"
# 발신할 이메일
EMAIL_HOST_PASSWORD = "*******"
# 발신할 메일의 비밀번호
EMAIL_USE_TLS = True
# TLS 보안 방법
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
# 사이트와 관련한 자동응답을 받을 이메일 주소
4. 테스트 해보자
(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\Blog>python manage.py shell
-- plain text message (default mime type -> "text/plain")
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('title', '<b>park</b>', to=['********@nate.com'])
>>> email.send()
1
-- html message (mime type change -> "text/html")
>>> email = EmailMessage('title', '<b>park</b>', to=['********@nate.com'])
>>> email.content_subtype = "html"
>>> email.send()
1
-- plain text + html message (To send a text and HTML combination) --> 메일수신자가 처리할 수 있다면 html형식으로 보여준다.
>>> from django.core.mail import EmailMultiAlternatives
>>> email = EmailMultiAlternatives('title', '<b>park</b>', to=['********@nate.com'])
>>> email.attach_alternative("<a href='naver.com'>naver.com</a>", "text/html")
>>> email.send()
1
# django에서 비밀번호 초기화 메일이 기본적으로 text/plain 으로 전송되는데 text/html로 전송하고 싶다면 아래와 같이 수정한다.
1. Blog\BlogVenv\Lib\site-packages\django\contrib\auth\views.py
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
#html_email_template_name = None
html_email_template_name = 'registration/password_reset_email_html.html' # html용 템플릿 추가로 만들어 지정
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_form.html'
title = _('Password reset')
token_generator = default_token_generator
2. mail template 을 만든다.
/home/webapp/blog.daonelab.com/BlogVenv/lib/python3.6/site-packages/django/contrib/admin/templates/registration/password_reset_email_html.html
{% load i18n %}{% autoescape off %}
<br/>{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
<br/>
<br/>{% trans "Please go to the following page and choose a new password:" %}
<br/>{% block reset_link %}
<br/>{% trans "Password Reset Link:" %} <a href="{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}">{{ domain }}</a>
<br/>{% endblock %}
<br/>{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
<br/>
<br/>{% trans "Thanks for using our site!" %}
<br/>
<br/>{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}