Skip to content

Commit

Permalink
feat(support-notifications): notification content type and long line
Browse files Browse the repository at this point in the history
splitting
Use ContentType defined on notification on HTTP header for HTTP channel
and MIME header for SMTP channel.  Also add chunking with newlines for
long SMTP message lines.

Closes #2699

Signed-off-by: Alex Ullrich <alexullrich@technotects.com>
  • Loading branch information
Alex Ullrich committed Sep 23, 2020
1 parent fd3e495 commit 65fb21f
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 11 deletions.
57 changes: 46 additions & 11 deletions internal/support/notifications/sending_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
mail "net/smtp"
Expand All @@ -45,9 +46,9 @@ func sendViaChannel(
lc.Debug("Sending notification: " + n.Slug + ", via channel: " + c.String())
var tr models.TransmissionRecord
if c.Type == models.ChannelType(models.Email) {
tr = sendMail(n.Content, c.MailAddresses, lc, config.Smtp)
tr = sendMail(n.Content, c.MailAddresses, n.ContentType, lc, config.Smtp)
} else {
tr = restSend(n.Content, c.Url, lc)
tr = restSend(n.Content, c.Url, n.ContentType, lc)
}
t, err := persistTransmission(tr, n, c, receiver, lc, dbClient)
if err == nil {
Expand All @@ -63,9 +64,9 @@ func resendViaChannel(

var tr models.TransmissionRecord
if t.Channel.Type == models.ChannelType(models.Email) {
tr = sendMail(t.Notification.Content, t.Channel.MailAddresses, lc, config.Smtp)
tr = sendMail(t.Notification.Content, t.Channel.MailAddresses, t.Notification.ContentType, lc, config.Smtp)
} else {
tr = restSend(t.Notification.Content, t.Channel.Url, lc)
tr = restSend(t.Notification.Content, t.Channel.Url, t.Notification.ContentType, lc)
}
t.ResendCount = t.ResendCount + 1
t.Status = tr.Status
Expand Down Expand Up @@ -112,15 +113,15 @@ func persistTransmission(
func sendMail(
message string,
addressees []string,
contentType string,
lc logger.LoggingClient,
smtp notificationsConfig.SmtpInfo) models.TransmissionRecord {

tr := getTransmissionRecord("SMTP server received", models.Sent)
buf := bytes.NewBufferString("Subject: " + smtp.Subject + "\r\n")
// required CRLF at ends of lines and CRLF between header and body for SMTP RFC 822 style email
buf.WriteString("\r\n")
buf.WriteString(message)
err := smtpSend(addressees, []byte(buf.String()), smtp)

smtpMessage := buildSmtpMessage(smtp.Subject, contentType, message)

err := smtpSend(addressees, smtpMessage, smtp)
if err != nil {
lc.Error("Problems sending message to: " + strings.Join(addressees, ",") + ", issue: " + err.Error())
tr.Status = models.Failed
Expand All @@ -130,9 +131,43 @@ func sendMail(
return tr
}

func restSend(message string, url string, lc logger.LoggingClient) models.TransmissionRecord {
func buildSmtpMessage(subject string, contentType string, message string) []byte {
smtpNewline := "\r\n"

// required CRLF at ends of lines and CRLF between header and body for SMTP RFC 822 style email
buf := bytes.NewBufferString("Subject: " + subject + smtpNewline)

// only add MIME header if notification content type was set
// maybe provide charset overrides as well?
if contentType != "" {
buf.WriteString(fmt.Sprintf("MIME-version: 1.0;\r\nContent-Type: %s; charset=\"UTF-8\";\r\n", contentType))
}

buf.WriteString(smtpNewline)

//maximum line size is 1000
//split on newline first then break further as needed
for _, line := range strings.Split(message, smtpNewline) {
ln := 998
idx := 0
for len(line) > idx+ln {
buf.WriteString(line[idx:idx+ln] + smtpNewline)
idx += ln
}
buf.WriteString(line[idx:] + smtpNewline)
}

return []byte(buf.String())
}

func restSend(message string, url string, contentType string, lc logger.LoggingClient) models.TransmissionRecord {
tr := getTransmissionRecord("", models.Sent)
rs, err := http.Post(url, "text/plain", bytes.NewBuffer([]byte(message)))

if contentType == "" {
contentType = "text/plain"
}

rs, err := http.Post(url, contentType, bytes.NewBuffer([]byte(message)))
if err != nil {
lc.Error("Problems sending message to: " + url)
lc.Error("Error indication was: " + err.Error())
Expand Down
121 changes: 121 additions & 0 deletions internal/support/notifications/sending_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*******************************************************************************
* Copyright 2020 Technotects
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*******************************************************************************/
package notifications

import (
"fmt"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestBuildSmtpMessageNoContentType(t *testing.T) {
subject := uuid.New().String()
message := uuid.New().String()

result := buildSmtpMessage(subject, "", message)

require.NotNil(t, result)

stringResult := string(result)

expected := fmt.Sprintf("Subject: %s\r\n\r\n%s\r\n", subject, message)
assert.Equal(t, expected, stringResult)
}

func TestBuildSmtpMessageContentType(t *testing.T) {
subject := uuid.New().String()
contentType := uuid.New().String()
message := uuid.New().String()

result := buildSmtpMessage(subject, contentType, message)

require.NotNil(t, result)

stringResult := string(result)

expected := fmt.Sprintf("Subject: %s\r\nMIME-version: 1.0;\r\nContent-Type: %s; charset=\"UTF-8\";\r\n\r\n%s\r\n", subject, contentType, message)
assert.Equal(t, expected, stringResult)
}

func TestBuildSmtpMessageLongMessageIsChunkedIfNeeded(t *testing.T) {
subject := uuid.New().String()
message := uuid.New().String()

for i := 0; i < 5; i++ {
message += message
}

require.Greater(t, len(message), 998)
require.Less(t, len(message), 1896)

result := buildSmtpMessage(subject, "", message)

require.NotNil(t, result)

stringResult := string(result)

expected := fmt.Sprintf("Subject: %s\r\n\r\n%s\r\n%s\r\n", subject, message[0:998], message[998:])
assert.Equal(t, expected, stringResult)
}

func TestBuildSmtpMessageLongMessageIsPreChunked(t *testing.T) {
subject := uuid.New().String()
longLine := uuid.New().String()

for i := 0; i < 5; i++ {
longLine += longLine
}

require.Greater(t, len(longLine), 998)
require.Less(t, len(longLine), 1896)

formattedMessage := fmt.Sprintf("%s\r\n%s", longLine[0:998], longLine[998:])

result := buildSmtpMessage(subject, "", formattedMessage)

require.NotNil(t, result)

stringResult := string(result)

expected := fmt.Sprintf("Subject: %s\r\n\r\n%s\r\n", subject, formattedMessage)
assert.Equal(t, expected, stringResult)
}

func TestBuildSmtpMessageLongMessageIsPartlyChunked(t *testing.T) {
subject := uuid.New().String()
longLine := uuid.New().String()

for i := 0; i < 5; i++ {
longLine += longLine
}

require.Greater(t, len(longLine), 998)
require.Less(t, len(longLine), 1896)

goodLine := uuid.New().String() + uuid.New().String() + "\r\n"

formattedMessage := fmt.Sprintf("%s\r\n%s", longLine[0:998], longLine[998:])

result := buildSmtpMessage(subject, "", goodLine+formattedMessage)

require.NotNil(t, result)

stringResult := string(result)

expected := fmt.Sprintf("Subject: %s\r\n\r\n%s%s\r\n%s\r\n", subject, goodLine, longLine[0:998], longLine[998:])
assert.Equal(t, expected, stringResult)
}

0 comments on commit 65fb21f

Please sign in to comment.