gitea源码

mail_user.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package mailer
  4. import (
  5. "bytes"
  6. "fmt"
  7. user_model "code.gitea.io/gitea/models/user"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/templates"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "code.gitea.io/gitea/modules/translation"
  13. sender_service "code.gitea.io/gitea/services/mailer/sender"
  14. )
  15. const (
  16. mailAuthActivate templates.TplName = "user/auth/activate"
  17. mailAuthActivateEmail templates.TplName = "user/auth/activate_email"
  18. mailAuthResetPassword templates.TplName = "user/auth/reset_passwd"
  19. mailAuthRegisterNotify templates.TplName = "user/auth/register_notify"
  20. )
  21. // sendUserMail sends a mail to the user
  22. func sendUserMail(language string, u *user_model.User, tpl templates.TplName, code, subject, info string) {
  23. locale := translation.NewLocale(language)
  24. data := map[string]any{
  25. "locale": locale,
  26. "DisplayName": u.DisplayName(),
  27. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale),
  28. "ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, locale),
  29. "Code": code,
  30. "Language": locale.Language(),
  31. }
  32. var content bytes.Buffer
  33. if err := LoadedTemplates().BodyTemplates.ExecuteTemplate(&content, string(tpl), data); err != nil {
  34. log.Error("Template: %v", err)
  35. return
  36. }
  37. msg := sender_service.NewMessage(u.EmailTo(), subject, content.String())
  38. msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
  39. SendAsync(msg)
  40. }
  41. // SendActivateAccountMail sends an activation mail to the user (new user registration)
  42. func SendActivateAccountMail(locale translation.Locale, u *user_model.User) {
  43. if setting.MailService == nil {
  44. // No mail service configured
  45. return
  46. }
  47. opts := &user_model.TimeLimitCodeOptions{Purpose: user_model.TimeLimitCodeActivateAccount}
  48. sendUserMail(locale.Language(), u, mailAuthActivate, user_model.GenerateUserTimeLimitCode(opts, u), locale.TrString("mail.activate_account"), "activate account")
  49. }
  50. // SendResetPasswordMail sends a password reset mail to the user
  51. func SendResetPasswordMail(u *user_model.User) {
  52. if setting.MailService == nil {
  53. // No mail service configured
  54. return
  55. }
  56. locale := translation.NewLocale(u.Language)
  57. opts := &user_model.TimeLimitCodeOptions{Purpose: user_model.TimeLimitCodeResetPassword}
  58. sendUserMail(u.Language, u, mailAuthResetPassword, user_model.GenerateUserTimeLimitCode(opts, u), locale.TrString("mail.reset_password"), "recover account")
  59. }
  60. // SendActivateEmailMail sends confirmation email to confirm new email address
  61. func SendActivateEmailMail(u *user_model.User, email string) {
  62. if setting.MailService == nil {
  63. // No mail service configured
  64. return
  65. }
  66. locale := translation.NewLocale(u.Language)
  67. opts := &user_model.TimeLimitCodeOptions{Purpose: user_model.TimeLimitCodeActivateEmail, NewEmail: email}
  68. data := map[string]any{
  69. "locale": locale,
  70. "DisplayName": u.DisplayName(),
  71. "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale),
  72. "Code": user_model.GenerateUserTimeLimitCode(opts, u),
  73. "Email": email,
  74. "Language": locale.Language(),
  75. }
  76. var content bytes.Buffer
  77. if err := LoadedTemplates().BodyTemplates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
  78. log.Error("Template: %v", err)
  79. return
  80. }
  81. msg := sender_service.NewMessage(email, locale.TrString("mail.activate_email"), content.String())
  82. msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
  83. SendAsync(msg)
  84. }
  85. // SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
  86. func SendRegisterNotifyMail(u *user_model.User) {
  87. if setting.MailService == nil || !u.IsActive {
  88. // No mail service configured OR user is inactive
  89. return
  90. }
  91. locale := translation.NewLocale(u.Language)
  92. data := map[string]any{
  93. "locale": locale,
  94. "DisplayName": u.DisplayName(),
  95. "Username": u.Name,
  96. "Language": locale.Language(),
  97. }
  98. var content bytes.Buffer
  99. if err := LoadedTemplates().BodyTemplates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
  100. log.Error("Template: %v", err)
  101. return
  102. }
  103. msg := sender_service.NewMessage(u.EmailTo(), locale.TrString("mail.register_notify", setting.AppName), content.String())
  104. msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
  105. SendAsync(msg)
  106. }