129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package template
|
|
|
|
import (
|
|
"github.com/matcornic/hermes/v2"
|
|
)
|
|
|
|
// ==============================
|
|
// Email 結構定義
|
|
// ==============================
|
|
|
|
// EmailTemplate 代表 Email 樣板
|
|
type EmailTemplate struct {
|
|
Title string
|
|
Body string
|
|
}
|
|
|
|
// ProductInfo 包含產品相關的資訊
|
|
type ProductInfo struct {
|
|
Name string
|
|
Link string
|
|
Logo string
|
|
Copyright string
|
|
}
|
|
|
|
// EmailBodyContent 包含郵件正文的資訊
|
|
type EmailBodyContent struct {
|
|
RecipientName string
|
|
Intros []string
|
|
Actions []hermes.Action
|
|
Outros []string
|
|
Signature string
|
|
}
|
|
|
|
// EmailContentParams 組成 Email 內容的參數
|
|
type EmailContentParams struct {
|
|
Product ProductInfo
|
|
Content EmailBodyContent
|
|
}
|
|
|
|
// ==============================
|
|
// Email 內容產生器
|
|
// ==============================
|
|
|
|
// GenerateEmailBody 根據參數產生 Email HTML 內容
|
|
func GenerateEmailBody(params EmailContentParams) (string, error) {
|
|
product := hermes.Product{
|
|
Name: params.Product.Name,
|
|
Link: params.Product.Link,
|
|
Logo: params.Product.Logo,
|
|
Copyright: params.Product.Copyright,
|
|
}
|
|
|
|
body := hermes.Body{
|
|
Name: params.Content.RecipientName,
|
|
Intros: params.Content.Intros,
|
|
Actions: params.Content.Actions,
|
|
Outros: params.Content.Outros,
|
|
Signature: params.Content.Signature,
|
|
}
|
|
|
|
h := hermes.Hermes{Product: product}
|
|
email := hermes.Email{Body: body}
|
|
|
|
return h.GenerateHTML(email)
|
|
}
|
|
|
|
// GenerateEmailContent 生成 Email 內容(可用於不同驗證類型)
|
|
func GenerateEmailContent(title string, intros []string) (EmailTemplate, error) {
|
|
req := EmailContentParams{
|
|
Product: ProductInfo{
|
|
Name: ProductName,
|
|
Link: ProductLink,
|
|
Logo: ProductLogo,
|
|
Copyright: ProductCopyright,
|
|
},
|
|
Content: EmailBodyContent{
|
|
RecipientName: "{{.Username}}",
|
|
Intros: intros,
|
|
Actions: []hermes.Action{
|
|
{
|
|
Instructions: "請複製您的驗證碼,到網頁重置",
|
|
InviteCode: "{{.VerifyCode}}",
|
|
},
|
|
},
|
|
Outros: []string{
|
|
"如果您沒有請求此操作,請忽略此郵件。",
|
|
},
|
|
Signature: "",
|
|
},
|
|
}
|
|
|
|
emailBody, err := GenerateEmailBody(req)
|
|
if err != nil {
|
|
return EmailTemplate{}, err
|
|
}
|
|
|
|
return EmailTemplate{
|
|
Title: title,
|
|
Body: emailBody,
|
|
}, nil
|
|
}
|
|
|
|
// ==============================
|
|
// Email 產生函數
|
|
// ==============================
|
|
|
|
// GenerateForgetPasswordEmailZHTW 生成繁體中文的忘記密碼驗證信
|
|
func GenerateForgetPasswordEmailZHTW() (EmailTemplate, error) {
|
|
return GenerateEmailContent("TrueHeart 重設密碼驗證信",
|
|
[]string{"您收到此電子郵件是因為我們收到了針對帳戶的密碼重置請求。"})
|
|
}
|
|
|
|
// GenerateBindingEmailZHTW 生成綁定帳號驗證信
|
|
func GenerateBindingEmailZHTW() (EmailTemplate, error) {
|
|
return GenerateEmailContent("TrueHeart 綁定信箱驗證信",
|
|
[]string{"您收到此電子郵件是因為我們收到了針對帳戶的 Email 認證請求。"})
|
|
}
|
|
|
|
// ==============================
|
|
// Email 模板對應表
|
|
// ==============================
|
|
|
|
var EmailTemplateMap = map[Language]map[Type]func() (EmailTemplate, error){
|
|
LanguageZhTW: {
|
|
ForgetPasswordVerify: GenerateForgetPasswordEmailZHTW,
|
|
BindingEmail: GenerateBindingEmailZHTW,
|
|
},
|
|
}
|