diff --git a/service/mail/mail.go b/service/mail/mail.go index e3876d0c..65abf83f 100644 --- a/service/mail/mail.go +++ b/service/mail/mail.go @@ -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 @@ -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: @@ -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(): diff --git a/service/mail/mail_test.go b/service/mail/mail_test.go new file mode 100644 index 00000000..ae2ee167 --- /dev/null +++ b/service/mail/mail_test.go @@ -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) +}