57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package usecase
|
|
|
|
import (
|
|
"app-cloudep-notification-service/internal/config"
|
|
"app-cloudep-notification-service/internal/domain/usecase"
|
|
|
|
pool "code.30cm.net/digimon/library-go/worker_pool"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"context"
|
|
|
|
"gopkg.in/gomail.v2"
|
|
)
|
|
|
|
type MailUseCaseParam struct {
|
|
Conf config.Config
|
|
}
|
|
|
|
type MailUseCase struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
Password string
|
|
Pool pool.WorkerPool
|
|
}
|
|
|
|
func (mu *MailUseCase) SendMail(_ context.Context, req usecase.MailReq) error {
|
|
// 用 goroutine pool 送,否則會超時
|
|
err := mu.Pool.Submit(func() {
|
|
m := gomail.NewMessage()
|
|
m.SetHeader("From", req.From)
|
|
m.SetHeader("To", req.To)
|
|
m.SetHeader("Subject", req.Subject)
|
|
m.SetBody("text/html", req.Body)
|
|
d := gomail.NewDialer(mu.Host, mu.Port, mu.User, mu.Password)
|
|
if err := d.DialAndSend(m); err != nil {
|
|
logx.WithCallerSkip(1).WithFields(
|
|
logx.Field("func", "MailUseCase.SendMail"),
|
|
logx.Field("req", req),
|
|
logx.Field("err", err),
|
|
).Error("failed to send mail by mailgun")
|
|
}
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
func MustMailgunUseCase(param MailUseCaseParam) usecase.MailClientUseCase {
|
|
return &MailUseCase{
|
|
Host: param.Conf.SMTP.Host,
|
|
Port: param.Conf.SMTP.Port,
|
|
User: param.Conf.SMTP.User,
|
|
Password: param.Conf.SMTP.Password,
|
|
Pool: pool.NewWorkerPool(2000),
|
|
}
|
|
}
|