Skip to content

Commit

Permalink
feat(smtp): add simple smtp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Jun 3, 2019
1 parent dfdf576 commit 946b6cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
48 changes: 45 additions & 3 deletions pkg/plugins/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package smtp
import (
"errors"
"fmt"
"log"
"net/smtp"
"regexp"
"strconv"
"strings"
)

// Plugin sends notifications to a given e-mail addresses via SMTP
type Plugin struct {}

// Send a notification message to discord
Expand Down Expand Up @@ -48,12 +52,13 @@ func (plugin *Plugin) CreateConfigFromURL(url string) (Config, error) {
Port: uint16(port),
FromAddress: args[4],
FromName: args[5],
ToAddresses: strings.Split(args[6], "/"),
}, nil
}

// ExtractArguments extracts the arguments from a notification url, i.e everything following the initial ://
func ExtractArguments(url string) ([]string, error) {
regex, err := regexp.Compile("^smtp://([^:]+):([^@]+)@([^:]+):([1-9][0-9]*)$")
regex, err := regexp.Compile("^smtp://([^:]+):([^@]+)@([^:]+):([1-9][0-9]*)/([a-z]+@[a-z|\\-|\\.])\\(([^\\)])\\)/(.*)$")
if err != nil {
return nil, errors.New("could not compile regex")
}
Expand All @@ -66,11 +71,48 @@ func ExtractArguments(url string) ([]string, error) {
return match, nil
}

func doSend(payload string, config Config) error {
func doSend(message string, config Config) error {


fmt.Println(config)

for _, toAddress := range config.ToAddresses {

return errors.New("not implemented")
client, err := smtp.Dial(fmt.Sprintf("%s:%d", config.Host, config.Port)
if err != nil {
log.Fatal(err)
}

// Set the sender and recipient first
if err := client.Mail(config.FromAddress); err != nil {
log.Fatal(err)
}
if err := client.Rcpt(toAddress); err != nil {
log.Fatal(err)
}

// Send the email body.
wc, err := client.Data()
if err != nil {
log.Fatal(err)
}
_, err = fmt.Fprintf(wc, message)
if err != nil {
log.Fatal(err)
}
err = wc.Close()
if err != nil {
log.Fatal(err)
}

// Send the QUIT command and close the connection.
err = client.Quit()
if err != nil {
log.Fatal(err)
}

}


return nil
}
1 change: 1 addition & 0 deletions pkg/plugins/smtp/smtp_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type Config struct {
Port uint16
FromAddress string
FromName string
ToAddresses []string
}
8 changes: 4 additions & 4 deletions pkg/plugins/smtp/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ func TestSMTP(t *testing.T) {

var (
plugin *Plugin
envSmtpURL string
envSMTPURL string
)

var _ = Describe("the SMTP plugin", func() {
BeforeSuite(func() {
plugin = &Plugin{}
envSmtpURL = os.Getenv("SHOUTRRR_SMTP_URL")
envSMTPURL = os.Getenv("SHOUTRRR_SMTP_URL")
})
When("running integration tests", func() {
It("should work without errors", func() {
if envSmtpURL == "" {
if envSMTPURL == "" {
return
}
err := plugin.Send(envSmtpURL, "this is an integration test")
err := plugin.Send(envSMTPURL, "this is an integration test")
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down

0 comments on commit 946b6cd

Please sign in to comment.