2024-08-20 06:35:14 +00:00
|
|
|
package senderservicelogic
|
|
|
|
|
|
|
|
import (
|
2024-08-20 13:08:00 +00:00
|
|
|
"app-cloudep-notification-service/internal/domain/usecase"
|
2024-08-20 06:35:14 +00:00
|
|
|
"context"
|
|
|
|
|
2024-08-20 15:53:20 +00:00
|
|
|
ers "code.30cm.net/digimon/library-go/errors"
|
|
|
|
|
2024-08-20 06:35:14 +00:00
|
|
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
|
|
|
"app-cloudep-notification-service/internal/svc"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SendMailLogic struct {
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSendMailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMailLogic {
|
|
|
|
return &SendMailLogic{
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 13:08:00 +00:00
|
|
|
type sendMailReq struct {
|
|
|
|
// TO 收件者
|
|
|
|
To string `validate:"required,email"`
|
|
|
|
// Subject 信件主旨
|
|
|
|
Subject string `validate:"required,max=128"`
|
|
|
|
// Body 內容
|
|
|
|
Body string `validate:"required"`
|
|
|
|
// From 寄件者
|
|
|
|
From string `validate:"required"`
|
|
|
|
}
|
|
|
|
|
2024-08-20 06:35:14 +00:00
|
|
|
// SendMail 寄信
|
|
|
|
func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OKResp, error) {
|
2024-08-20 13:08:00 +00:00
|
|
|
if err := l.svcCtx.Validate.ValidateAll(&sendMailReq{
|
|
|
|
To: in.GetTo(),
|
|
|
|
Subject: in.GetSubject(),
|
|
|
|
Body: in.GetBody(),
|
|
|
|
From: in.GetFrom(),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, ers.InvalidFormat(err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:53:20 +00:00
|
|
|
// TODO 以後可以做換線
|
2024-08-20 13:08:00 +00:00
|
|
|
err := l.svcCtx.MailSender.SendMail(l.ctx, usecase.MailReq{
|
|
|
|
To: in.GetTo(),
|
|
|
|
Subject: in.GetSubject(),
|
|
|
|
From: in.GetFrom(),
|
|
|
|
Body: in.GetBody(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logx.WithCallerSkip(1).WithFields(
|
|
|
|
logx.Field("func", "MailSender.SendMail"),
|
|
|
|
logx.Field("in", in),
|
|
|
|
logx.Field("err", err),
|
|
|
|
).Error(err.Error())
|
|
|
|
return nil, ers.ArkInternal("MailSender.SendMail failed to send mail")
|
|
|
|
}
|
2024-08-20 06:35:14 +00:00
|
|
|
|
|
|
|
return ¬ification.OKResp{}, nil
|
|
|
|
}
|