123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- package mailer
- import (
- "bytes"
- "fmt"
- "html/template"
- "log"
- "net/url"
- "strings"
- "github.com/wneessen/go-mail"
- "git.linuxforward.com/byom/byom-onboard/internal/platform/config"
- )
- const welcomeEmailTemplate = `
- <!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Bienvenue sur Byom!</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- line-height: 1.6;
- color: #333;
- }
- .container {
- width: 80%;
- margin: 0 auto;
- padding: 20px;
- border: 1px solid #ddd;
- border-radius: 5px;
- background-color: #f9f9f9;
- }
- h1 {
- color: #0056b3;
- }
- .info {
- margin-bottom: 20px;
- }
- .info p {
- margin: 5px 0;
- }
- .subdomains {
- margin-bottom: 20px;
- }
- .subdomains p {
- margin: 5px 0;
- }
- .footer {
- margin-top: 20px;
- font-size: 0.9em;
- color: #777;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>Bienvenue sur Byom!</h1>
- <p>Votre application a été configurée avec succès et est prête à être utilisée.</p>
- <div class="info">
- <h2>Informations d'accès :</h2>
- <p><strong>Nom d'utilisateur :</strong> {{.Username}}</p>
- <p><strong>Mot de passe :</strong> {{.Password}}</p>
- </div>
- <div class="subdomains">
- <h2>URL de votre application :</h2>
- {{range .Subdomains}}
- <p>{{.}}</p>
- {{end}}
- <p><strong>Application principale :</strong> {{.WebAppURL}}</p>
- </div>
- <div class="setup-guide">
- <h2>Pour bien commencer :</h2>
- <p>{{.SetupGuide}}</p>
- </div>
- <div class="footer">
- <p>Pour des raisons de sécurité, nous vous recommandons de changer votre mot de passe lors de votre première connexion.</p>
- <p>Si vous avez besoin d'aide, n'hésitez pas à contacter notre équipe d'assistance.</p>
- <p>Cordialement,<br>Byom</p>
- </div>
- </div>
- </body>
- </html>
- `
- const verifyEmailTemplate = `
- <!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Vérification de votre email - Byom</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- line-height: 1.6;
- color: #333;
- background-color: #f5f5f5;
- margin: 0;
- padding: 20px;
- }
- .container {
- max-width: 600px;
- margin: 0 auto;
- background-color: #ffffff;
- border-radius: 12px;
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
- overflow: hidden;
- }
- .header {
- background-color: #0056b3;
- color: white;
- padding: 30px;
- text-align: center;
- }
- .header h1 {
- margin: 0;
- font-size: 28px;
- color: white;
- }
- .content {
- padding: 40px 30px;
- }
- .message {
- text-align: center;
- margin-bottom: 30px;
- font-size: 16px;
- color: #555;
- }
- .button-container {
- text-align: center;
- margin: 30px 0;
- }
- .button {
- display: inline-block;
- padding: 15px 35px;
- background-color: #0056b3;
- color: white;
- text-decoration: none;
- border-radius: 50px;
- font-weight: bold;
- font-size: 16px;
- transition: all 0.3s ease;
- box-shadow: 0 2px 4px rgba(0, 86, 179, 0.3);
- }
- .button:hover {
- background-color: #003d80;
- transform: translateY(-2px);
- box-shadow: 0 4px 8px rgba(0, 86, 179, 0.4);
- }
- .verification-link {
- text-align: center;
- margin: 20px 0;
- padding: 15px;
- background-color: #f8f9fa;
- border-radius: 8px;
- font-size: 14px;
- color: #666;
- word-break: break-all;
- }
- .divider {
- height: 1px;
- background-color: #eee;
- margin: 30px 0;
- }
- .footer {
- text-align: center;
- padding: 20px 30px;
- background-color: #f8f9fa;
- color: #666;
- font-size: 14px;
- }
- .logo {
- margin-bottom: 20px;
- }
- .security-notice {
- background-color: #fff3cd;
- border-left: 4px solid #ffc107;
- padding: 15px;
- margin: 20px 0;
- font-size: 14px;
- color: #856404;
- }
- .signature {
- margin-top: 20px;
- color: #0056b3;
- font-weight: bold;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <div class="logo">
- <!-- Vous pouvez ajouter votre logo ici -->
- <h1>BYOM</h1>
- </div>
- <h2>Vérification de votre email</h2>
- </div>
-
- <div class="content">
- <div class="message">
- <h2>Merci de vous être inscrit!</h2>
- <p>Pour finaliser votre inscription et accéder à toutes les fonctionnalités de Byom, veuillez vérifier votre adresse email.</p>
- </div>
- <div class="button-container">
- <a href="{{.VerificationURL}}" class="button">Vérifier mon email</a>
- </div>
- <div class="security-notice">
- <strong>🔒 Note de sécurité:</strong>
- <p>Ce lien expirera dans 24 heures pour des raisons de sécurité.</p>
- </div>
- <div class="verification-link">
- <p>Si le bouton ne fonctionne pas, copiez et collez ce lien dans votre navigateur:</p>
- <p>{{.VerificationURL}}</p>
- </div>
- <div class="divider"></div>
- <div class="footer">
- <p>Si vous n'avez pas créé de compte sur Byom, vous pouvez ignorer cet email en toute sécurité.</p>
- <div class="signature">
- <p>Cordialement,<br>L'équipe Byom</p>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
- `
- const workspaceWelcomeTemplate = `
- <!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Votre espace de travail Byom est prêt !</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- line-height: 1.6;
- color: #333;
- background-color: #f5f5f5;
- margin: 0;
- padding: 20px;
- }
- .container {
- max-width: 600px;
- margin: 0 auto;
- background-color: #ffffff;
- border-radius: 12px;
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
- overflow: hidden;
- }
- .header {
- background-color: #0056b3;
- color: white;
- padding: 30px;
- text-align: center;
- }
- .header h1 {
- margin: 0;
- font-size: 28px;
- color: white;
- }
- .content {
- padding: 40px 30px;
- }
- .welcome-message {
- text-align: center;
- margin-bottom: 30px;
- font-size: 18px;
- color: #333;
- }
- .button-container {
- text-align: center;
- margin: 30px 0;
- }
- .button {
- display: inline-block;
- padding: 15px 35px;
- background-color: #0056b3;
- color: white;
- text-decoration: none;
- border-radius: 50px;
- font-weight: bold;
- font-size: 16px;
- transition: all 0.3s ease;
- box-shadow: 0 2px 4px rgba(0, 86, 179, 0.3);
- }
- .button:hover {
- background-color: #003d80;
- transform: translateY(-2px);
- box-shadow: 0 4px 8px rgba(0, 86, 179, 0.4);
- }
- .features {
- margin: 30px 0;
- padding: 20px;
- background-color: #f8f9fa;
- border-radius: 8px;
- }
- .features h3 {
- color: #0056b3;
- margin-bottom: 15px;
- }
- .features ul {
- list-style-type: none;
- padding: 0;
- }
- .features li {
- margin: 10px 0;
- padding-left: 25px;
- position: relative;
- }
- .features li:before {
- content: "✓";
- color: #0056b3;
- position: absolute;
- left: 0;
- }
- .footer {
- text-align: center;
- padding: 20px 30px;
- background-color: #f8f9fa;
- color: #666;
- font-size: 14px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <h1>BYOM</h1>
- <h2>Votre espace de travail est prêt !</h2>
- </div>
-
- <div class="content">
- <div class="welcome-message">
- <h2>Félicitations !</h2>
- <p>Votre paiement a été confirmé et votre espace de travail Byom est maintenant prêt à être utilisé.</p>
- </div>
- <div class="button-container">
- <a href="{{.WebAppURL}}" class="button">Accéder à mon espace</a>
- </div>
- <div class="features">
- <h3>Ce qui vous attend :</h3>
- <ul>
- <li>Interface intuitive et personnalisable</li>
- <li>Outils de collaboration avancés</li>
- <li>Support technique dédié</li>
- <li>Sécurité renforcée</li>
- </ul>
- </div>
- <div class="footer">
- <p>Si vous avez des questions, notre équipe de support est là pour vous aider.</p>
- <p>Cordialement,<br>L'équipe Byom</p>
- </div>
- </div>
- </div>
- </body>
- </html>
- `
- type Mailer struct {
- client *mail.Client
- from string
- }
- type EmailData struct {
- Username string
- Password string
- Subdomains []string
- WebAppURL string
- SetupGuide string
- }
- type VerifyEmailData struct {
- VerificationURL string
- }
- func NewMailer(config *config.MailerConfig) *Mailer {
- log.Printf("Test mail successfully delivered.")
- fmt.Println(config)
- client, err := mail.NewClient(config.Host,
- mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithTLSPortPolicy(mail.DefaultTLSPolicy),
- mail.WithUsername(config.Username), mail.WithPassword(config.Password),
- )
- if err != nil {
- log.Fatalf("Failed to create mail client: %v", err)
- }
- return &Mailer{
- client: client,
- from: config.From,
- }
- }
- func (m *Mailer) SendEmail(to string, data *EmailData) error {
- tmpl := template.Must(template.New("welcomeEmail").Parse(welcomeEmailTemplate))
- var tpl bytes.Buffer
- err := tmpl.Execute(&tpl, data)
- if err != nil {
- return err
- }
- body := tpl.String()
- message := mail.NewMsg()
- message.From(m.from)
- message.To(to)
- message.Subject("Bienvenue sur BYOM!")
- message.SetBodyString(mail.TypeTextHTML, body)
- message.ReplyTo(m.from)
- return m.client.DialAndSend(message)
- }
- func (m *Mailer) SendVerifyEmail(to, token, plan string) error {
- if strings.HasSuffix(to, "@example.com") {
- return fmt.Errorf("cannot send email to example.com domain: %s", to)
- }
- message := mail.NewMsg()
- message.From(m.from)
- message.To(to)
- message.Subject("Vérification de votre adresse email")
- verificationURL := fmt.Sprintf("http://192.168.1.35:5173/verify-email?token=%s&plan=%s&email=%s",
- token,
- plan,
- url.QueryEscape(to),
- )
- htmlBody := fmt.Sprintf(`
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Vérification de votre email</title>
- </head>
- <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
- <div style="background-color: #ffffff; border-radius: 5px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
- <h1 style="color: #2c3e50; margin-bottom: 20px;">Bienvenue !</h1>
-
- <p style="margin-bottom: 20px;">
- Merci de vous être inscrit. Pour finaliser votre inscription, veuillez vérifier votre adresse email en cliquant sur le bouton ci-dessous.
- </p>
- <div style="text-align: center; margin: 30px 0;">
- <a href="%s"
- style="background-color: #3498db;
- color: white;
- padding: 12px 30px;
- text-decoration: none;
- border-radius: 5px;
- display: inline-block;
- font-weight: bold;">
- Vérifier mon email
- </a>
- </div>
- <p style="color: #666; font-size: 14px;">
- Si le bouton ne fonctionne pas, vous pouvez copier et coller le lien suivant dans votre navigateur :
- <br>
- <span style="color: #3498db;">%s</span>
- </p>
- <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">
- <p style="color: #666; font-size: 12px; text-align: center;">
- Si vous n'avez pas créé de compte, vous pouvez ignorer cet email.
- </p>
- </div>
- </body>
- </html>
- `, verificationURL, verificationURL)
- message.SetBodyString(mail.TypeTextHTML, htmlBody)
- message.ReplyTo(m.from)
- return m.client.DialAndSend(message)
- }
- func (m *Mailer) SendWelcomeEmail(to string, data *EmailData) error {
- tmpl := template.Must(template.New("workspaceWelcome").Parse(workspaceWelcomeTemplate))
- var tpl bytes.Buffer
- err := tmpl.Execute(&tpl, data)
- if err != nil {
- return err
- }
- message := mail.NewMsg()
- message.From(m.from)
- message.To(to)
- message.Subject("Bienvenue sur votre espace de travail Byom !")
- message.SetBodyString(mail.TypeTextHTML, tpl.String())
- message.ReplyTo(m.from)
- return m.client.DialAndSend(message)
- }
|