mirror of
https://github.com/nikoksr/notify.git
synced 2025-02-11 13:15:34 +02:00
Add plaintext toggle (#476)
Co-authored-by: Niko Köser <koeserniko@gmail.com>
This commit is contained in:
parent
1e6d668179
commit
d3ba200695
@ -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():
|
||||
|
52
service/mail/mail_test.go
Normal file
52
service/mail/mail_test.go
Normal file
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user