django gmail smtp
To use Django's Gmail SMTP for sending emails, follow these steps:
- Import the necessary modules:
- Import the
send_mail
function fromdjango.core.mail
. Import the
settings
module fromdjango.conf
.Configure the email settings:
- Set the
EMAIL_BACKEND
setting in thesettings.py
file to'django.core.mail.backends.smtp.EmailBackend'
. - Set the
EMAIL_HOST
setting to'smtp.gmail.com'
. - Set the
EMAIL_PORT
setting to587
. - Set the
EMAIL_HOST_USER
setting to your Gmail email address. - Set the
EMAIL_HOST_PASSWORD
setting to your Gmail password. Set the
EMAIL_USE_TLS
setting toTrue
.Use the
send_mail
function to send an email:- Pass the subject of the email as the first argument.
- Pass the body of the email as the second argument.
- Pass the sender's email address as the third argument.
- Pass a list of recipient email addresses as the fourth argument.
Optionally, pass a list of email addresses to receive a copy of the email as the
fail_silently
parameter.Handle any exceptions that may occur during the email sending process.