mailer.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package mailer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "html/template"
  6. "log"
  7. "net/url"
  8. "strings"
  9. "github.com/wneessen/go-mail"
  10. "git.linuxforward.com/byom/byom-onboard/internal/platform/config"
  11. )
  12. const welcomeEmailTemplate = `
  13. <!DOCTYPE html>
  14. <html lang="fr">
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <title>Bienvenue sur BYOM</title>
  19. <style>
  20. body {
  21. font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  22. line-height: 1.6;
  23. color: #000;
  24. background-color: #fff;
  25. margin: 0;
  26. padding: 40px 20px;
  27. }
  28. .container {
  29. max-width: 600px;
  30. margin: 0 auto;
  31. background-color: #fff;
  32. border: 2px solid #000;
  33. }
  34. .header {
  35. padding: 40px;
  36. text-align: left;
  37. border-bottom: 2px solid #000;
  38. }
  39. .header h1 {
  40. margin: 0;
  41. font-size: 32px;
  42. font-weight: bold;
  43. text-transform: uppercase;
  44. letter-spacing: -1px;
  45. }
  46. .content {
  47. padding: 40px;
  48. }
  49. .section {
  50. margin-bottom: 40px;
  51. border-bottom: 1px solid #000;
  52. padding-bottom: 40px;
  53. }
  54. .info-grid {
  55. display: grid;
  56. grid-template-columns: auto 1fr;
  57. gap: 10px;
  58. margin: 20px 0;
  59. }
  60. .info-label {
  61. font-weight: bold;
  62. text-transform: uppercase;
  63. font-size: 14px;
  64. }
  65. .info-value {
  66. font-family: monospace;
  67. background: #f0f0f0;
  68. padding: 4px 8px;
  69. border: 1px solid #000;
  70. }
  71. .urls {
  72. margin: 20px 0;
  73. padding: 20px;
  74. border: 1px solid #000;
  75. }
  76. .footer {
  77. padding: 40px;
  78. border-top: 2px solid #000;
  79. font-size: 14px;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <div class="container">
  85. <div class="header">
  86. <h1>BYOM</h1>
  87. </div>
  88. <div class="content">
  89. <div class="section">
  90. <h2>Configuration terminée</h2>
  91. <p>Votre application est prête à être utilisée.</p>
  92. </div>
  93. <div class="section">
  94. <h2>Informations d'accès</h2>
  95. <div class="info-grid">
  96. <div class="info-label">Utilisateur</div>
  97. <div class="info-value">{{.Username}}</div>
  98. <div class="info-label">Mot de passe</div>
  99. <div class="info-value">{{.Password}}</div>
  100. </div>
  101. </div>
  102. <div class="section">
  103. <h2>URLs d'accès</h2>
  104. <div class="urls">
  105. {{range .Subdomains}}
  106. <p class="info-value">{{.}}</p>
  107. {{end}}
  108. <p><strong>Application principale:</strong></p>
  109. <p class="info-value">{{.WebAppURL}}</p>
  110. </div>
  111. </div>
  112. {{if .SetupGuide}}
  113. <div class="section">
  114. <h2>Guide de démarrage</h2>
  115. <p>{{.SetupGuide}}</p>
  116. </div>
  117. {{end}}
  118. </div>
  119. <div class="footer">
  120. <p>Pour des raisons de sécurité, changez votre mot de passe lors de votre première connexion.</p>
  121. <p>Support technique: support@byom.fr</p>
  122. </div>
  123. </div>
  124. </body>
  125. </html>
  126. `
  127. const verifyEmailTemplate = `
  128. <!DOCTYPE html>
  129. <html lang="fr">
  130. <head>
  131. <meta charset="UTF-8">
  132. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  133. <title>Vérification Email - BYOM</title>
  134. <style>
  135. body {
  136. font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  137. line-height: 1.6;
  138. color: #000;
  139. background-color: #fff;
  140. margin: 0;
  141. padding: 40px 20px;
  142. }
  143. .container {
  144. max-width: 600px;
  145. margin: 0 auto;
  146. background-color: #fff;
  147. border: 2px solid #000;
  148. }
  149. .header {
  150. padding: 40px;
  151. text-align: left;
  152. border-bottom: 2px solid #000;
  153. }
  154. .header h1 {
  155. margin: 0;
  156. font-size: 32px;
  157. font-weight: bold;
  158. text-transform: uppercase;
  159. letter-spacing: -1px;
  160. }
  161. .content {
  162. padding: 40px;
  163. border-bottom: 2px solid #000;
  164. }
  165. .message {
  166. font-size: 18px;
  167. margin-bottom: 40px;
  168. }
  169. .button {
  170. display: inline-block;
  171. padding: 16px 32px;
  172. background-color: #000;
  173. color: #fff;
  174. text-decoration: none;
  175. text-transform: uppercase;
  176. font-weight: bold;
  177. letter-spacing: 1px;
  178. margin: 20px 0;
  179. }
  180. .verification-link {
  181. margin: 20px 0;
  182. padding: 20px;
  183. background-color: #f0f0f0;
  184. border: 1px solid #000;
  185. word-break: break-all;
  186. font-family: monospace;
  187. }
  188. .footer {
  189. padding: 40px;
  190. font-size: 14px;
  191. color: #666;
  192. }
  193. </style>
  194. </head>
  195. <body>
  196. <div class="container">
  197. <div class="header">
  198. <h1>BYOM</h1>
  199. </div>
  200. <div class="content">
  201. <div class="message">
  202. <h2>Vérification de votre email</h2>
  203. <p>Pour finaliser votre inscription, veuillez vérifier votre adresse email.</p>
  204. </div>
  205. <a href="{{.VerificationURL}}" class="button">Vérifier mon email</a>
  206. <div class="verification-link">
  207. <p>Si le bouton ne fonctionne pas :</p>
  208. <code>{{.VerificationURL}}</code>
  209. </div>
  210. </div>
  211. <div class="footer">
  212. <p>Si vous n'avez pas créé de compte sur BYOM, ignorez cet email.</p>
  213. </div>
  214. </div>
  215. </body>
  216. </html>
  217. `
  218. const workspaceWelcomeTemplate = `
  219. <!DOCTYPE html>
  220. <html lang="fr">
  221. <head>
  222. <meta charset="UTF-8">
  223. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  224. <title>Bienvenue sur BYOM</title>
  225. <style>
  226. body {
  227. font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  228. line-height: 1.6;
  229. color: #000;
  230. background-color: #fff;
  231. margin: 0;
  232. padding: 40px 20px;
  233. }
  234. .container {
  235. max-width: 600px;
  236. margin: 0 auto;
  237. background-color: #fff;
  238. border: 2px solid #000;
  239. }
  240. .header {
  241. padding: 40px;
  242. text-align: left;
  243. border-bottom: 2px solid #000;
  244. }
  245. .header h1 {
  246. margin: 0;
  247. font-size: 32px;
  248. font-weight: bold;
  249. text-transform: uppercase;
  250. letter-spacing: -1px;
  251. }
  252. .content {
  253. padding: 40px;
  254. }
  255. .section {
  256. margin-bottom: 40px;
  257. border-bottom: 1px solid #000;
  258. padding-bottom: 40px;
  259. }
  260. .button {
  261. display: inline-block;
  262. padding: 16px 32px;
  263. background-color: #000;
  264. color: #fff;
  265. text-decoration: none;
  266. text-transform: uppercase;
  267. font-weight: bold;
  268. letter-spacing: 1px;
  269. margin: 20px 0;
  270. }
  271. .features {
  272. display: grid;
  273. grid-template-columns: repeat(2, 1fr);
  274. gap: 20px;
  275. margin: 40px 0;
  276. }
  277. .feature {
  278. padding: 20px;
  279. border: 1px solid #000;
  280. }
  281. .footer {
  282. padding: 40px;
  283. border-top: 2px solid #000;
  284. font-size: 14px;
  285. }
  286. </style>
  287. </head>
  288. <body>
  289. <div class="container">
  290. <div class="header">
  291. <h1>BYOM</h1>
  292. </div>
  293. <div class="content">
  294. <div class="section">
  295. <h2>Votre espace est prêt</h2>
  296. <p>Votre paiement a été confirmé et votre espace BYOM est maintenant disponible.</p>
  297. <a href="{{.WebAppURL}}" class="button">Accéder à mon espace</a>
  298. </div>
  299. <div class="features">
  300. <div class="feature">
  301. <h3>Interface</h3>
  302. <p>Design minimaliste et efficace</p>
  303. </div>
  304. <div class="feature">
  305. <h3>Collaboration</h3>
  306. <p>Outils de travail en équipe</p>
  307. </div>
  308. <div class="feature">
  309. <h3>Support</h3>
  310. <p>Assistance technique dédiée</p>
  311. </div>
  312. <div class="feature">
  313. <h3>Sécurité</h3>
  314. <p>Protection des données</p>
  315. </div>
  316. </div>
  317. </div>
  318. <div class="footer">
  319. <p>Support technique: support@byom.fr</p>
  320. </div>
  321. </div>
  322. </body>
  323. </html>
  324. `
  325. type Mailer struct {
  326. client *mail.Client
  327. from string
  328. }
  329. type EmailData struct {
  330. Username string
  331. Password string
  332. Subdomains []string
  333. WebAppURL string
  334. SetupGuide string
  335. }
  336. type VerifyEmailData struct {
  337. VerificationURL string
  338. }
  339. func NewMailer(config *config.MailerConfig) *Mailer {
  340. log.Printf("Test mail successfully delivered.")
  341. fmt.Println(config)
  342. client, err := mail.NewClient(config.Host,
  343. mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithTLSPortPolicy(mail.DefaultTLSPolicy),
  344. mail.WithUsername(config.Username), mail.WithPassword(config.Password),
  345. )
  346. if err != nil {
  347. log.Fatalf("Failed to create mail client: %v", err)
  348. }
  349. return &Mailer{
  350. client: client,
  351. from: config.From,
  352. }
  353. }
  354. func (m *Mailer) SendEmail(to string, data *EmailData) error {
  355. tmpl := template.Must(template.New("welcomeEmail").Parse(welcomeEmailTemplate))
  356. var tpl bytes.Buffer
  357. err := tmpl.Execute(&tpl, data)
  358. if err != nil {
  359. return err
  360. }
  361. body := tpl.String()
  362. message := mail.NewMsg()
  363. message.From(m.from)
  364. message.To(to)
  365. message.Subject("Bienvenue sur BYOM!")
  366. message.SetBodyString(mail.TypeTextHTML, body)
  367. message.ReplyTo(m.from)
  368. return m.client.DialAndSend(message)
  369. }
  370. func (m *Mailer) SendVerifyEmail(to, token, plan string) error {
  371. if strings.HasSuffix(to, "@example.com") {
  372. return fmt.Errorf("cannot send email to example.com domain: %s", to)
  373. }
  374. message := mail.NewMsg()
  375. message.From(m.from)
  376. message.To(to)
  377. message.Subject("Vérification de votre adresse email")
  378. verificationURL := fmt.Sprintf("https://byom.moooffle.com/verify-email?token=%s&plan=%s&email=%s",
  379. token,
  380. plan,
  381. url.QueryEscape(to),
  382. )
  383. htmlBody := fmt.Sprintf(`
  384. <!DOCTYPE html>
  385. <html>
  386. <head>
  387. <meta charset="UTF-8">
  388. <title>Vérification de votre email</title>
  389. </head>
  390. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  391. <div style="background-color: #ffffff; border-radius: 5px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
  392. <h1 style="color: #2c3e50; margin-bottom: 20px;">Bienvenue !</h1>
  393. <p style="margin-bottom: 20px;">
  394. Merci de vous être inscrit. Pour finaliser votre inscription, veuillez vérifier votre adresse email en cliquant sur le bouton ci-dessous.
  395. </p>
  396. <div style="text-align: center; margin: 30px 0;">
  397. <a href="%s"
  398. style="background-color: #3498db;
  399. color: white;
  400. padding: 12px 30px;
  401. text-decoration: none;
  402. border-radius: 5px;
  403. display: inline-block;
  404. font-weight: bold;">
  405. Vérifier mon email
  406. </a>
  407. </div>
  408. <p style="color: #666; font-size: 14px;">
  409. Si le bouton ne fonctionne pas, vous pouvez copier et coller le lien suivant dans votre navigateur :
  410. <br>
  411. <span style="color: #3498db;">%s</span>
  412. </p>
  413. <hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">
  414. <p style="color: #666; font-size: 12px; text-align: center;">
  415. Si vous n'avez pas créé de compte, vous pouvez ignorer cet email.
  416. </p>
  417. </div>
  418. </body>
  419. </html>
  420. `, verificationURL, verificationURL)
  421. message.SetBodyString(mail.TypeTextHTML, htmlBody)
  422. message.ReplyTo(m.from)
  423. return m.client.DialAndSend(message)
  424. }
  425. func (m *Mailer) SendWelcomeEmail(to string, data *EmailData) error {
  426. tmpl := template.Must(template.New("workspaceWelcome").Parse(workspaceWelcomeTemplate))
  427. var tpl bytes.Buffer
  428. err := tmpl.Execute(&tpl, data)
  429. if err != nil {
  430. return err
  431. }
  432. message := mail.NewMsg()
  433. message.From(m.from)
  434. message.To(to)
  435. message.Subject("Bienvenue sur votre espace de travail Byom !")
  436. message.SetBodyString(mail.TypeTextHTML, tpl.String())
  437. message.ReplyTo(m.from)
  438. return m.client.DialAndSend(message)
  439. }