2024-08-20 13:08:00 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"app-cloudep-notification-service/internal/config"
|
|
|
|
"app-cloudep-notification-service/internal/domain/usecase"
|
2024-08-20 15:53:20 +00:00
|
|
|
|
|
|
|
pool "code.30cm.net/digimon/library-go/worker_pool"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
2024-08-20 13:08:00 +00:00
|
|
|
"context"
|
2024-08-20 15:53:20 +00:00
|
|
|
|
2024-08-20 13:08:00 +00:00
|
|
|
"gopkg.in/gomail.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MailUseCaseParam struct {
|
|
|
|
Conf config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type MailUseCase struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
User string
|
|
|
|
Password string
|
2024-08-20 15:53:20 +00:00
|
|
|
Pool pool.WorkerPool
|
2024-08-20 13:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mu *MailUseCase) SendMail(_ context.Context, req usecase.MailReq) error {
|
2024-08-20 15:53:20 +00:00
|
|
|
// 用 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
|
2024-08-20 13:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2024-08-20 15:53:20 +00:00
|
|
|
Pool: pool.NewWorkerPool(2000),
|
2024-08-20 13:08:00 +00:00
|
|
|
}
|
|
|
|
}
|