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

69 lines
1.6 KiB
Go

package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain/usecase"
"context"
ers "code.30cm.net/digimon/library-go/errors"
"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(err.Error())
}
// 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 {
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")
}
return &notification.OKResp{}, nil
}