-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Niko Köser <[email protected]>
- Loading branch information
Showing
2 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: "", "[email protected]", "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(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |