Skip to content

Commit

Permalink
Add plaintext toggle (#476)
Browse files Browse the repository at this point in the history
Co-authored-by: Niko Köser <koeserniko@gmail.com>
  • Loading branch information
janboll and nikoksr authored Dec 9, 2022
1 parent 1e6d668 commit d3ba200
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
42 changes: 37 additions & 5 deletions service/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// Mail struct holds necessary data to send emails.
type Mail struct {
usePlainText bool
senderAddress string
smtpHostAddr string
smtpAuth smtp.Auth
Expand All @@ -20,12 +21,23 @@ type Mail struct {
// New returns a new instance of a Mail notification service.
func New(senderAddress, smtpHostAddress string) *Mail {
return &Mail{
usePlainText: false,
senderAddress: senderAddress,
smtpHostAddr: smtpHostAddress,
receiverAddresses: []string{},
}
}

// BodyType is used to specify the format of the body.
type BodyType int

const (
// PlainText is used to specify that the body is plain text.
PlainText BodyType = iota
// HTML is used to specify that the body is HTML.
HTML
)

// AuthenticateSMTP authenticates you to send emails via smtp.
// Example values: "", "test@gmail.com", "password123", "smtp.gmail.com"
// For more information about smtp authentication, see here:
Expand All @@ -41,18 +53,38 @@ func (m *Mail) AddReceivers(addresses ...string) {
m.receiverAddresses = append(m.receiverAddresses, addresses...)
}

// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
// html as markup language.
func (m Mail) Send(ctx context.Context, subject, message string) error {
// BodyFormat can be used to specify the format of the body.
// Default BodyType is HTML.
func (m *Mail) BodyFormat(format BodyType) {
switch format {
case PlainText:
m.usePlainText = true
default:
m.usePlainText = false
}
}

func (m *Mail) newEmail(subject, message string) *email.Email {
msg := &email.Email{
To: m.receiverAddresses,
From: m.senderAddress,
Subject: subject,
// Text: []byte("Text Body is, of course, supported!"),
HTML: []byte(message),
Headers: textproto.MIMEHeader{},
}

if m.usePlainText {
msg.Text = []byte(message)
} else {
msg.HTML = []byte(message)
}
return msg
}

// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
// html as markup language.
func (m Mail) Send(ctx context.Context, subject, message string) error {
msg := m.newEmail(subject, message)

var err error
select {
case <-ctx.Done():
Expand Down
52 changes: 52 additions & 0 deletions service/mail/mail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mail

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMail_newEmailHtml(t *testing.T) {
t.Parallel()

text := "test"
m := New("foo", "server")
email := m.newEmail("test", text)

assert.False(t, m.usePlainText)
assert.Equal(t, []byte(nil), email.Text)
assert.Equal(t, []byte(text), email.HTML)
}

func TestMail_newEmailText(t *testing.T) {
t.Parallel()

text := "test"
m := New("foo", "server")
m.BodyFormat(PlainText)
email := m.newEmail("test", text)

assert.True(t, m.usePlainText)
assert.Equal(t, []byte(text), email.Text)
assert.Equal(t, []byte(nil), email.HTML)
}

func TestMail_AddReceivers(t *testing.T) {
t.Parallel()

m := New("foo", "server")
m.AddReceivers("test")

assert.Len(t, m.receiverAddresses, 1)
assert.Equal(t, "test", m.receiverAddresses[0])
}

func TestMail_AuthenticateSMTP(t *testing.T) {
t.Parallel()

m := New("foo", "server")
assert.Nil(t, m.smtpAuth)

m.AuthenticateSMTP("test", "test", "test", "test")
assert.NotNil(t, m.smtpAuth)
}

0 comments on commit d3ba200

Please sign in to comment.