Initial commit

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 13:57:10 +08:00
co-authored by Claude Opus 5
commit 8679200f41
1897 changed files with 257900 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
package email
import (
"bytes"
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/pkg/errors"
gomail "gopkg.in/gomail.v2"
)
// Send sends email without attachments.
func (c *EmailClient) Send(ctx context.Context, sender string, mailList []*string, title string, body string) error {
if err := check(sender, mailList); err != nil {
return err
}
sesEmailInput := &ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: mailList,
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Data: aws.String(body)},
},
Subject: &ses.Content{
Data: aws.String(title),
},
},
Source: aws.String(sender),
}
if _, err := c.ses.SendEmail(sesEmailInput); err != nil {
return errors.Wrap(err, "send email failed")
}
return nil
}
// SendRaw send email that supports attachments.
func (c *EmailClient) SendRaw(ctx context.Context, sender string, mailList []*string, title string, body string, attachments []string) error {
if err := check(sender, mailList); err != nil {
return err
}
msg := gomail.NewMessage(gomail.SetCharset("UTF-8"))
msg.SetHeader("From", sender)
toList := make([]string, len(mailList))
for i, l := range mailList {
toList[i] = *l
}
msg.SetHeader("To", toList...)
msg.SetHeader("Subject", title)
msg.SetBody("text/html", body)
for _, a := range attachments {
msg.Attach(a)
}
var emailRaw bytes.Buffer
_, _ = msg.WriteTo(&emailRaw)
if _, err := c.ses.SendRawEmail(&ses.SendRawEmailInput{
RawMessage: &ses.RawMessage{Data: emailRaw.Bytes()},
}); err != nil {
return errors.Wrap(err, "send raw email failed")
}
return nil
}
func check(sender string, mailList []*string) error {
if sender == "" {
return errors.New("no sender")
}
if len(mailList) == 0 {
return errors.New("no recipient")
}
return nil
}
+37
View File
@@ -0,0 +1,37 @@
package email
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
)
type Config struct {
Region string `json:"region"` // aws region
AccessKeyID string `json:"accessKeyID"` // aws accessKeyID
SecretAccessKey string `json:"secretAccessKey"` // aws secretAccessKey
VerifiedDomain string `json:"verifiedDomain"` // aws verifiedDomain. The sender's email must in this domain.
}
type EmailClient struct {
ses *ses.SES
}
var Client *EmailClient
// MustInit 初始化ses连接
func MustInit(ctx context.Context, cfg Config) error {
session, err := session.NewSession(&aws.Config{
Region: &cfg.Region,
Credentials: credentials.NewStaticCredentials(cfg.AccessKeyID, cfg.SecretAccessKey, ""),
})
if err != nil {
panic(err)
}
Client = &EmailClient{}
Client.ses = ses.New(session)
return nil
}