Skip to content

Commit

Permalink
mail service
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjirku committed Feb 23, 2024
1 parent 8589192 commit a2283c2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
31 changes: 31 additions & 0 deletions services/mail.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions services/mail_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit a2283c2

Please sign in to comment.