DevOps/Django

[Django] SENDGRID를 이용한 메일 보내기.

bestwish 2022. 11. 10. 22:37

1. API키 생성

https://app.sendgrid.com/settings/api_keys

 

SendGrid

 

app.sendgrid.com

위 사이트로 들어가서 API키를 생성한다.

 

2. 발신자 인증

https://app.sendgrid.com/settings/sender_auth

 

SendGrid

 

app.sendgrid.com

Sender Authentication(발신자 인증)에 들어가서 Verify a Single Sender(단일 발신자 확인)을 인증한다.

 

 

3.  Django 프로젝트에 적용하기.

3-1. 환경변수 설정 및 확인

  1. powershell에서 환경변수로 SENDGRID_API_KEY를 등록하기 위해 $Env:SENDGRID_API_KEY = "~~~"를 해준다.
  2. 장고 shell에 들어가 환경변수가 잘 등록 되었는지 확인한다.

3-2. sendgrid-django 설치

pip install sendgrid-django

위 명령어를 이용하여 설치 후 settings.py에 다음을 추가한다.

# Send Grid
SENDGRID_API_KEY = os.environ.get("SENDGRID_API_KEY")

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True

# 고정 Sender Email 추가
WELCOME_EMAIL_SENDER = "~~@naver.com"

주의 : DEBUG가 True인 경우에만 작동한다.

 

참고

https://docs.sendgrid.com/for-developers/sending-email/django

 

Send SMTP Email with Django

View instructions on how to easily send email with Django using SendGrid, by setting up setting up Django's built in mail library.

docs.sendgrid.com

https://pypi.org/project/sendgrid-django/

 

sendgrid-django

SendGrid Backend for Django

pypi.org