From 0c028ef107b28dc04afdb3444dac62334b12fdad Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 11:29:41 +0300 Subject: [PATCH] fix(settings): OTP mail is sent from the SMTP account, or DEFAULT_FROM_EMAIL send_mail got no sender, so the OTP mail went out from Django's default webmaster@localhost, which relays that take mail only from their account refuse. DEFAULT_FROM_EMAIL is now read from the environment, defaulting to EMAIL_HOST_USER. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- README.md | 2 +- webvirtcloud/settings.py.template | 2 ++ webvirtcloud/tests/test_settings_env.py | 9 ++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6bde5fe2..0086c366 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,7 @@ If libvirt warns `Host SMBIOS information is not available`, install `dmidecode` | `WEBVIRTCLOUD_SERIAL_CONSOLE=1` | serial console, see [Serial console](#serial-console) | | `WEBVIRTCLOUD_DEBUG=1` | Django debug mode: error pages show the traceback and settings; for trying things out only, never on a panel others use | | `WEBVIRTCLOUD_DB_HOST` | use PostgreSQL instead of sqlite (new installs); `WEBVIRTCLOUD_DB_PORT`/`_NAME`/`_USER`/`_PASSWORD` default to 5432/webvirtcloud/webvirtcloud/empty | -| `EMAIL_HOST`, `EMAIL_PORT`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`, `EMAIL_USE_TLS` | SMTP for e-mail OTP; `EMAIL_USE_TLS` is 1 (STARTTLS, the default) or 0 | +| `EMAIL_HOST`, `EMAIL_PORT`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`, `EMAIL_USE_TLS`, `DEFAULT_FROM_EMAIL` | SMTP for e-mail OTP; `EMAIL_USE_TLS` is 1 (STARTTLS, the default) or 0; the sender `DEFAULT_FROM_EMAIL` defaults to `EMAIL_HOST_USER` | | `TZ` | time zone of the panel's dates and times and, in Docker, of the container (its daily backup), e.g. `Europe/Istanbul`; default UTC (the installer writes the server's zone into `settings.py` instead) | | `WEBVIRTCLOUD_LOG_FILE` | log file (default `data/webvirtcloud.log`; a relative path is from the panel's directory; rotated at 10 MB × 5) | | `SECRET_KEY` | Django secret key (default: the `data/secret_key` file) | diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index bb5a3c78..661d971b 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -436,6 +436,8 @@ EMAIL_PORT = int(os.getenv("EMAIL_PORT", "587")) EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER", "") ## password associated with above email-id EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "") +# the mails' From: relays take mail only from their account (Django's own default is webmaster@localhost) +DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL") or EMAIL_HOST_USER or "webmaster@localhost" # seconds an SMTP server may take to answer; without it a server that stops answering holds the request for good EMAIL_TIMEOUT = 10 diff --git a/webvirtcloud/tests/test_settings_env.py b/webvirtcloud/tests/test_settings_env.py index b5bd051c..33392c1e 100644 --- a/webvirtcloud/tests/test_settings_env.py +++ b/webvirtcloud/tests/test_settings_env.py @@ -14,7 +14,7 @@ from django.test import SimpleTestCase, TestCase, override_settings # every case starts from the defaults. CONFIG_VARS = [ "WEBVIRTCLOUD_HTTPS", "WEBVIRTCLOUD_DEBUG", "WEBVIRTCLOUD_SERIAL_CONSOLE", "ALLOWED_HOSTS", - "EMAIL_HOST", "EMAIL_PORT", "EMAIL_HOST_USER", "EMAIL_HOST_PASSWORD", "EMAIL_USE_TLS", + "EMAIL_HOST", "EMAIL_PORT", "EMAIL_HOST_USER", "EMAIL_HOST_PASSWORD", "EMAIL_USE_TLS", "DEFAULT_FROM_EMAIL", "WEBVIRTCLOUD_DB_HOST", "WEBVIRTCLOUD_DB_PORT", "WEBVIRTCLOUD_DB_NAME", "WEBVIRTCLOUD_DB_USER", "WEBVIRTCLOUD_DB_PASSWORD", "TZ", ] @@ -107,6 +107,13 @@ class EmailEnvTestCase(SimpleTestCase): "EMAIL_HOST_PASSWORD": "secret", "EMAIL_USE_TLS": False, }) + def test_the_sender_is_the_smtp_account_unless_set(self): + self.assertEqual(load_settings({}, ["DEFAULT_FROM_EMAIL"])["DEFAULT_FROM_EMAIL"], "webmaster@localhost") + env = {"EMAIL_HOST_USER": "wvc@example.com"} + self.assertEqual(load_settings(env, ["DEFAULT_FROM_EMAIL"])["DEFAULT_FROM_EMAIL"], "wvc@example.com") + env["DEFAULT_FROM_EMAIL"] = "WebVirtCloud " + self.assertEqual(load_settings(env, ["DEFAULT_FROM_EMAIL"])["DEFAULT_FROM_EMAIL"], "WebVirtCloud ") + class DatabaseEnvTestCase(SimpleTestCase): def db(self, env):