mailer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package mailer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "html/template"
  6. "log"
  7. "github.com/wneessen/go-mail"
  8. "git.linuxforward.com/byom/byom-onboard/internal/platform/config"
  9. )
  10. const welcomeEmailTemplate = `
  11. <!DOCTYPE html>
  12. <html lang="fr">
  13. <head>
  14. <meta charset="UTF-8">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <title>Bienvenue sur Byom!</title>
  17. <style>
  18. body {
  19. font-family: Arial, sans-serif;
  20. line-height: 1.6;
  21. color: #333;
  22. }
  23. .container {
  24. width: 80%;
  25. margin: 0 auto;
  26. padding: 20px;
  27. border: 1px solid #ddd;
  28. border-radius: 5px;
  29. background-color: #f9f9f9;
  30. }
  31. h1 {
  32. color: #0056b3;
  33. }
  34. .info {
  35. margin-bottom: 20px;
  36. }
  37. .info p {
  38. margin: 5px 0;
  39. }
  40. .subdomains {
  41. margin-bottom: 20px;
  42. }
  43. .subdomains p {
  44. margin: 5px 0;
  45. }
  46. .footer {
  47. margin-top: 20px;
  48. font-size: 0.9em;
  49. color: #777;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="container">
  55. <h1>Bienvenue sur Byom!</h1>
  56. <p>Votre application a été configurée avec succès et est prête à être utilisée.</p>
  57. <div class="info">
  58. <h2>Informations d'accès :</h2>
  59. <p><strong>Nom d'utilisateur :</strong> {{.Username}}</p>
  60. <p><strong>Mot de passe :</strong> {{.Password}}</p>
  61. </div>
  62. <div class="subdomains">
  63. <h2>URL de votre application :</h2>
  64. {{range .Subdomains}}
  65. <p>{{.}}</p>
  66. {{end}}
  67. <p><strong>Application principale :</strong> {{.WebAppURL}}</p>
  68. </div>
  69. <div class="setup-guide">
  70. <h2>Pour bien commencer :</h2>
  71. <p>{{.SetupGuide}}</p>
  72. </div>
  73. <div class="footer">
  74. <p>Pour des raisons de sécurité, nous vous recommandons de changer votre mot de passe lors de votre première connexion.</p>
  75. <p>Si vous avez besoin d'aide, n'hésitez pas à contacter notre équipe d'assistance.</p>
  76. <p>Cordialement,<br>Byom</p>
  77. </div>
  78. </div>
  79. </body>
  80. </html>
  81. `
  82. type Mailer struct {
  83. client *mail.Client
  84. from string
  85. }
  86. type EmailData struct {
  87. Username string
  88. Password string
  89. Subdomains []string
  90. WebAppURL string
  91. SetupGuide string
  92. }
  93. func NewMailer(config *config.MailerConfig) *Mailer {
  94. log.Printf("Test mail successfully delivered.")
  95. fmt.Println(config)
  96. client, err := mail.NewClient(config.Host,
  97. mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithTLSPortPolicy(mail.DefaultTLSPolicy),
  98. mail.WithUsername(config.Username), mail.WithPassword(config.Password),
  99. )
  100. if err != nil {
  101. log.Fatalf("Failed to create mail client: %v", err)
  102. }
  103. return &Mailer{
  104. client: client,
  105. from: config.From,
  106. }
  107. }
  108. func (m *Mailer) SendEmail(to string, data *EmailData) error {
  109. tmpl := template.Must(template.New("welcomeEmail").Parse(welcomeEmailTemplate))
  110. var tpl bytes.Buffer
  111. err := tmpl.Execute(&tpl, data)
  112. if err != nil {
  113. return err
  114. }
  115. body := tpl.String()
  116. message := mail.NewMsg()
  117. message.From(m.from)
  118. message.To(to)
  119. message.Subject("Bienvenue sur BYOM!")
  120. message.SetBodyString(mail.TypeTextHTML, body)
  121. message.ReplyTo(m.from)
  122. return m.client.DialAndSend(message)
  123. }
  124. func (m *Mailer) SendVerifyEmail(to, token string) error {
  125. message := mail.NewMsg()
  126. message.From(m.from)
  127. message.To(to)
  128. message.Subject("Vérification de votre adresse email")
  129. message.SetBodyString(mail.TypeTextHTML, "Cliquez sur le lien suivant pour vérifier votre adresse email: http://localhost:8080/verify-email?token="+token)
  130. message.ReplyTo(m.from)
  131. return m.client.DialAndSend(message)
  132. }