123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- 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: "Helvetica Neue", Helvetica, Arial, sans-serif;
- line-height: 1.6;
- color: #000;
- background-color: #fff;
- margin: 0;
- padding: 40px 20px;
- }
- .container {
- max-width: 600px;
- margin: 0 auto;
- background-color: #fff;
- border: 2px solid #000;
- }
- .header {
- padding: 40px;
- text-align: left;
- border-bottom: 2px solid #000;
- }
- .header h1 {
- margin: 0;
- font-size: 32px;
- font-weight: bold;
- text-transform: uppercase;
- letter-spacing: -1px;
- }
- .content {
- padding: 40px;
- }
- .section {
- margin-bottom: 40px;
- border-bottom: 1px solid #000;
- padding-bottom: 40px;
- }
- .info-grid {
- display: grid;
- grid-template-columns: auto 1fr;
- gap: 10px;
- margin: 20px 0;
- }
- .info-label {
- font-weight: bold;
- text-transform: uppercase;
- font-size: 14px;
- }
- .info-value {
- font-family: monospace;
- background: #f0f0f0;
- padding: 4px 8px;
- border: 1px solid #000;
- }
- .urls {
- margin: 20px 0;
- padding: 20px;
- border: 1px solid #000;
- }
- .footer {
- padding: 40px;
- border-top: 2px solid #000;
- font-size: 14px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <h1>BYOM</h1>
- </div>
- <div class="content">
- <div class="section">
- <h2>Configuration terminée</h2>
- <p>Votre application est prête à être utilisée.</p>
- </div>
-
- <div class="section">
- <h2>Informations d'accès</h2>
- <div class="info-grid">
- <div class="info-label">Utilisateur</div>
- <div class="info-value">{{.Username}}</div>
-
- <div class="info-label">Mot de passe</div>
- <div class="info-value">{{.Password}}</div>
- </div>
- </div>
- <div class="section">
- <h2>URLs d'accès</h2>
- <div class="urls">
- {{range .Subdomains}}
- <p class="info-value">{{.}}</p>
- {{end}}
- <p><strong>Application principale:</strong></p>
- <p class="info-value">{{.WebAppURL}}</p>
- </div>
- </div>
- {{if .SetupGuide}}
- <div class="section">
- <h2>Guide de démarrage</h2>
- <p>{{.SetupGuide}}</p>
- </div>
- {{end}}
- </div>
- <div class="footer">
- <p>Pour des raisons de sécurité, changez votre mot de passe lors de votre première connexion.</p>
- <p>Support technique: support@byom.fr</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 Email - BYOM</title>
- <style>
- body {
- font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
- line-height: 1.6;
- color: #000;
- background-color: #fff;
- margin: 0;
- padding: 40px 20px;
- }
- .container {
- max-width: 600px;
- margin: 0 auto;
- background-color: #fff;
- border: 2px solid #000;
- }
- .header {
- padding: 40px;
- text-align: left;
- border-bottom: 2px solid #000;
- }
- .header h1 {
- margin: 0;
- font-size: 32px;
- font-weight: bold;
- text-transform: uppercase;
- letter-spacing: -1px;
- }
- .content {
- padding: 40px;
- border-bottom: 2px solid #000;
- }
- .message {
- font-size: 18px;
- margin-bottom: 40px;
- }
- .button {
- display: inline-block;
- padding: 16px 32px;
- background-color: #000;
- color: #fff;
- text-decoration: none;
- text-transform: uppercase;
- font-weight: bold;
- letter-spacing: 1px;
- margin: 20px 0;
- }
- .verification-link {
- margin: 20px 0;
- padding: 20px;
- background-color: #f0f0f0;
- border: 1px solid #000;
- word-break: break-all;
- font-family: monospace;
- }
- .footer {
- padding: 40px;
- font-size: 14px;
- color: #666;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <h1>BYOM</h1>
- </div>
- <div class="content">
- <div class="message">
- <h2>Vérification de votre email</h2>
- <p>Pour finaliser votre inscription, veuillez vérifier votre adresse email.</p>
- </div>
- <a href="{{.VerificationURL}}" class="button">Vérifier mon email</a>
- <div class="verification-link">
- <p>Si le bouton ne fonctionne pas :</p>
- <code>{{.VerificationURL}}</code>
- </div>
- </div>
- <div class="footer">
- <p>Si vous n'avez pas créé de compte sur BYOM, ignorez cet email.</p>
- </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>Bienvenue sur BYOM</title>
- <style>
- body {
- font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
- line-height: 1.6;
- color: #000;
- background-color: #fff;
- margin: 0;
- padding: 40px 20px;
- }
- .container {
- max-width: 600px;
- margin: 0 auto;
- background-color: #fff;
- border: 2px solid #000;
- }
- .header {
- padding: 40px;
- text-align: left;
- border-bottom: 2px solid #000;
- }
- .header h1 {
- margin: 0;
- font-size: 32px;
- font-weight: bold;
- text-transform: uppercase;
- letter-spacing: -1px;
- }
- .content {
- padding: 40px;
- }
- .section {
- margin-bottom: 40px;
- border-bottom: 1px solid #000;
- padding-bottom: 40px;
- }
- .button {
- display: inline-block;
- padding: 16px 32px;
- background-color: #000;
- color: #fff;
- text-decoration: none;
- text-transform: uppercase;
- font-weight: bold;
- letter-spacing: 1px;
- margin: 20px 0;
- }
- .features {
- display: grid;
- grid-template-columns: repeat(2, 1fr);
- gap: 20px;
- margin: 40px 0;
- }
- .feature {
- padding: 20px;
- border: 1px solid #000;
- }
- .footer {
- padding: 40px;
- border-top: 2px solid #000;
- font-size: 14px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="header">
- <h1>BYOM</h1>
- </div>
- <div class="content">
- <div class="section">
- <h2>Votre espace est prêt</h2>
- <p>Votre paiement a été confirmé et votre espace BYOM est maintenant disponible.</p>
- <a href="{{.WebAppURL}}" class="button">Accéder à mon espace</a>
- </div>
- <div class="features">
- <div class="feature">
- <h3>Interface</h3>
- <p>Design minimaliste et efficace</p>
- </div>
- <div class="feature">
- <h3>Collaboration</h3>
- <p>Outils de travail en équipe</p>
- </div>
- <div class="feature">
- <h3>Support</h3>
- <p>Assistance technique dédiée</p>
- </div>
- <div class="feature">
- <h3>Sécurité</h3>
- <p>Protection des données</p>
- </div>
- </div>
- </div>
- <div class="footer">
- <p>Support technique: support@byom.fr</p>
- </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("https://byom.moooffle.com/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)
- }
|