pusher/pushers/email.go

36 lines
915 B
Go
Raw Normal View History

2024-05-20 17:57:25 +08:00
package pushers
import (
"context"
"fmt"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
"github.com/wneessen/go-mail"
)
type Email struct {
opt *config.EmailPush
}
func NewEmailPusher(opt *config.EmailPush) *Email {
return &Email{opt: opt}
}
func (e *Email) Push(ctx context.Context, title, content string) error {
em := mail.NewMsg()
if err := em.From(e.opt.From); err != nil {
return fmt.Errorf("设置from失败:%v", err)
}
if err := em.To(e.opt.To); err != nil {
return fmt.Errorf("设置to失败:%v", err)
}
em.Subject(title)
em.SetBodyString(mail.TypeTextPlain, content)
c, err := mail.NewClient(e.opt.Host, mail.WithPort(int(e.opt.Port)), mail.WithSMTPAuth(mail.SMTPAuthPlain),
mail.WithUsername(e.opt.Username), mail.WithPassword(e.opt.Password))
if err != nil {
return fmt.Errorf("创建email客户端失败%v", err)
}
return c.DialAndSendWithContext(ctx, em)
}