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)) + } +}