Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/email channel #135

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
_ "github.com/nyaruka/courier/handlers/dart"
_ "github.com/nyaruka/courier/handlers/discord"
_ "github.com/nyaruka/courier/handlers/dmark"
_ "github.com/nyaruka/courier/handlers/email"
_ "github.com/nyaruka/courier/handlers/external"
_ "github.com/nyaruka/courier/handlers/facebook"
_ "github.com/nyaruka/courier/handlers/facebookapp"
Expand Down
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Config struct {
RabbitmqURL string `help:"rabbitmq url"`
RabbitmqRetryPubAttempts int `help:"rabbitmq retry attempts"`
RabbitmqRetryPubDelay int `help:"rabbitmq retry delay"`

EmailProxyURL string `help:"email proxy url"`
EmailProxyAuthToken string `help:"email proxy auth token"`
}

// NewConfig returns a new default configuration object
Expand Down Expand Up @@ -87,6 +90,8 @@ func NewConfig() *Config {
WaitMediaChannels: []string{},
RabbitmqRetryPubAttempts: 3,
RabbitmqRetryPubDelay: 1000,
EmailProxyURL: "http://localhost:9090",
EmailProxyAuthToken: "",
}
}

Expand Down
119 changes: 119 additions & 0 deletions handlers/email/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package email

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
)

func init() {
courier.RegisterHandler(newHandler())
}

type handler struct {
handlers.BaseHandler
}

func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("EM"), "Email")}
}

func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
s.AddHandlerRoute(h, http.MethodPost, "receive", h.receiveEvent)
return nil
}

func (h *handler) receiveEvent(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
payload := &moPayload{}
err := handlers.DecodeAndValidateJSON(payload, r)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}

urn, err := urns.NewURNFromParts(urns.EmailScheme, payload.From, "", "")
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}

msg := h.Backend().NewIncomingMsg(channel, urn, payload.Body)

for _, attachment := range payload.Attachments {
msg.WithAttachment(attachment)
}

return handlers.WriteMsgsAndResponse(ctx, h, []courier.Msg{msg}, w, r)
}

type moPayload struct {
UUID courier.MsgUUID `json:"uuid,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Body string `json:"body,omitempty"`
Subject string `json:"subject,omitempty"`
Attachments []string `json:"attachments,omitempty"`
ChannelUUID courier.ChannelUUID `json:"channel_uuid,omitempty"`
}

func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored)

attachments := []string{}
for _, attachment := range msg.Attachments() {
_, attachmentURL := handlers.SplitAttachment(attachment)
attachments = append(attachments, attachmentURL)
}

senderEmail := msg.Channel().StringConfigForKey("username", "")
if senderEmail == "" {
return nil, errors.New("could not send msg without a sender email")
}

payload := moPayload{
UUID: msg.UUID(),
From: senderEmail,
To: msg.URN().Path(),
Subject: trunkString(msg.Text(), 32),
Body: msg.Text(),
Attachments: attachments,
ChannelUUID: msg.Channel().UUID(),
}

sendURL := h.Server().Config().EmailProxyURL + "/send"
authToken := h.Server().Config().EmailProxyAuthToken

body, err := json.Marshal(payload)
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken))

rr, err := utils.MakeHTTPRequest(req)
log := courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr).WithError("Message Send Error", err)
if err == nil {
status.SetStatus(courier.MsgWired)
}
status.AddLog(log)
return status, nil
}

func trunkString(str string, maxSize int) string {
if len(str) < maxSize {
return str
}
return str[:maxSize]
}
1 change: 1 addition & 0 deletions handlers/email/email_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package email
Loading