From 946b6cd740ce9300e975c18394ceda7cbc67b3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20ma=CC=8Ase=CC=81n?= Date: Mon, 3 Jun 2019 21:36:20 +0200 Subject: [PATCH] feat(smtp): add simple smtp implementation --- pkg/plugins/smtp/smtp.go | 48 ++++++++++++++++++++++++++++++--- pkg/plugins/smtp/smtp_config.go | 1 + pkg/plugins/smtp/smtp_test.go | 8 +++--- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/pkg/plugins/smtp/smtp.go b/pkg/plugins/smtp/smtp.go index 23c9dfc9..71ed62bc 100644 --- a/pkg/plugins/smtp/smtp.go +++ b/pkg/plugins/smtp/smtp.go @@ -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 @@ -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") } @@ -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 } \ No newline at end of file diff --git a/pkg/plugins/smtp/smtp_config.go b/pkg/plugins/smtp/smtp_config.go index a8678ae8..d10f8de6 100644 --- a/pkg/plugins/smtp/smtp_config.go +++ b/pkg/plugins/smtp/smtp_config.go @@ -8,4 +8,5 @@ type Config struct { Port uint16 FromAddress string FromName string + ToAddresses []string } diff --git a/pkg/plugins/smtp/smtp_test.go b/pkg/plugins/smtp/smtp_test.go index 6bf2bed5..e737f35f 100644 --- a/pkg/plugins/smtp/smtp_test.go +++ b/pkg/plugins/smtp/smtp_test.go @@ -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()) }) })