From a2283c20353ae40a9f5cca5aca803a0b8ef1c476 Mon Sep 17 00:00:00 2001 From: Martin Jirku Date: Fri, 23 Feb 2024 22:16:01 +0100 Subject: [PATCH] mail service --- services/mail.go | 31 +++++++++++++++++++++++++++++++ services/mail_test.go | 16 ++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 services/mail.go create mode 100644 services/mail_test.go diff --git a/services/mail.go b/services/mail.go new file mode 100644 index 0000000..d985bf1 --- /dev/null +++ b/services/mail.go @@ -0,0 +1,31 @@ +package services + +import "net/smtp" + +type MailService struct { + password string + account string +} + +func NewMailService(account, password string) MailService { + return MailService{ + password: password, + account: account, + } +} + +func (s *MailService) Send(to, body string) error { + from := s.account + pass := s.password + msg := "From: " + from + "\n" + + "To: " + to + "\n" + + "Subject: Hello there\n\n" + + body + err := smtp.SendMail("smtp.gmail.com:587", + smtp.PlainAuth("", from, pass, "smtp.gmail.com"), + from, []string{to}, []byte(msg)) + if err != nil { + return err + } + return nil +} diff --git a/services/mail_test.go b/services/mail_test.go new file mode 100644 index 0000000..1fa12c2 --- /dev/null +++ b/services/mail_test.go @@ -0,0 +1,16 @@ +package services_test + +import ( + "fmt" + "testing" + + "jirku.sk/mcmamina/services" +) + +func TestMailSend(t *testing.T) { + service := services.NewMailService("email@emai.sk", "pwd") + err := service.Send("email@email.sk", "ahoj svet") + if err != nil { + t.Error(fmt.Errorf("sending mail failed: %w", err)) + } +}