-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8589192
commit a2283c2
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |