✨ Push email
This commit is contained in:
parent
7597bff972
commit
478d18d75f
1
go.mod
1
go.mod
@ -8,6 +8,7 @@ require (
|
|||||||
github.com/fatih/color v1.17.0
|
github.com/fatih/color v1.17.0
|
||||||
github.com/go-playground/validator/v10 v10.22.1
|
github.com/go-playground/validator/v10 v10.22.1
|
||||||
github.com/goccy/go-json v0.10.3
|
github.com/goccy/go-json v0.10.3
|
||||||
|
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
|
||||||
github.com/nats-io/nats.go v1.37.0
|
github.com/nats-io/nats.go v1.37.0
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/rs/zerolog v1.33.0
|
github.com/rs/zerolog v1.33.0
|
||||||
|
2
go.sum
2
go.sum
@ -175,6 +175,8 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR
|
|||||||
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
|
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
|
||||||
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
|
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
|
||||||
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
|
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
|
||||||
|
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
|
||||||
|
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
|
||||||
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
|
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
|
||||||
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
|
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
|
||||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||||
|
@ -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) {
|
func (v *Server) DeliverEmail(ctx context.Context, request *proto.DeliverEmailRequest) (*proto.DeliveryResponse, error) {
|
||||||
//TODO implement me
|
go provider.SendMail(request.GetTo(), pushkit.NewEmailDataFromProto(request.GetEmail()))
|
||||||
panic("implement me")
|
return &proto.DeliveryResponse{IsSuccess: true}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEmailBatchRequest) (*proto.DeliveryResponse, error) {
|
func (v *Server) DeliverEmailBatch(ctx context.Context, request *proto.DeliverEmailBatchRequest) (*proto.DeliveryResponse, error) {
|
||||||
//TODO implement me
|
go func() {
|
||||||
panic("implement me")
|
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),
|
Topic: viper.GetString(v.topic),
|
||||||
Payload: rawData,
|
Payload: rawData,
|
||||||
}
|
}
|
||||||
_, err := v.conn.Push(payload)
|
_, err = v.conn.Push(payload)
|
||||||
return err
|
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)
|
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
|
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()),
|
||||||
|
}
|
||||||
|
}
|
@ -7,3 +7,10 @@ nexus_addr = "127.0.0.1:7001"
|
|||||||
[debug]
|
[debug]
|
||||||
database = false
|
database = false
|
||||||
print_routes = false
|
print_routes = false
|
||||||
|
|
||||||
|
[mailer]
|
||||||
|
name = "Alphabot <alphabot@smartsheep.studio>"
|
||||||
|
smtp_host = "smtp.exmail.qq.com"
|
||||||
|
smtp_port = 465
|
||||||
|
username = "alphabot@smartsheep.studio"
|
||||||
|
password = "gz937Zxxzfcd9SeH"
|
||||||
|
Loading…
Reference in New Issue
Block a user