feat: add email settings with database overrides for admin and vendor

Platform Email Settings (Admin):
- Add GET/PUT/DELETE /admin/settings/email/* endpoints
- Settings stored in admin_settings table override .env values
- Support all providers: SMTP, SendGrid, Mailgun, Amazon SES
- Edit mode UI with provider-specific configuration forms
- Reset to .env defaults functionality
- Test email to verify configuration

Vendor Email Settings:
- Add VendorEmailSettings model with one-to-one vendor relationship
- Migration: v0a1b2c3d4e5_add_vendor_email_settings.py
- Service: vendor_email_settings_service.py with tier validation
- API endpoints: /vendor/email-settings/* (CRUD, status, verify)
- Email tab in vendor settings page with full configuration
- Warning banner until email is configured (like billing warnings)
- Premium providers (SendGrid, Mailgun, SES) tier-gated to Business+

Email Service Updates:
- get_platform_email_config(db) checks DB first, then .env
- Configurable provider classes accept config dict
- EmailService uses database-aware providers
- Vendor emails use vendor's own SMTP (Wizamart doesn't pay)
- "Powered by Wizamart" footer for Essential/Professional tiers
- White-label (no footer) for Business/Enterprise tiers

Other:
- Add scripts/install.py for first-time platform setup
- Add make install target
- Update init-prod to include email template seeding

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-05 22:23:47 +01:00
parent ad28a8a9a3
commit 36603178c3
51 changed files with 4959 additions and 1141 deletions

View File

@@ -40,7 +40,8 @@ function vendorSettings() {
{ id: 'branding', label: 'Branding', icon: 'color-swatch' },
{ id: 'domains', label: 'Domains', icon: 'globe-alt' },
{ id: 'api', label: 'API & Payments', icon: 'key' },
{ id: 'notifications', label: 'Notifications', icon: 'bell' }
{ id: 'notifications', label: 'Notifications', icon: 'bell' },
{ id: 'email', label: 'Email', icon: 'envelope' }
],
// Forms for different sections
@@ -95,6 +96,38 @@ function vendorSettings() {
storefront_locale: ''
},
// Email settings
emailSettings: null,
emailSettingsLoading: false,
emailProviders: [],
emailForm: {
from_email: '',
from_name: '',
reply_to_email: '',
signature_text: '',
signature_html: '',
provider: 'smtp',
// SMTP
smtp_host: '',
smtp_port: 587,
smtp_username: '',
smtp_password: '',
smtp_use_tls: true,
smtp_use_ssl: false,
// SendGrid
sendgrid_api_key: '',
// Mailgun
mailgun_api_key: '',
mailgun_domain: '',
// SES
ses_access_key_id: '',
ses_secret_access_key: '',
ses_region: 'eu-west-1'
},
testEmailAddress: '',
sendingTestEmail: false,
hasEmailChanges: false,
// Track changes per section
hasChanges: false,
hasBusinessChanges: false,
@@ -383,6 +416,161 @@ function vendorSettings() {
} finally {
this.saving = false;
}
},
// =====================================================================
// EMAIL SETTINGS
// =====================================================================
/**
* Load email settings when email tab is activated
*/
async loadEmailSettings() {
if (this.emailSettings !== null) {
return; // Already loaded
}
this.emailSettingsLoading = true;
try {
// Load settings and providers in parallel
const [settingsResponse, providersResponse] = await Promise.all([
apiClient.get('/vendor/email-settings'),
apiClient.get('/vendor/email-settings/providers')
]);
this.emailProviders = providersResponse.providers || [];
if (settingsResponse.configured && settingsResponse.settings) {
this.emailSettings = settingsResponse.settings;
this.populateEmailForm(settingsResponse.settings);
} else {
this.emailSettings = { is_configured: false, is_verified: false };
}
vendorSettingsLog.info('Loaded email settings');
} catch (error) {
vendorSettingsLog.error('Failed to load email settings:', error);
Utils.showToast('Failed to load email settings', 'error');
} finally {
this.emailSettingsLoading = false;
}
},
/**
* Populate email form from settings
*/
populateEmailForm(settings) {
this.emailForm = {
from_email: settings.from_email || '',
from_name: settings.from_name || '',
reply_to_email: settings.reply_to_email || '',
signature_text: settings.signature_text || '',
signature_html: settings.signature_html || '',
provider: settings.provider || 'smtp',
// SMTP - don't populate password
smtp_host: settings.smtp_host || '',
smtp_port: settings.smtp_port || 587,
smtp_username: settings.smtp_username || '',
smtp_password: '', // Never populate password
smtp_use_tls: settings.smtp_use_tls !== false,
smtp_use_ssl: settings.smtp_use_ssl || false,
// SendGrid - don't populate API key
sendgrid_api_key: '',
// Mailgun - don't populate API key
mailgun_api_key: '',
mailgun_domain: settings.mailgun_domain || '',
// SES - don't populate secrets
ses_access_key_id: '',
ses_secret_access_key: '',
ses_region: settings.ses_region || 'eu-west-1'
};
this.hasEmailChanges = false;
},
/**
* Mark email form as changed
*/
markEmailChanged() {
this.hasEmailChanges = true;
},
/**
* Save email settings
*/
async saveEmailSettings() {
// Validate required fields
if (!this.emailForm.from_email || !this.emailForm.from_name) {
Utils.showToast('From Email and From Name are required', 'error');
return;
}
this.saving = true;
try {
const response = await apiClient.put('/vendor/email-settings', this.emailForm);
if (response.success) {
Utils.showToast('Email settings saved', 'success');
vendorSettingsLog.info('Email settings updated');
// Update local state
this.emailSettings = response.settings;
this.hasEmailChanges = false;
} else {
Utils.showToast(response.message || 'Failed to save email settings', 'error');
}
} catch (error) {
vendorSettingsLog.error('Failed to save email settings:', error);
Utils.showToast(error.message || 'Failed to save email settings', 'error');
} finally {
this.saving = false;
}
},
/**
* Send test email
*/
async sendTestEmail() {
if (!this.testEmailAddress) {
Utils.showToast('Please enter a test email address', 'error');
return;
}
if (!this.emailSettings?.is_configured) {
Utils.showToast('Please save your email settings first', 'error');
return;
}
this.sendingTestEmail = true;
try {
const response = await apiClient.post('/vendor/email-settings/verify', {
test_email: this.testEmailAddress
});
if (response.success) {
Utils.showToast('Test email sent! Check your inbox.', 'success');
// Update verification status
this.emailSettings.is_verified = true;
} else {
Utils.showToast(response.message || 'Failed to send test email', 'error');
}
} catch (error) {
vendorSettingsLog.error('Failed to send test email:', error);
Utils.showToast(error.message || 'Failed to send test email', 'error');
} finally {
this.sendingTestEmail = false;
}
},
/**
* Switch active section - with email loading hook
*/
setSection(sectionId) {
this.activeSection = sectionId;
// Load email settings when email tab is activated
if (sectionId === 'email' && this.emailSettings === null) {
this.loadEmailSettings();
}
}
};
}