From e6c0c0af7da0f8d020bb80cc171b308fdfb67d56 Mon Sep 17 00:00:00 2001 From: julio Date: Sat, 25 May 2024 16:56:18 +0200 Subject: [PATCH] #153 - environment variable to enable/disable email sending --- .env.template | 1 + appmes/settings.py | 2 ++ helpers/mailing.py | 24 +++++++++++-------- market/management/commands/send_test_email.py | 1 + 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.env.template b/.env.template index 41a72b3..cc9b8f7 100644 --- a/.env.template +++ b/.env.template @@ -30,5 +30,6 @@ EMAIL_HOST_USER= EMAIL_HOST_PASSWORD= EMAIL_USE_TLS=True EMAIL_SEND_FROM= +ENABLE_EMAIL_SENDING= SENTRY_DSN= SENTRY_ENV= \ No newline at end of file diff --git a/appmes/settings.py b/appmes/settings.py index 6dad681..4454137 100644 --- a/appmes/settings.py +++ b/appmes/settings.py @@ -302,6 +302,8 @@ EMAIL_USE_TLS = env('EMAIL_USE_TLS') EMAIL_SEND_FROM = env('EMAIL_SEND_FROM') +ENABLE_EMAIL_SENDING = env('ENABLE_EMAIL_SENDING') + # ======= Sentry Configuration ======== sentry_sdk.init( dsn=env('SENTRY_DSN'), diff --git a/helpers/mailing.py b/helpers/mailing.py index c5aac09..fcaad08 100644 --- a/helpers/mailing.py +++ b/helpers/mailing.py @@ -4,13 +4,17 @@ def send_template_email(title, destination, template_name, template_params): - msg_plain = render_to_string('email/%s.txt' % template_name, template_params) - msg_html = render_to_string('email/%s.html' % template_name, template_params) - send_mail( - title, - msg_plain, - settings.EMAIL_SEND_FROM, - [destination], - html_message=msg_html, - fail_silently=False - ) + + if settings.ENABLE_EMAIL_SENDING: + msg_plain = render_to_string('email/%s.txt' % template_name, template_params) + msg_html = render_to_string('email/%s.html' % template_name, template_params) + send_mail( + title, + msg_plain, + settings.EMAIL_SEND_FROM, + [destination], + html_message=msg_html, + fail_silently=False + ) + else: + print("Email sending is disabled") diff --git a/market/management/commands/send_test_email.py b/market/management/commands/send_test_email.py index 8fb7eca..b512f9c 100644 --- a/market/management/commands/send_test_email.py +++ b/market/management/commands/send_test_email.py @@ -25,4 +25,5 @@ def handle(self, *args, **options): ) except Exception as e: + print("error") print(e)