diff --git a/docs/deployment.md b/docs/deployment.md index 5f6cd5e..f257eb2 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -2771,6 +2771,12 @@ Operators can open **Contact and invitation** from **Show All Users** to review the welcome email and send a fresh password setup link to a pending login. Sending a new invitation replaces the previous link. +Verified server administrators can edit the welcome email sender in +**Server Manager → Operator Settings → Email → Edit sender**. Leave the +From address blank to use the address configured for the installation. +Choose a From address accepted by the SMTP relay. The optional Reply-to +address directs replies to a separate inbox. + Managers handle customer onboarding and hosting assignments in their portfolio. Customers manage their own website files, databases, mailboxes, and other hosting resources in the customer workspace. Operators can diff --git a/pyproject.toml b/pyproject.toml index 0f0b89b..755c1eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ server = ["gunicorn>=27.0.1,<28"] "static/hosting/*.js", "static/hosting/*.css", "static/hosting/*.svg", + "static/hosting/*.png", "templates/*.html", "templates/hosting/*.conf", "templates/hosting/*.html", diff --git a/src/coulobre/hosting/account_administration.py b/src/coulobre/hosting/account_administration.py index 9e3e2a8..03aa1a4 100644 --- a/src/coulobre/hosting/account_administration.py +++ b/src/coulobre/hosting/account_administration.py @@ -29,7 +29,7 @@ from .installation_features import ( from .invitations import ( InviteLoginForm, queue_invitation, - render_welcome_message, + welcome_message_context, ) from .models import ( Customer, @@ -189,10 +189,10 @@ class CustomerOnboardingView(UserPassesTestMixin, FormView): data = form.cleaned_data if self.request.POST.get("action") == "preview": defaults = zone_defaults() - preview = render_welcome_message( + preview = welcome_message_context( username=data["username"], welcome_message=data["welcome_message"], - setup_url="[Personal password setup link]", + setup_url="", account=HostingAccount( primary_domain=data["primary_domain"], hosting_plan=data["plan"], diff --git a/src/coulobre/hosting/installation_views.py b/src/coulobre/hosting/installation_views.py index 744e6a9..43a2fd5 100644 --- a/src/coulobre/hosting/installation_views.py +++ b/src/coulobre/hosting/installation_views.py @@ -1,3 +1,4 @@ +from email.utils import parseaddr from importlib.metadata import PackageNotFoundError, version from django import forms @@ -15,7 +16,7 @@ from django.views.generic import FormView, TemplateView from .access import is_tenant from .installation_features import FEATURES, FeaturePolicy -from .models import PanelFeaturePolicy +from .models import InvitationMailSettings, PanelFeaturePolicy from .update_views import inspect_linked_nodes @@ -228,3 +229,57 @@ class FeatureConfigurationView(UserPassesTestMixin, FormView): return self.render_to_response( self.get_context_data(form=form), status=409 ) + + +class InvitationMailSettingsForm(forms.ModelForm): + class Meta: + model = InvitationMailSettings + fields = ("sender_name", "sender_address", "reply_to") + labels = { + "sender_name": "Sender display name", + "sender_address": "From address", + "reply_to": "Reply-to address", + } + help_texts = { + "sender_name": "Leave blank to use the installed sender name.", + "sender_address": "Use an address your SMTP server allows.", + "reply_to": "Leave blank to receive replies at the From address.", + } + + +@method_decorator(never_cache, name="dispatch") +class InvitationMailSettingsView(UserPassesTestMixin, FormView): + form_class = InvitationMailSettingsForm + template_name = "hosting/invitation_mail_settings.html" + success_url = reverse_lazy("panel:invitation_mail_settings") + + def test_func(self): + return ( + self.request.user.is_superuser and self.request.user.is_verified() + ) + + def get_form_kwargs(self): + return super().get_form_kwargs() | { + "instance": InvitationMailSettings.objects.filter(pk=1).first(), + } + + def get_context_data(self, **kwargs): + _, installed_address = parseaddr(settings.DEFAULT_FROM_EMAIL) + return super().get_context_data( + installed_address=installed_address, **kwargs + ) + + @transaction.atomic + def form_valid(self, form): + saved, _ = InvitationMailSettings.objects.update_or_create( + pk=1, defaults=form.cleaned_data + ) + if form.has_changed(): + LogEntry.objects.log_actions( + self.request.user.pk, + [saved], + CHANGE, + "Updated welcome email sender.", + ) + messages.success(self.request, "Welcome email sender saved.") + return super().form_valid(form) diff --git a/src/coulobre/hosting/invitations.py b/src/coulobre/hosting/invitations.py index cedb495..dd66045 100644 --- a/src/coulobre/hosting/invitations.py +++ b/src/coulobre/hosting/invitations.py @@ -1,5 +1,9 @@ import uuid from datetime import timedelta +from email.mime.image import MIMEImage +from email.mime.multipart import MIMEMultipart +from email.utils import parseaddr +from pathlib import Path from django import forms from django.conf import settings @@ -10,7 +14,8 @@ from django.contrib.auth.views import ( INTERNAL_RESET_SESSION_TOKEN, PasswordResetConfirmView, ) -from django.core.mail import send_mail +from django.core.mail import EmailMultiAlternatives +from django.core.mail.message import sanitize_address from django.core.signing import BadSignature, Signer from django.db import transaction from django.shortcuts import get_object_or_404, redirect @@ -27,7 +32,13 @@ from kombu.exceptions import OperationalError from .access import customer_for_user, is_tenant, reseller_for_user from .account_setup import website_records -from .models import Customer, HostingAccount, PanelInvitation, Reseller +from .models import ( + Customer, + HostingAccount, + InvitationMailSettings, + PanelInvitation, + Reseller, +) from .panel_factors import lock_user from .password_reset import reset_enabled @@ -170,16 +181,7 @@ def deliver_invitation(invitation_id, revision): ) context = invitation_message_context(user, setup_url=origin + path) try: - sent = send_mail( - "Welcome to Coulobre Panel", - render_to_string("hosting/panel_invitation.txt", context), - None, - [user.email], - html_message=render_to_string( - "hosting/panel_invitation.html", - {"invitation_content": context}, - ), - ) + sent = invitation_email(user.email, context).send() if sent != 1: raise OSError("The email backend did not accept the invitation.") except (OSError, ValueError): @@ -188,6 +190,57 @@ def deliver_invitation(invitation_id, revision): pending.update(delivery=PanelInvitation.Delivery.SENT) +class InvitationEmail(EmailMultiAlternatives): + def message(self): + message = super().message() + plain, html = message.get_payload() + logo_file = ( + Path(__file__).parent / "static/hosting/coulobre-mail-mark.png" + ) + logo = MIMEImage(logo_file.read_bytes(), _subtype="png") + logo.add_header("Content-ID", "") + logo.add_header("Content-Disposition", "inline") + related = MIMEMultipart( + "related", _subparts=[html, logo], type="text/html" + ) + message.set_payload([plain, related]) + return message + + +@sensitive_variables("context") +def invitation_email(recipient, context): + mail_settings = ( + InvitationMailSettings.objects.filter(pk=1).first() + or InvitationMailSettings() + ) + sender = settings.DEFAULT_FROM_EMAIL + if mail_settings.sender_name or mail_settings.sender_address: + name, address = parseaddr(sender) + sender = sanitize_address( + ( + mail_settings.sender_name or name, + mail_settings.sender_address or address, + ), + settings.DEFAULT_CHARSET, + ) + return InvitationEmail( + "Welcome to Coulobre Panel", + render_to_string("hosting/panel_invitation.txt", context), + sender, + [recipient], + reply_to=[mail_settings.reply_to] if mail_settings.reply_to else None, + alternatives=[ + ( + render_to_string( + "hosting/panel_invitation.html", + {"invitation_content": context}, + ), + "text/html", + ) + ], + ) + + def invitation_message_context(user, *, setup_url=""): invitation = getattr(user, "panel_invitation", None) account = ( @@ -249,12 +302,6 @@ def welcome_message_context( } -def render_welcome_message(**kwargs): - return render_to_string( - "hosting/panel_invitation.txt", welcome_message_context(**kwargs) - ) - - class AcceptInvitation(PasswordResetConfirmView): template_name = "hosting/accept_invitation.html" token_generator = invitation_token diff --git a/src/coulobre/hosting/migrations/0080_invitation_mail_settings.py b/src/coulobre/hosting/migrations/0080_invitation_mail_settings.py new file mode 100644 index 0000000..b289d2d --- /dev/null +++ b/src/coulobre/hosting/migrations/0080_invitation_mail_settings.py @@ -0,0 +1,45 @@ +# Generated by Django 5.2.19 on 2026-11-24 12:32 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0079_onboarding_dismissal"), + ] + + operations = [ + migrations.CreateModel( + name="InvitationMailSettings", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "sender_name", + models.CharField( + blank=True, + max_length=120, + validators=[ + django.core.validators.RegexValidator( + "\\A[^\\x00-\\x1f\\x7f]*\\Z", + "Enter a single-line name.", + ) + ], + ), + ), + ( + "sender_address", + models.EmailField(blank=True, max_length=254), + ), + ("reply_to", models.EmailField(blank=True, max_length=254)), + ], + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 97d9cf6..85488f5 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -187,6 +187,20 @@ class PanelInvitation(models.Model): ) +class InvitationMailSettings(models.Model): + sender_name = models.CharField( + max_length=120, + blank=True, + validators=[ + RegexValidator( + r"\A[^\x00-\x1f\x7f]*\Z", "Enter a single-line name." + ) + ], + ) + sender_address = models.EmailField(blank=True) + reply_to = models.EmailField(blank=True) + + class CustomerImpersonation(models.Model): class EndReason(models.TextChoices): STOPPED = "stopped", "Ended by administrator" diff --git a/src/coulobre/hosting/static/hosting/coulobre-mail-mark.png b/src/coulobre/hosting/static/hosting/coulobre-mail-mark.png new file mode 100644 index 0000000..194013e Binary files /dev/null and b/src/coulobre/hosting/static/hosting/coulobre-mail-mark.png differ diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 9a7d332..8de1b08 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -959,6 +959,11 @@ dd { accent-color: var(--accent); } +.welcome-email-preview { + min-width: 0; + overflow-wrap: anywhere; +} + .plan-field-heading { position: relative; display: flex; diff --git a/src/coulobre/hosting/templates/hosting/feature_configuration.html b/src/coulobre/hosting/templates/hosting/feature_configuration.html index e4e533d..258ef47 100644 --- a/src/coulobre/hosting/templates/hosting/feature_configuration.html +++ b/src/coulobre/hosting/templates/hosting/feature_configuration.html @@ -9,6 +9,12 @@ {% endif %} {% endblock %} {% block page_content %} + {% if selected_section == 'email' %} +
+

Welcome email sender

+ Edit sender +
+ {% endif %} {% if selected_section == 'general' %}

Installation

diff --git a/src/coulobre/hosting/templates/hosting/invitation_mail_settings.html b/src/coulobre/hosting/templates/hosting/invitation_mail_settings.html new file mode 100644 index 0000000..c7ffd73 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/invitation_mail_settings.html @@ -0,0 +1,19 @@ +{% extends "hosting/workspace_page.html" %} +{% block title %}Welcome email sender · Coulobre Panel{% endblock %} +{% block page_title %}Welcome email sender{% endblock %} +{% block page_context %} + ← Email settings +{% endblock %} +{% block page_content %} +
+

These settings apply to welcome emails. Leave the From address blank to use {{ installed_address }}.

+
+ {% csrf_token %} + {{ form.as_div }} +
+ + Cancel +
+
+
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/manage_invitation.html b/src/coulobre/hosting/templates/hosting/manage_invitation.html index 38e9e6b..b0d9167 100644 --- a/src/coulobre/hosting/templates/hosting/manage_invitation.html +++ b/src/coulobre/hosting/templates/hosting/manage_invitation.html @@ -14,7 +14,7 @@ {% if welcome_preview %} -
+
Welcome email preview

A password setup link is added when the invitation is sent.

diff --git a/src/coulobre/hosting/templates/hosting/panel_invitation.html b/src/coulobre/hosting/templates/hosting/panel_invitation.html index 82e7d7a..f99ebe8 100644 --- a/src/coulobre/hosting/templates/hosting/panel_invitation.html +++ b/src/coulobre/hosting/templates/hosting/panel_invitation.html @@ -5,13 +5,17 @@ Welcome to Coulobre Panel - + -
-
- {% include "hosting/panel_invitation_body.html" with message=invitation_content email_format=True %} -
+
+ + + + +
+ {% include "hosting/panel_invitation_body.html" with message=invitation_content email_format=True %} +
diff --git a/src/coulobre/hosting/templates/hosting/panel_invitation_body.html b/src/coulobre/hosting/templates/hosting/panel_invitation_body.html index 7f26720..4451ae5 100644 --- a/src/coulobre/hosting/templates/hosting/panel_invitation_body.html +++ b/src/coulobre/hosting/templates/hosting/panel_invitation_body.html @@ -1,19 +1,30 @@ +{% load panel_assets %} + + + + + + Welcome to Coulobre Panel {% if message.welcome_message %}{{ message.welcome_message|linebreaks }}{% endif %}

Your hosting provider created a panel login for you. Set your password within 48 hours.

-

Username {{ message.username }}

+

Username
{{ message.username }}

{% if message.setup_url %} -

Set your password

+ + +
+ Set your password +
{% else %}

Set your password

{% endif %} {% if message.panel_url %}

Open Coulobre Panel

{% endif %} {% if message.account %}

Your hosting

-

Domain {{ message.account.primary_domain }}

+

Domain
{{ message.account.primary_domain }}

{% with package=message.account.hosting_plan %} {% if package %} -

Hosting package {{ package.name }}

+

Hosting package
{{ package.name }}

  • Websites · {{ package.website_limit }}
  • Scheduled tasks · {{ package.website_job_limit }}
  • diff --git a/src/coulobre/hosting/templates/hosting/workspace_form.html b/src/coulobre/hosting/templates/hosting/workspace_form.html index a395bee..b24ee9a 100644 --- a/src/coulobre/hosting/templates/hosting/workspace_form.html +++ b/src/coulobre/hosting/templates/hosting/workspace_form.html @@ -10,9 +10,12 @@ {% csrf_token %} {{ form.as_div }} {% if welcome_preview %} -
    +
    Welcome email preview -
    {{ welcome_preview|force_escape }}
    +

    A password setup link is added when the invitation is sent.

    +
    + {% include "hosting/panel_invitation_body.html" with message=welcome_preview %} +
    {% endif %} {% if moving %} diff --git a/src/coulobre/hosting/test_panel_authentication.py b/src/coulobre/hosting/test_panel_authentication.py index c303cf6..9ffb460 100644 --- a/src/coulobre/hosting/test_panel_authentication.py +++ b/src/coulobre/hosting/test_panel_authentication.py @@ -140,6 +140,19 @@ class PanelAuthenticationTests(TestCase): session = self.client.session session["otp_device_id"] = device.persistent_id session.save() + settings_url = reverse("panel:invitation_mail_settings") + config = { + "sender_name": "Hosting support", + "sender_address": "welcome@example.test", + "reply_to": "support@example.test", + } + self.assertRedirects( + self.client.post(settings_url, config), settings_url + ) + self.assertContains( + self.client.post(settings_url, config | {"reply_to": "invalid"}), + "Enter a valid email address.", + ) recipient = "new@example.test" def run_delivery(*, args, retry): @@ -167,6 +180,10 @@ class PanelAuthenticationTests(TestCase): self.assertEqual(len(mail.outbox), 1) invitation = mail.outbox[0] self.assertEqual(invitation.to, [recipient]) + self.assertEqual( + invitation.from_email, "Hosting support " + ) + self.assertEqual(invitation.reply_to, ["support@example.test"]) self.assertIn("Username: new-user", invitation.body) self.assertIn("48 hours", invitation.body) link = self.invitation_path(invitation.body) @@ -174,6 +191,15 @@ class PanelAuthenticationTests(TestCase): self.assertEqual(content_type, "text/html") self.assertIn("Welcome to Coulobre Panel", html) self.assertIn(f'https://panel.example.test{link}"', html) + self.assertIn('src="cid:coulobre-welcome-logo"', html) + plain, related = invitation.message().get_payload() + self.assertEqual(plain.get_content_type(), "text/plain") + self.assertEqual(related.get_content_type(), "multipart/related") + markup, logo = related.get_payload() + self.assertEqual(markup.get_content_type(), "text/html") + self.assertEqual(logo.get_content_type(), "image/png") + self.assertEqual(logo["Content-ID"], "") + self.assertTrue(logo.get_payload(decode=True).startswith(b"\x89PNG")) manage_url = reverse( "panel:manage_invitation", args=["customer", customer.pk] @@ -219,6 +245,16 @@ class PanelAuthenticationTests(TestCase): ) self.assertEqual(invited_browser.get(replacement).status_code, 302) + def test_customer_cannot_edit_the_welcome_email_sender(self): + self.client.force_login(self.customer) + url = reverse("panel:invitation_mail_settings") + self.assertEqual( + self.client.post( + url, {"sender_address": "other@example.test"} + ).status_code, + 403, + ) + def invitation_path(self, body): return urlsplit( next(line for line in body.splitlines() if "/welcome/" in line) diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py index 932c68e..7888989 100644 --- a/src/coulobre/hosting/urls.py +++ b/src/coulobre/hosting/urls.py @@ -41,7 +41,11 @@ from .independent_website_views import ( IndependentWebsiteOperation, IndependentWebsitePhpSettings, ) -from .installation_views import FeatureConfigurationView, InstallationView +from .installation_views import ( + FeatureConfigurationView, + InstallationView, + InvitationMailSettingsView, +) from .invitations import AcceptInvitation, ManageInvitation from .mail_delivery_views import MailDeliveryView from .mail_dns_views import MailDnsCheckView, RequestMailDnsSetup @@ -296,6 +300,11 @@ urlpatterns = [ FeatureConfigurationView.as_view(), name="operator_settings", ), + path( + "settings/email/sender/", + InvitationMailSettingsView.as_view(), + name="invitation_mail_settings", + ), path("account/role/", switch_panel_role, name="switch_panel_role"), path( "account/getting-started/", diff --git a/src/coulobre/hosting/workspaces.py b/src/coulobre/hosting/workspaces.py index b602755..1ae7ce9 100644 --- a/src/coulobre/hosting/workspaces.py +++ b/src/coulobre/hosting/workspaces.py @@ -70,6 +70,7 @@ WORKSPACE_PARENT_ROUTES = { "remote_deploy": ("connected_nodes", ()), "dns_topology": ("dns_defaults", ()), "operator_settings": ("operator_settings", ("general",)), + "invitation_mail_settings": ("operator_settings", ("general",)), "http_defaults": ("http_defaults", ("apache",)), "server_updates": ("updates", ()), "changelog": ("updates", ()),