Init V4 community edition (#2265)
* Init V4 community edition * Init V4 community edition
This commit is contained in:
@@ -1,36 +1,125 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/setting"
|
||||
)
|
||||
|
||||
// NewActivationEmail 新建激活邮件
|
||||
func NewActivationEmail(userName, activateURL string) (string, string) {
|
||||
options := model.GetSettingByNames("siteName", "siteURL", "siteTitle", "mail_activation_template")
|
||||
replace := map[string]string{
|
||||
"{siteTitle}": options["siteName"],
|
||||
"{userName}": userName,
|
||||
"{activationUrl}": activateURL,
|
||||
"{siteUrl}": options["siteURL"],
|
||||
"{siteSecTitle}": options["siteTitle"],
|
||||
}
|
||||
return fmt.Sprintf("【%s】注册激活", options["siteName"]),
|
||||
util.Replace(replace, options["mail_activation_template"])
|
||||
type CommonContext struct {
|
||||
SiteBasic *setting.SiteBasic
|
||||
Logo *setting.Logo
|
||||
SiteUrl string
|
||||
}
|
||||
|
||||
// NewResetEmail 新建重设密码邮件
|
||||
func NewResetEmail(userName, resetURL string) (string, string) {
|
||||
options := model.GetSettingByNames("siteName", "siteURL", "siteTitle", "mail_reset_pwd_template")
|
||||
replace := map[string]string{
|
||||
"{siteTitle}": options["siteName"],
|
||||
"{userName}": userName,
|
||||
"{resetUrl}": resetURL,
|
||||
"{siteUrl}": options["siteURL"],
|
||||
"{siteSecTitle}": options["siteTitle"],
|
||||
}
|
||||
return fmt.Sprintf("【%s】密码重置", options["siteName"]),
|
||||
util.Replace(replace, options["mail_reset_pwd_template"])
|
||||
// ResetContext used for variables in reset email
|
||||
type ResetContext struct {
|
||||
*CommonContext
|
||||
User *ent.User
|
||||
Url string
|
||||
}
|
||||
|
||||
// NewResetEmail generates reset email from template
|
||||
func NewResetEmail(ctx context.Context, settings setting.Provider, user *ent.User, url string) (string, string, error) {
|
||||
templates := settings.ResetEmailTemplate(ctx)
|
||||
if len(templates) == 0 {
|
||||
return "", "", fmt.Errorf("reset email template not configured")
|
||||
}
|
||||
|
||||
selected := selectTemplate(templates, user)
|
||||
resetCtx := ResetContext{
|
||||
CommonContext: commonContext(ctx, settings),
|
||||
User: user,
|
||||
Url: url,
|
||||
}
|
||||
|
||||
tmpl, err := template.New("reset").Parse(selected.Body)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to parse email template: %w", err)
|
||||
}
|
||||
|
||||
var res strings.Builder
|
||||
err = tmpl.Execute(&res, resetCtx)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to execute email template: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("[%s] %s", resetCtx.SiteBasic.Name, selected.Title), res.String(), nil
|
||||
}
|
||||
|
||||
// ActivationContext used for variables in activation email
|
||||
type ActivationContext struct {
|
||||
*CommonContext
|
||||
User *ent.User
|
||||
Url string
|
||||
}
|
||||
|
||||
// NewActivationEmail generates activation email from template
|
||||
func NewActivationEmail(ctx context.Context, settings setting.Provider, user *ent.User, url string) (string, string, error) {
|
||||
templates := settings.ActivationEmailTemplate(ctx)
|
||||
if len(templates) == 0 {
|
||||
return "", "", fmt.Errorf("activation email template not configured")
|
||||
}
|
||||
|
||||
selected := selectTemplate(templates, user)
|
||||
activationCtx := ActivationContext{
|
||||
CommonContext: commonContext(ctx, settings),
|
||||
User: user,
|
||||
Url: url,
|
||||
}
|
||||
|
||||
tmpl, err := template.New("activation").Parse(selected.Body)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to parse email template: %w", err)
|
||||
}
|
||||
|
||||
var res strings.Builder
|
||||
err = tmpl.Execute(&res, activationCtx)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to execute email template: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("[%s] %s", activationCtx.SiteBasic.Name, selected.Title), res.String(), nil
|
||||
}
|
||||
|
||||
func commonContext(ctx context.Context, settings setting.Provider) *CommonContext {
|
||||
logo := settings.Logo(ctx)
|
||||
siteUrl := settings.SiteURL(ctx)
|
||||
res := &CommonContext{
|
||||
SiteBasic: settings.SiteBasic(ctx),
|
||||
Logo: settings.Logo(ctx),
|
||||
SiteUrl: siteUrl.String(),
|
||||
}
|
||||
|
||||
// Add site url if logo is not an url
|
||||
if !strings.HasPrefix(logo.Light, "http") {
|
||||
logoPath, _ := url.Parse(logo.Light)
|
||||
res.Logo.Light = siteUrl.ResolveReference(logoPath).String()
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(logo.Normal, "http") {
|
||||
logoPath, _ := url.Parse(logo.Normal)
|
||||
res.Logo.Normal = siteUrl.ResolveReference(logoPath).String()
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func selectTemplate(templates []setting.EmailTemplate, u *ent.User) setting.EmailTemplate {
|
||||
selected := templates[0]
|
||||
if u != nil {
|
||||
for _, t := range templates {
|
||||
if strings.EqualFold(t.Language, u.Settings.Language) {
|
||||
selected = t
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return selected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user