diff --git a/backend/backend/api/email.py b/backend/backend/api/email.py new file mode 100644 index 000000000..50a20074a --- /dev/null +++ b/backend/backend/api/email.py @@ -0,0 +1,22 @@ +from django.conf import settings +from django.core.mail import send_mail +from django.template.loader import render_to_string + +def send_email(subject, recipient_list, template_name, context): + """ + Send email via SMTP gateway through Django's email backend. + """ + # Load the template + email_html_message = render_to_string(template_name, context) + + # Get the DEFAULT_FROM_EMAIL from settings + default_from_email = getattr(settings, "DEFAULT_FROM_EMAIL") + + # Send the email + send_mail( + subject, + '', # plain text content can be empty as we're sending HTML + default_from_email, + recipient_list, + html_message=email_html_message + ) diff --git a/backend/backend/settings.py b/backend/backend/settings.py index 321257936..602334b42 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -121,6 +121,17 @@ OAUTH_REDIRECT_URI = os.getenv('OAUTH_REDIRECT_URI') + +# Email configurations +EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +EMAIL_HOST = os.getenv('SMTP_SERVER') +EMAIL_PORT = int(os.getenv('SMTP_PORT', 587)) +EMAIL_USE_TLS = True +EMAIL_HOST_USER = os.getenv('SMTP_USERNAME') +EMAIL_HOST_PASSWORD = os.getenv('SMTP_PASSWORD') +DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL') + + SITE_ID = 1 MIDDLEWARE = [