diff --git a/INSTALLATION.md b/INSTALLATION.md index b5eda216a45bc5ccbeca7a0bb5339aec52121943..e56d59c7df399ff62fcc059fa2e2305c83d263ab 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -9,6 +9,7 @@ - `INJECT_EMAIL_HOST_USER`: _string, default=""_ - Username to use for authentication to the SMTP server defined in _INJECT_EMAIL_HOST_. - `INJECT_EMAIL_HOST_PASSWORD`: _string, default=""_ - Password to use for the SMTP server defined in _INJECT_EMAIL_HOST_. This setting is used in conjunction with _INJECT_EMAIL_HOST_USER_ when authenticating to the SMTP server. - `INJECT_EMAIL_SENDER_ADDRESS`: _string, default=""_ - The sender address for automatic emails. +- `INJECT_EMAIL_PROTOCOL`: _string, default=None_ - Optional variable for defining the preferred protocol for communication with SMTP server. The choices (values) can be _ssl_ or _tls_ (case insensitive). This variable can also be left undefined - no encryption will be used. - `INJECT_LOGS`: _string, default=backend-logs.log_ - Path to a file where to save logs. - `INJECT_DOMAIN`: _string, default=""_ - Domain where yours instance of the INJECT is available. - `INJECT_SECRET_KEY`: _string_ - Used to provide cryptographic signing. Must be at least 50 long characters string. diff --git a/ttxbackend/settings.py b/ttxbackend/settings.py index d4d2eda8b2af3af4fde3563289dcf080e073d600..66c973a83778f751be011f28543380ef12267593 100644 --- a/ttxbackend/settings.py +++ b/ttxbackend/settings.py @@ -229,10 +229,21 @@ else: EMAIL_PORT = 25 EMAIL_HOST_USER = os.environ.get("INJECT_EMAIL_HOST_USER", "") EMAIL_HOST_PASSWORD = os.environ.get("INJECT_EMAIL_HOST_PASSWORD", "") -EMAIL_USE_SSL = True EMAIL_SENDER_ADDRESS = os.environ.get("INJECT_EMAIL_SENDER_ADDRESS", "") DOMAIN = os.environ.get("INJECT_DOMAIN", "") +if protocol := os.environ.get("INJECT_EMAIL_PROTOCOL"): + formatted_protocol = protocol.lower().strip() + if formatted_protocol == "tls": + EMAIL_USE_TLS = True + elif formatted_protocol == "ssl": + EMAIL_USE_SSL = True + else: + raise Exception( + f"Invalid SMTP protocol '{protocol}'. " + f"Options are 'ssl', 'tls' or leave 'undefined' for unencrypted." + ) + # CORS settings CORS_ALLOW_CREDENTIALS = True CORS_ALLOWED_ORIGINS = [ diff --git a/user/email/email_sender.py b/user/email/email_sender.py index fe04bf0f3cc248547a27df5c0205e3ce5264c877..af7a1ed9ea311c4bbf14b45c7bfcd7a9e44a6013 100644 --- a/user/email/email_sender.py +++ b/user/email/email_sender.py @@ -4,6 +4,7 @@ from django.core.mail import get_connection, EmailMultiAlternatives, send_mail from django.template.loader import render_to_string from django.utils.html import strip_tags 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 @@ -41,8 +42,11 @@ def send_credentials(new_users: List[Tuple[User, str]]): message.attach_alternative(html_content, "text/html") messages.append(message) - connection = get_connection(fail_silently=True) - connection.send_messages(messages) + try: + connection = get_connection(fail_silently=False) + connection.send_messages(messages) + except Exception as e: + logger.error(f"Failed to send credentials via SMTP server: {e}") def send_password_change_notification(user: User): diff --git a/user/lib/user_uploader.py b/user/lib/user_uploader.py index d4df050f4af3440d065b7b302e28b9cc806877b7..006e2961f6b7f5e173bc1397c33ff82b430f6c19 100644 --- a/user/lib/user_uploader.py +++ b/user/lib/user_uploader.py @@ -15,6 +15,7 @@ from user.lib.user_validator import ( ) from user.models import User, Tag, UserTag from user.email.email_sender import send_credentials +from common_lib.logger import logger TAG_DELIMITER = "|" @@ -125,6 +126,9 @@ class UserUploader: if created_user is not None: created_users.append(created_user) + logger.info( + f"Users: {[new_user for new_user, _ in created_users]} were created." + ) if not settings.DEBUG and not settings.TESTING_MODE: send_credentials(created_users) return validator.result_handler.create_response()