Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

feat: add onboarding templates for new users (password reset, order confirmation, newsletter)

Jonas Kaninda8c5a0d5992ed
diff --git a/internal/services/seeder/constants.go b/internal/services/seeder/constants.go
index c9568f4..ac734e9 100644
--- a/internal/services/seeder/constants.go
+++ b/internal/services/seeder/constants.go
@@ -227,3 +227,279 @@ L'équipe {{company}}
© {{year}} {{company}} — Licence Apache 2.0
`
+
+// ======== Password Reset Templates ========
+
+const passwordResetHTMLTemplate = `<div class="email-wrapper">
+ <div class="email-container">
+ <div class="email-header">
+ <h1>Password Reset</h1>
+ <p>Reset your account password</p>
+ </div>
+ <div class="email-body">
+ <h2>Hello {{name}},</h2>
+ <p>We received a request to reset your password. Click the button below to choose a new one. This link will expire in <strong>{{expiry}}</strong>.</p>
+ <p style="text-align:center;margin:32px 0;">
+ <a href="{{resetLink}}" class="btn">Reset Password</a>
+ </p>
+ <p>If you didn't request a password reset, you can safely ignore this email. Your password will remain unchanged.</p>
+ <p>Best regards,<br/>The {{company}} Team</p>
+ </div>
+ <div class="email-footer">
+ <p>© {{year}} {{company}}</p>
+ </div>
+ </div>
+</div>`
+
+const passwordResetTextTemplate = `Hello {{name}},
+
+We received a request to reset your password.
+
+Click the link below to choose a new password. This link will expire in {{expiry}}.
+
+Reset your password: {{resetLink}}
+
+If you didn't request a password reset, you can safely ignore this email. Your password will remain unchanged.
+
+Best regards,
+The {{company}} Team
+
+© {{year}} {{company}}
+`
+
+const passwordResetHTMLTemplateFr = `<div class="email-wrapper">
+ <div class="email-container">
+ <div class="email-header">
+ <h1>Réinitialisation du mot de passe</h1>
+ <p>Réinitialisez le mot de passe de votre compte</p>
+ </div>
+ <div class="email-body">
+ <h2>Bonjour {{name}},</h2>
+ <p>Nous avons reçu une demande de réinitialisation de votre mot de passe. Cliquez sur le bouton ci-dessous pour en choisir un nouveau. Ce lien expirera dans <strong>{{expiry}}</strong>.</p>
+ <p style="text-align:center;margin:32px 0;">
+ <a href="{{resetLink}}" class="btn">Réinitialiser le mot de passe</a>
+ </p>
+ <p>Si vous n'avez pas demandé de réinitialisation de mot de passe, vous pouvez ignorer cet email en toute sécurité. Votre mot de passe restera inchangé.</p>
+ <p>Cordialement,<br/>L'équipe {{company}}</p>
+ </div>
+ <div class="email-footer">
+ <p>© {{year}} {{company}}</p>
+ </div>
+ </div>
+</div>`
+
+const passwordResetTextTemplateFr = `Bonjour {{name}},
+
+Nous avons reçu une demande de réinitialisation de votre mot de passe.
+
+Cliquez sur le lien ci-dessous pour choisir un nouveau mot de passe. Ce lien expirera dans {{expiry}}.
+
+Réinitialiser votre mot de passe : {{resetLink}}
+
+Si vous n'avez pas demandé de réinitialisation de mot de passe, vous pouvez ignorer cet email en toute sécurité. Votre mot de passe restera inchangé.
+
+Cordialement,
+L'équipe {{company}}
+
+© {{year}} {{company}}
+`
+
+// ======== Order Confirmation Templates ========
+
+const orderConfirmationHTMLTemplate = `<div class="email-wrapper">
+ <div class="email-container">
+ <div class="email-header">
+ <h1>Order Confirmed</h1>
+ <p>Thank you for your purchase</p>
+ </div>
+ <div class="email-body">
+ <h2>Hello {{name}},</h2>
+ <p>Your order <strong>#{{orderNumber}}</strong> has been confirmed. Here is a summary of your purchase:</p>
+ <table width="100%" cellpadding="0" cellspacing="0" style="margin:20px 0;border-collapse:collapse;">
+ <tr style="border-bottom:2px solid #e5e7eb;">
+ <th style="text-align:left;padding:10px 0;color:#111827;">Item</th>
+ <th style="text-align:center;padding:10px 0;color:#111827;">Qty</th>
+ <th style="text-align:right;padding:10px 0;color:#111827;">Price</th>
+ </tr>
+ {{range items}}
+ <tr style="border-bottom:1px solid #e5e7eb;">
+ <td style="padding:10px 0;color:#4b5563;">{{.name}}</td>
+ <td style="text-align:center;padding:10px 0;color:#4b5563;">{{.qty}}</td>
+ <td style="text-align:right;padding:10px 0;color:#4b5563;">{{.price}}</td>
+ </tr>
+ {{end}}
+ <tr>
+ <td colspan="2" style="padding:12px 0;font-weight:700;color:#111827;">Total</td>
+ <td style="text-align:right;padding:12px 0;font-weight:700;color:#111827;">{{total}}</td>
+ </tr>
+ </table>
+ <p>We will notify you once your order has shipped.</p>
+ <p>Best regards,<br/>The {{company}} Team</p>
+ </div>
+ <div class="email-footer">
+ <p>© {{year}} {{company}}</p>
+ </div>
+ </div>
+</div>`
+
+const orderConfirmationTextTemplate = `Hello {{name}},
+
+Your order #{{orderNumber}} has been confirmed. Here is a summary of your purchase:
+
+{{range items}}
+- {{.name}} (x{{.qty}}): {{.price}}
+{{end}}
+
+Total: {{total}}
+
+We will notify you once your order has shipped.
+
+Best regards,
+The {{company}} Team
+
+© {{year}} {{company}}
+`
+
+const orderConfirmationHTMLTemplateFr = `<div class="email-wrapper">
+ <div class="email-container">
+ <div class="email-header">
+ <h1>Commande confirmée</h1>
+ <p>Merci pour votre achat</p>
+ </div>
+ <div class="email-body">
+ <h2>Bonjour {{name}},</h2>
+ <p>Votre commande <strong>#{{orderNumber}}</strong> a été confirmée. Voici un récapitulatif de votre achat :</p>
+ <table width="100%" cellpadding="0" cellspacing="0" style="margin:20px 0;border-collapse:collapse;">
+ <tr style="border-bottom:2px solid #e5e7eb;">
+ <th style="text-align:left;padding:10px 0;color:#111827;">Article</th>
+ <th style="text-align:center;padding:10px 0;color:#111827;">Qté</th>
+ <th style="text-align:right;padding:10px 0;color:#111827;">Prix</th>
+ </tr>
+ {{range items}}
+ <tr style="border-bottom:1px solid #e5e7eb;">
+ <td style="padding:10px 0;color:#4b5563;">{{.name}}</td>
+ <td style="text-align:center;padding:10px 0;color:#4b5563;">{{.qty}}</td>
+ <td style="text-align:right;padding:10px 0;color:#4b5563;">{{.price}}</td>
+ </tr>
+ {{end}}
+ <tr>
+ <td colspan="2" style="padding:12px 0;font-weight:700;color:#111827;">Total</td>
+ <td style="text-align:right;padding:12px 0;font-weight:700;color:#111827;">{{total}}</td>
+ </tr>
+ </table>
+ <p>Nous vous informerons dès que votre commande aura été expédiée.</p>
+ <p>Cordialement,<br/>L'équipe {{company}}</p>
+ </div>
+ <div class="email-footer">
+ <p>© {{year}} {{company}}</p>
+ </div>
+ </div>
+</div>`
+
+const orderConfirmationTextTemplateFr = `Bonjour {{name}},
+
+Votre commande #{{orderNumber}} a été confirmée. Voici un récapitulatif de votre achat :
+
+{{range items}}
+- {{.name}} (x{{.qty}}) : {{.price}}
+{{end}}
+
+Total : {{total}}
+
+Nous vous informerons dès que votre commande aura été expédiée.
+
+Cordialement,
+L'équipe {{company}}
+
+© {{year}} {{company}}
+`
+
+// ======== Newsletter Templates ========
+
+const newsletterHTMLTemplate = `<div class="email-wrapper">
+ <div class="email-container">
+ <div class="email-header">
+ <h1>{{month}} Newsletter</h1>
+ <p>The latest updates from {{company}}</p>
+ </div>
+ <div class="email-body">
+ <h2>Hello {{name}},</h2>
+ <p>Here are the highlights from this month:</p>
+ {{range articles}}
+ <div style="margin:24px 0;padding-bottom:20px;border-bottom:1px solid #e5e7eb;">
+ <h3 style="margin:0 0 8px;color:#111827;font-size:18px;">{{.title}}</h3>
+ <p style="margin:0 0 12px;color:#4b5563;">{{.summary}}</p>
+ <a href="{{.url}}" style="color:#9333ea;font-weight:600;text-decoration:none;">Read more →</a>
+ </div>
+ {{end}}
+ <p>Best regards,<br/>The {{company}} Team</p>
+ </div>
+ <div class="email-footer">
+ <p>© {{year}} {{company}}</p>
+ <p><a href="{{unsubscribeUrl}}">Unsubscribe</a></p>
+ </div>
+ </div>
+</div>`
+
+const newsletterTextTemplate = `Hello {{name}},
+
+{{month}} Newsletter — The latest updates from {{company}}
+
+{{range articles}}
+## {{.title}}
+{{.summary}}
+Read more: {{.url}}
+
+{{end}}
+
+Best regards,
+The {{company}} Team
+
+© {{year}} {{company}}
+
+Unsubscribe: {{unsubscribeUrl}}
+`
+
+const newsletterHTMLTemplateFr = `<div class="email-wrapper">
+ <div class="email-container">
+ <div class="email-header">
+ <h1>Newsletter de {{month}}</h1>
+ <p>Les dernières nouvelles de {{company}}</p>
+ </div>
+ <div class="email-body">
+ <h2>Bonjour {{name}},</h2>
+ <p>Voici les points forts de ce mois-ci :</p>
+ {{range articles}}
+ <div style="margin:24px 0;padding-bottom:20px;border-bottom:1px solid #e5e7eb;">
+ <h3 style="margin:0 0 8px;color:#111827;font-size:18px;">{{.title}}</h3>
+ <p style="margin:0 0 12px;color:#4b5563;">{{.summary}}</p>
+ <a href="{{.url}}" style="color:#9333ea;font-weight:600;text-decoration:none;">Lire la suite →</a>
+ </div>
+ {{end}}
+ <p>Cordialement,<br/>L'équipe {{company}}</p>
+ </div>
+ <div class="email-footer">
+ <p>© {{year}} {{company}}</p>
+ <p><a href="{{unsubscribeUrl}}">Se désabonner</a></p>
+ </div>
+ </div>
+</div>`
+
+const newsletterTextTemplateFr = `Bonjour {{name}},
+
+Newsletter de {{month}} — Les dernières nouvelles de {{company}}
+
+{{range articles}}
+## {{.title}}
+{{.summary}}
+Lire la suite : {{.url}}
+
+{{end}}
+
+Cordialement,
+L'équipe {{company}}
+
+© {{year}} {{company}}
+
+Se désabonner : {{unsubscribeUrl}}
+`
diff --git a/internal/services/seeder/seeder.go b/internal/services/seeder/seeder.go
index 8918ebb..d26a94e 100644
--- a/internal/services/seeder/seeder.go
+++ b/internal/services/seeder/seeder.go
@@ -53,129 +53,219 @@ func New(
}
}
-// SeedUserDefaults creates default stylesheet and template for a user
-func (s *Seeder) SeedUserDefaults(userID uint, userName string) {
- if userName == "" {
- userName = "Jonas"
- }
- templates, total, err := s.templateRepo.FindByUserID(userID, 1, 0)
- if err != nil || total > 0 || len(templates) > 0 {
- return
- }
-
- // Create default stylesheet
- ss := &models.StyleSheet{
- UserID: userID,
- Name: "Default",
- CSS: defaultCSS,
- }
- if err := s.stylesheetRepo.Create(ss); err != nil {
- logger.Error("failed to seed default stylesheet", "user_id", userID, "error", err)
- return
- }
-
- sample := okapi.M{
- "name": userName,
- "product": "Posta",
- "company": "Posta",
- "year": time.Now().Year(),
- "docs": fmt.Sprintf("%s/docs", goutils.Env("POSTA_WEB_URL", "")),
- "features": []string{
- "REST API for transactional, batch, and templated emails",
- "Scheduled sending and preview mode",
- "Async processing with automatic retries and priority queues",
- "Versioned and multi-language templates with variable substitution",
- "Multiple SMTP providers with TLS and shared pools",
- "Domain verification (SPF, DKIM, DMARC)",
- "API keys with expiration, hashing, and IP allowlisting",
- "JWT authentication, RBAC, and two-factor authentication",
- "Contact tracking, segmentation, and suppression lists",
- "Multi-tenant workspaces with scoped API keys",
- "Event-driven webhooks with retry and delivery tracking",
- "Email delivery analytics and Prometheus metrics",
- "Web dashboard with dark/light mode",
- "Official SDKs for Go, PHP, and Java",
- },
-
- "links": []map[string]string{
- {
- "title": "Website",
- "url": "https://goposta.dev/",
- },
- {
- "title": "API Documentation",
- "url": "https://app.goposta.dev/docs",
- },
- {
- "title": "Documentation",
- "url": "https://docs.goposta.dev/",
- },
- {
- "title": "GitHub Repository",
- "url": "https://github.com/goposta/posta",
- },
- },
- }
+type templateDef struct {
+ Name string
+ Description string
+ SampleData okapi.M
+ SubjectEN string
+ HTMLEN string
+ TextEN string
+ SubjectFR string
+ HTMLFR string
+ TextFR string
+}
- b, _ := json.MarshalIndent(sample, "", " ")
+// seedTemplate creates a single template with version and localizations.
+func (s *Seeder) seedTemplate(userID uint, ssID uint, def templateDef) {
+ b, _ := json.MarshalIndent(def.SampleData, "", " ")
sampleData := string(b)
- // Create default template linked to the stylesheet
tmpl := &models.Template{
UserID: userID,
- Name: "Welcome Email",
+ Name: def.Name,
DefaultLanguage: "en",
- Description: "Welcome email introducing Posta and its features",
+ Description: def.Description,
SampleData: sampleData,
}
if err := s.templateRepo.Create(tmpl); err != nil {
- logger.Error("failed to seed default template", "user_id", userID, "error", err)
+ logger.Error("failed to seed template", "name", def.Name, "user_id", userID, "error", err)
return
}
- // Create a default version with an English localization
v := &models.TemplateVersion{
TemplateID: tmpl.ID,
Version: 1,
- StyleSheetID: &ss.ID,
+ StyleSheetID: &ssID,
SampleData: sampleData,
}
if err := s.versionRepo.Create(v); err != nil {
- logger.Error("failed to seed default template version", "user_id", userID, "error", err)
+ logger.Error("failed to seed template version", "name", def.Name, "user_id", userID, "error", err)
return
}
- l := &models.TemplateLocalization{
+ enLoc := &models.TemplateLocalization{
VersionID: v.ID,
Language: "en",
- SubjectTemplate: "Welcome to Posta, {{name}}!",
- HTMLTemplate: defaultHTMLTemplate,
- TextTemplate: defaultTextTemplate,
+ SubjectTemplate: def.SubjectEN,
+ HTMLTemplate: def.HTMLEN,
+ TextTemplate: def.TextEN,
}
- if err := s.localizationRepo.Create(l); err != nil {
- logger.Error("failed to seed default localization", "user_id", userID, "error", err)
+ if err := s.localizationRepo.Create(enLoc); err != nil {
+ logger.Error("failed to seed English localization", "name", def.Name, "user_id", userID, "error", err)
return
}
- lFr := &models.TemplateLocalization{
+ frLoc := &models.TemplateLocalization{
VersionID: v.ID,
Language: "fr",
- SubjectTemplate: "Bienvenue sur Posta, {{name}} !",
- HTMLTemplate: defaultHTMLTemplateFr,
- TextTemplate: defaultTextTemplateFr,
+ SubjectTemplate: def.SubjectFR,
+ HTMLTemplate: def.HTMLFR,
+ TextTemplate: def.TextFR,
}
- if err := s.localizationRepo.Create(lFr); err != nil {
- logger.Error("failed to seed French localization", "user_id", userID, "error", err)
+ if err := s.localizationRepo.Create(frLoc); err != nil {
+ logger.Error("failed to seed French localization", "name", def.Name, "user_id", userID, "error", err)
}
- // Activate the version
vID := v.ID
tmpl.ActiveVersionID = &vID
if err := s.templateRepo.Update(tmpl); err != nil {
- logger.Error("failed to activate default version", "user_id", userID, "error", err)
+ logger.Error("failed to activate template version", "name", def.Name, "user_id", userID, "error", err)
+ }
+}
+
+// SeedUserDefaults creates default stylesheet and templates for a user
+func (s *Seeder) SeedUserDefaults(userID uint, userName string) {
+ if userName == "" {
+ userName = "Jonas"
+ }
+ templates, total, err := s.templateRepo.FindByUserID(userID, 1, 0)
+ if err != nil || total > 0 || len(templates) > 0 {
+ return
+ }
+
+ // Create default stylesheet
+ ss := &models.StyleSheet{
+ UserID: userID,
+ Name: "default",
+ CSS: defaultCSS,
+ }
+ if err := s.stylesheetRepo.Create(ss); err != nil {
+ logger.Error("failed to seed default stylesheet", "user_id", userID, "error", err)
return
}
+ year := time.Now().Year()
+ docsURL := fmt.Sprintf("%s/docs", goutils.Env("POSTA_WEB_URL", ""))
+
+ // 1. Welcome template
+ s.seedTemplate(userID, ss.ID, templateDef{
+ Name: "Welcome Email",
+ Description: "Welcome email introducing Posta and its features",
+ SampleData: okapi.M{
+ "name": userName,
+ "product": "Posta",
+ "company": "Posta",
+ "year": year,
+ "docs": docsURL,
+ "features": []string{
+ "REST API for transactional, batch, and templated emails",
+ "Scheduled sending and preview mode",
+ "Async processing with automatic retries and priority queues",
+ "Versioned and multi-language templates with variable substitution",
+ "Multiple SMTP providers with TLS and shared pools",
+ "Domain verification (SPF, DKIM, DMARC)",
+ "API keys with expiration, hashing, and IP allowlisting",
+ "JWT authentication, RBAC, and two-factor authentication",
+ "Contact tracking, segmentation, and suppression lists",
+ "Multi-tenant workspaces with scoped API keys",
+ "Event-driven webhooks with retry and delivery tracking",
+ "Email delivery analytics and Prometheus metrics",
+ "Web dashboard with dark/light mode",
+ "Official SDKs for Go, PHP, and Java",
+ },
+ "links": []map[string]string{
+ {"title": "Website", "url": "https://goposta.dev/"},
+ {"title": "API Documentation", "url": "https://app.goposta.dev/docs"},
+ {"title": "Documentation", "url": "https://docs.goposta.dev/"},
+ {"title": "GitHub Repository", "url": "https://github.com/goposta/posta"},
+ },
+ },
+ SubjectEN: "Welcome to Posta, {{name}}!",
+ HTMLEN: defaultHTMLTemplate,
+ TextEN: defaultTextTemplate,
+ SubjectFR: "Bienvenue sur Posta, {{name}} !",
+ HTMLFR: defaultHTMLTemplateFr,
+ TextFR: defaultTextTemplateFr,
+ })
+
+ // 2. Password Reset template
+ s.seedTemplate(userID, ss.ID, templateDef{
+ Name: "Password Reset",
+ Description: "Transactional email for password reset requests",
+ SampleData: okapi.M{
+ "name": userName,
+ "company": "Posta",
+ "year": year,
+ "resetLink": "https://example.com/reset?token=abc123",
+ "expiry": "1 hour",
+ },
+ SubjectEN: "Reset your password, {{name}}",
+ HTMLEN: passwordResetHTMLTemplate,
+ TextEN: passwordResetTextTemplate,
+ SubjectFR: "Réinitialisez votre mot de passe, {{name}}",
+ HTMLFR: passwordResetHTMLTemplateFr,
+ TextFR: passwordResetTextTemplateFr,
+ })
+
+ // 3. Order Confirmation template
+ s.seedTemplate(userID, ss.ID, templateDef{
+ Name: "Order Confirmation",
+ Description: "Order confirmation email with item details and total",
+ SampleData: okapi.M{
+ "name": userName,
+ "company": "Posta",
+ "year": year,
+ "orderNumber": "10042",
+ "total": "$129.97",
+ "items": []map[string]interface{}{
+ {"name": "Wireless Keyboard", "qty": 1, "price": "$59.99"},
+ {"name": "USB-C Hub", "qty": 2, "price": "$34.99"},
+ },
+ },
+ SubjectEN: "Order #{{orderNumber}} confirmed",
+ HTMLEN: orderConfirmationHTMLTemplate,
+ TextEN: orderConfirmationTextTemplate,
+ SubjectFR: "Commande #{{orderNumber}} confirmée",
+ HTMLFR: orderConfirmationHTMLTemplateFr,
+ TextFR: orderConfirmationTextTemplateFr,
+ })
+
+ // 4. Newsletter template
+ s.seedTemplate(userID, ss.ID, templateDef{
+ Name: "Monthly Newsletter",
+ Description: "Monthly newsletter with articles and unsubscribe link",
+ SampleData: okapi.M{
+ "name": userName,
+ "company": "Posta",
+ "year": year,
+ "month": "April",
+ "articles": []map[string]string{
+ {
+ "title": "Introducing Webhooks",
+ "summary": "Track every email event in real time with our new webhook system. Configure endpoints, set retry policies, and monitor delivery.",
+ "url": "https://docs.goposta.dev/webhooks",
+ },
+ {
+ "title": "Template Versioning Guide",
+ "summary": "Learn how to manage template versions, roll back changes, and preview before publishing.",
+ "url": "https://docs.goposta.dev/templates/versioning",
+ },
+ {
+ "title": "Multi-language Emails",
+ "summary": "Send localized emails to your global audience with built-in language support and fallback chains.",
+ "url": "https://docs.goposta.dev/templates/localization",
+ },
+ },
+ "unsubscribeUrl": "https://example.com/unsubscribe?token=xyz",
+ },
+ SubjectEN: "{{company}} — {{month}} Newsletter",
+ HTMLEN: newsletterHTMLTemplate,
+ TextEN: newsletterTextTemplate,
+ SubjectFR: "{{company}} — Newsletter de {{month}}",
+ HTMLFR: newsletterHTMLTemplateFr,
+ TextFR: newsletterTextTemplateFr,
+ })
+
// Seed default languages
defaultLanguages := []struct {
Code string
@@ -191,5 +281,5 @@ func (s *Seeder) SeedUserDefaults(userID uint, userName string) {
}
}
- logger.Info("seeded default stylesheet, template, version, localization, and languages", "user_id", userID)
+ logger.Info("seeded default stylesheet, templates, versions, localizations, and languages", "user_id", userID)
}