Push email

This commit is contained in:
2024-10-26 23:01:40 +08:00
parent 7597bff972
commit 478d18d75f
8 changed files with 118 additions and 6 deletions

View File

@@ -26,11 +26,15 @@ func (v *Server) PushNotificationBatch(ctx context.Context, request *proto.PushN
}
func (v *Server) DeliverEmail(ctx context.Context, request *proto.DeliverEmailRequest) (*proto.DeliveryResponse, error) {
//TODO implement me
panic("implement me")
go provider.SendMail(request.GetTo(), pushkit.NewEmailDataFromProto(request.GetEmail()))
return &proto.DeliveryResponse{IsSuccess: true}, nil
}
func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEmailBatchRequest) (*proto.DeliveryResponse, error) {
//TODO implement me
panic("implement me")
go func() {
for _, to := range request.GetTo() {
_ = provider.SendMail(to, pushkit.NewEmailDataFromProto(request.GetEmail()))
}
}()
return &proto.DeliveryResponse{IsSuccess: true}, nil
}

View File

@@ -39,7 +39,7 @@ func (v *AppleNotifyProvider) Push(in pushkit.Notification, tk string) error {
Topic: viper.GetString(v.topic),
Payload: rawData,
}
_, err := v.conn.Push(payload)
_, err = v.conn.Push(payload)
return err
}

View File

@@ -0,0 +1,36 @@
package provider
import (
"crypto/tls"
"fmt"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"github.com/jordan-wright/email"
"github.com/spf13/viper"
"net/smtp"
"net/textproto"
)
func SendMail(target string, in pushkit.EmailData) error {
mail := &email.Email{
To: []string{target},
From: viper.GetString("mailer.name"),
Subject: in.Subject,
Headers: textproto.MIMEHeader{},
}
if in.Text != nil {
mail.Text = []byte(*in.Text)
}
if in.HTML != nil {
mail.HTML = []byte(*in.HTML)
}
return mail.SendWithTLS(
fmt.Sprintf("%s:%d", viper.GetString("mailer.smtp_host"), viper.GetInt("mailer.smtp_port")),
smtp.PlainAuth(
"",
viper.GetString("mailer.username"),
viper.GetString("mailer.password"),
viper.GetString("mailer.smtp_host"),
),
&tls.Config{ServerName: viper.GetString("mailer.smtp_host")},
)
}

View File

@@ -47,5 +47,37 @@ func SubscribeToQueue() error {
return fmt.Errorf("failed to subscribe notification batch topic: %v", err)
}
_, err = mq.Nt.Subscribe(pushkit.PushEmailMqTopic, func(msg *nats.Msg) {
var req pushkit.EmailDeliverRequest
if json.Unmarshal(msg.Data, &req) != nil {
return
} else if validate.Struct(&req) != nil {
return
}
go provider.SendMail(req.To, req.Email)
})
if err != nil {
return fmt.Errorf("failed to subscribe email topic: %v", err)
}
_, err = mq.Nt.Subscribe(pushkit.PushEmailBatchMqTopic, func(msg *nats.Msg) {
var req pushkit.EmailDeliverBatchRequest
if json.Unmarshal(msg.Data, &req) != nil {
return
} else if validate.Struct(&req) != nil {
return
}
go func() {
for _, to := range req.To {
_ = provider.SendMail(to, req.Email)
}
}()
})
if err != nil {
return fmt.Errorf("failed to subscribe email batch topic: %v", err)
}
return nil
}