app-cloudep-notification-se.../internal/logic/senderservice/send_mail_logic.go

73 lines
1.7 KiB
Go

package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"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),
}
}
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"`
}
// SendMail 寄信
func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OKResp, error) {
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("invalid format")
}
// TODO 以後可以做換線
err := l.svcCtx.MailSender.SendMail(l.ctx, usecase.MailReq{
To: in.GetTo(),
Subject: in.GetSubject(),
From: in.GetFrom(),
Body: in.GetBody(),
})
if err != nil {
return nil, domain.SendMailErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
logx.Field("func", "MailSender.SendMail"),
logx.Field("in", in),
logx.Field("err", err),
},
"MailSender.SendMail failed to send mail",
)
}
return &notification.OKResp{}, nil
}