✨ Push email
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
36
pkg/internal/provider/mailer.go
Normal file
36
pkg/internal/provider/mailer.go
Normal 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")},
|
||||
)
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
30
pkg/pushkit/email.go
Normal file
30
pkg/pushkit/email.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package pushkit
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/pusher/pkg/proto"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type EmailDeliverRequest struct {
|
||||
To string `json:"to" validate:"required"`
|
||||
Email EmailData `json:"email" validate:"required"`
|
||||
}
|
||||
|
||||
type EmailDeliverBatchRequest struct {
|
||||
To []string `json:"to" validate:"required"`
|
||||
Email EmailData `json:"email" validate:"required"`
|
||||
}
|
||||
|
||||
type EmailData struct {
|
||||
Subject string `json:"subject" validate:"required"`
|
||||
Text *string `json:"text"`
|
||||
HTML *string `json:"html"`
|
||||
}
|
||||
|
||||
func NewEmailDataFromProto(in *proto.EmailInfo) EmailData {
|
||||
return EmailData{
|
||||
Subject: in.GetSubject(),
|
||||
Text: lo.ToPtr(in.GetTextBody()),
|
||||
HTML: lo.ToPtr(in.GetHtmlBody()),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user