pusher/pushers/email.go
timerzz 4ec0cd1564
Some checks failed
Build image / build (push) Failing after 4m48s
feat 测试
2024-05-20 17:57:25 +08:00

36 lines
915 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}