123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package mailer
- import (
- "bytes"
- "fmt"
- "html/template"
- "log"
- "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>
- `
- type Mailer struct {
- client *mail.Client
- from string
- }
- type EmailData struct {
- Username string
- Password string
- Subdomains []string
- WebAppURL string
- SetupGuide 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 string) error {
- message := mail.NewMsg()
- message.From(m.from)
- message.To(to)
- message.Subject("Vérification de votre adresse email")
- message.SetBodyString(mail.TypeTextHTML, "Cliquez sur le lien suivant pour vérifier votre adresse email: http://localhost:8080/verify-email?token="+token)
- message.ReplyTo(m.from)
- return m.client.DialAndSend(message)
- }
|