diff --git a/INSTALLATION.md b/INSTALLATION.md index f6a09007c14eaa5736fedb3ce6752ba901b2db4a..cfc757467c4912a48b50003b8eeb20b950d0e30f 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -2,7 +2,7 @@ Boolean type variables use consider `true` and `yes` as truthy, and `false` and `no` as falsy. -- `INJECT_DEBUG`: _boolean, default=false_ - Run the backend in debug mode. If set, email sending is disabled. **Do not set in production.** +- `INJECT_DEBUG`: _boolean, default=false_ - Run the backend in debug mode. If set, email sending is disabled and emails are printed in the console. **Do not set in production.** - `INJECT_HOST_ADDRESSES`: _string, default=localhost_ - A comma-separated list of allowed hosts. - `INJECT_CORS_ALLOWED_ORIGINS`: _string, default=http://localhost_ - A comma-separated list of origins (domains) that are authorized to make cross-site HTTP requests. - `INJECT_EMAIL_HOST`: _string, default=""_ - SMTP server address. diff --git a/ttxbackend/settings.py b/ttxbackend/settings.py index aa47f93f9bb609829ed16f5e04f51e91eb424b54..59381aaae83e4cae6522f719feda95c54fa506a1 100644 --- a/ttxbackend/settings.py +++ b/ttxbackend/settings.py @@ -235,7 +235,11 @@ SESSION_EXPIRE_AT_BROWSER_CLOSE = True AUTHENTICATION_BACKENDS = ["aai.backend.CustomAuthBackend"] # Email client -EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" +EMAIL_BACKEND = ( + "django.core.mail.backends.console.EmailBackend" + if DEBUG + else "django.core.mail.backends.smtp.EmailBackend" +) EMAIL_HOST = os.environ.get("INJECT_EMAIL_HOST", "") if email_port := os.environ.get("INJECT_EMAIL_PORT"): EMAIL_PORT = int(email_port) diff --git a/user/email/email_sender.py b/user/email/email_sender.py index af7a1ed9ea311c4bbf14b45c7bfcd7a9e44a6013..82afb3e4427eb87e7069b2f0e59ee0ce2b8f007b 100644 --- a/user/email/email_sender.py +++ b/user/email/email_sender.py @@ -7,7 +7,7 @@ from email.mime.image import MIMEImage from common_lib.logger import logger from user.models import User -from ttxbackend.settings import EMAIL_SENDER_ADDRESS, DOMAIN +from ttxbackend.settings import EMAIL_SENDER_ADDRESS, DOMAIN, DEBUG def send_credentials(new_users: List[Tuple[User, str]]): @@ -27,26 +27,33 @@ def send_credentials(new_users: List[Tuple[User, str]]): message = EmailMultiAlternatives( subject, text_content, EMAIL_SENDER_ADDRESS, [user.username] ) - # attach logo - fp = open("user/templates/inject-logo.png", "rb") - msg_image = MIMEImage(fp.read()) - fp.close() - msg_image.add_header("Content-ID", "<logo-image>") - msg_image.add_header( - "Content-Disposition", "attachment; filename=inject-logo.png" - ) - message.mixed_subtype = "related" - message.attach(msg_image) + if not DEBUG: + # attach logo + fp = open("user/templates/inject-logo.png", "rb") + msg_image = MIMEImage(fp.read()) + fp.close() + msg_image.add_header("Content-ID", "<logo-image>") + msg_image.add_header( + "Content-Disposition", "attachment; filename=inject-logo.png" + ) + message.mixed_subtype = "related" + message.attach(msg_image) - # attach HTML content - message.attach_alternative(html_content, "text/html") + # attach HTML content + message.attach_alternative(html_content, "text/html") messages.append(message) try: connection = get_connection(fail_silently=False) - connection.send_messages(messages) + num_sent = connection.send_messages(messages) + logger.info( + f"{num_sent} emails with credentials were " + + ("printed to console" if DEBUG else "sent via SMTP server") + ) except Exception as e: - logger.error(f"Failed to send credentials via SMTP server: {e}") + logger.error( + f"Failed to send emails with credentials via SMTP server: {e}" + ) def send_password_change_notification(user: User):