From 2ff37d613aaa151112e41706e97a32aa1110cedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 19 Nov 2024 20:22:18 +0100 Subject: [PATCH] init --- .golangci.yaml | 2 ++ internal/email/client.go | 2 +- internal/email/types.go | 22 +++++++++++----------- internal/smtp/server.go | 10 ++++++---- internal/smtp/server_test.go | 2 +- main.go | 7 +++++-- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 522fd9b..b4142f3 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -9,6 +9,8 @@ linters: - mnd - exportloopref - err113 + - tparallel + - paralleltest issues: exclude-rules: diff --git a/internal/email/client.go b/internal/email/client.go index b2b6a90..2da9109 100644 --- a/internal/email/client.go +++ b/internal/email/client.go @@ -55,7 +55,7 @@ func (client *Client) SendEmail(ctx context.Context, email *Email) error { err = json.NewDecoder(resp.Body).Decode(&commError) if err != nil { - return err + return fmt.Errorf("failed to decode error response: %w", err) } return fmt.Errorf("status code: %d, error message: %s", resp.StatusCode, commError.Error.Message) diff --git a/internal/email/types.go b/internal/email/types.go index 6e618cc..14f2e64 100644 --- a/internal/email/types.go +++ b/internal/email/types.go @@ -13,21 +13,21 @@ type Client struct { } type Email struct { - Recipients Recipients `json:"recipients"` - SenderAddress string `json:"senderAddress"` - Content Content `json:"content"` - Tracking bool `json:"disableUserEngagementTracking"` - Importance string `json:"importance"` - ReplyTo []EmailAddress `json:"replyTo"` + Recipients Recipients `json:"recipients"` + SenderAddress string `json:"senderAddress"` + Content Content `json:"content"` + Tracking bool `json:"disableUserEngagementTracking"` + Importance string `json:"importance"` + ReplyTo []Address `json:"replyTo"` } type Recipients struct { - To []EmailAddress `json:"to"` - CC []EmailAddress `json:"cc"` - BCC []EmailAddress `json:"bcc"` + To []Address `json:"to"` + CC []Address `json:"cc"` + BCC []Address `json:"bcc"` } -type EmailAddress struct { +type Address struct { DisplayName string `json:"displayName"` Address string `json:"address"` } @@ -42,7 +42,7 @@ type ErrorResponse struct { Error CommunicationError `json:"error"` } -// CommunicationError contains the error code and message +// CommunicationError contains the error code and message. type CommunicationError struct { Code string `json:"code"` Message string `json:"message"` diff --git a/internal/smtp/server.go b/internal/smtp/server.go index 0f088da..1019153 100644 --- a/internal/smtp/server.go +++ b/internal/smtp/server.go @@ -110,6 +110,8 @@ func (s *Server) Shutdown() error { } // handleConnection processes an SMTP client connection. +// +//nolint:gocognit func (s *Server) handleConnection(conn net.Conn) error { var ( err error @@ -316,8 +318,8 @@ func parseMailData(data string) (*MailMessage, error) { return "" } - from := getHeader("From") - to := getHeader("To") + mailFrom := getHeader("From") + mailTo := getHeader("To") subject := getHeader("Subject") contentType := getHeader("Content-Type") @@ -328,8 +330,8 @@ func parseMailData(data string) (*MailMessage, error) { } mailMessage := &MailMessage{ - From: from, - To: to, + From: mailFrom, + To: mailTo, Subject: subject, } diff --git a/internal/smtp/server_test.go b/internal/smtp/server_test.go index fd4cc85..885e3ab 100644 --- a/internal/smtp/server_test.go +++ b/internal/smtp/server_test.go @@ -108,7 +108,7 @@ Your Name`, msg := []byte(fmt.Sprintf("From: %s\r\nTo: %s\r\n"+ "Subject: %s\r\n"+ - "%s\r\n", test.from, test.to, test.subject, test.body)) + "%s\r\n", test.from, test.to, test.subject, body)) err := smtp.SendMail("localhost:1515", nil, test.from, []string{test.to}, msg) require.NoError(t, err) diff --git a/main.go b/main.go index 5a7f94c..96a0317 100644 --- a/main.go +++ b/main.go @@ -67,8 +67,11 @@ func run(logger *slog.Logger) error { return emailClient.SendEmail(context.Background(), &email.Email{ SenderAddress: mail.From, Recipients: email.Recipients{ - To: []email.EmailAddress{ - {mail.To, mail.To}, + To: []email.Address{ + { + Address: mail.To, + DisplayName: mail.To, + }, }, }, Content: email.Content{