Django – send_mail SMTP 메일 보내기

장고 기반의 프로젝트에서 이메일로 처리해야 할 일이 제법 있다.

가장 간단하게는 비밀번호를 잊었을 때 복구 링크 보내주는 것이 대표적인 예로 볼 수 있다.

장고 문서 공식 사이트에서는, 아래 링크에서 정보를 찾을 수 있다.

https://docs.djangoproject.com/en/4.1/topics/email/

settings.py

아래 링크에 가면 아주 자세하게 설정해서 보내는 샘플 프로젝트가 잘 되어 있다.

https://opensource.com/article/22/12/django-send-emails-smtp

Scroll to the end of the code and update the file with the following code:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.yourserver.com'
EMAIL_USE_TLS = False
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = 'your@djangoapp.com'
EMAIL_HOST_PASSWORD = 'your password'

send_mail()

함수 호출은 단순하다.

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)
email, newsletter, email marketing
sending email