django

How to Set Up Email Functionality in Django?

Setting up email functionality in Django involves configuring the email backend and creating views and forms for email sending. You can use Django's built-in EmailMessage class to compose and send emails. Here is a basic example:
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.example.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@example.com'
EMAIL_HOST_PASSWORD = 'your-email-password'
# views.py
from django.core.mail import send_mail
from django.http import HttpResponse
def send_email(request):
subject = 'Welcome to My Site'
message = 'Thank you for signing up for our newsletter!'
from_email = 'your-email@example.com'
recipient_list = ['recipient@example.com']
send_mail(subject, message, from_email, recipient_list)
return HttpResponse('Email sent successfully')

Frequently asked queries:

Cities We Serve