Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 601c843

Browse files
committed
Impl email.Sender
1 parent fee42d3 commit 601c843

File tree

6 files changed

+77
-17
lines changed

6 files changed

+77
-17
lines changed

.env.ci

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ DEBUG_LOG=true
3030
JOB_INTERVAL_SEC=120
3131

3232
HYDRA_ADMIN_URL=http://hydra:4445
33+
34+
CLIENT_REGISTRATION_URL=http://localhost:8080/registration
35+
36+
SMTP_ADDR=smtp:1025
37+

.env.sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ DEBUG_LOG=true
3131
JOB_INTERVAL_SEC=120
3232

3333
HYDRA_ADMIN_URL=http://hydra:4445
34+
35+
CLIENT_REGISTRATION_URL=http://localhost:8080/registration
36+
37+
SMTP_ADDR=smtp:1025
38+

app/config/config.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import (
88

99
// Config represents app config
1010
type Config struct {
11-
DataBaseURL string `envconfig:"database_url" required:"true"`
12-
TestDataBaseURL string `envconfig:"test_database_url" required:"true"`
13-
RedisAddr string `envconfig:"redis_addr" required:"true"`
14-
DebugLog bool `envconfig:"debug_log"`
15-
JobIntervalSec int `envconfig:"job_interval_sec" required:"true"`
16-
HydraAdminURL string `envconfig:"hydra_admin_url" required:"true"`
17-
MinioPublicURL string `envconfig:"minio_public_url" required:"true"`
18-
MinioEndpoint string `envconfig:"minio_endpoint" required:"true"`
19-
MinioAccessKey string `envconfig:"minio_access_key" required:"true"`
20-
MinioSecretKey string `envconfig:"minio_secret_key" required:"true"`
21-
MinioBucketName string `envconfig:"minio_bucket_name" required:"true"`
11+
DataBaseURL string `envconfig:"database_url" required:"true"`
12+
TestDataBaseURL string `envconfig:"test_database_url" required:"true"`
13+
RedisAddr string `envconfig:"redis_addr" required:"true"`
14+
DebugLog bool `envconfig:"debug_log"`
15+
JobIntervalSec int `envconfig:"job_interval_sec" required:"true"`
16+
HydraAdminURL string `envconfig:"hydra_admin_url" required:"true"`
17+
MinioPublicURL string `envconfig:"minio_public_url" required:"true"`
18+
MinioEndpoint string `envconfig:"minio_endpoint" required:"true"`
19+
MinioAccessKey string `envconfig:"minio_access_key" required:"true"`
20+
MinioSecretKey string `envconfig:"minio_secret_key" required:"true"`
21+
MinioBucketName string `envconfig:"minio_bucket_name" required:"true"`
22+
ClientRegistrationURL string `envconfig:"client_registration_url" required:"true"`
23+
SMTPAddr string `envconfig:"smtp_addr" required:"true"`
24+
EmailFrom string `envconfig:"email_from" required:"true"`
2225
}
2326

2427
// LoadConfig loads config

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
- pg
1313
- redis
1414
- hydra
15+
- mailhog
1516
ports:
1617
- "127.0.0.1:3000:3000"
1718

@@ -111,6 +112,14 @@ services:
111112
ports:
112113
- "127.0.0.1:9100:9000"
113114

115+
mailhog:
116+
image: mailhog/mailhog
117+
hostname: smtp
118+
restart: always
119+
ports:
120+
- "127.0.0.1:1025:1025"
121+
- "127.0.0.1:8025:8025"
122+
114123
volumes:
115124
pg-data:
116125
driver: local

infra/email/email.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package email
22

33
import (
4+
"bytes"
45
"context"
56

6-
"github.com/ProgrammingLab/prolab-accounts/static"
7+
"github.com/jordan-wright/email"
8+
"github.com/pkg/errors"
79

810
"github.com/ProgrammingLab/prolab-accounts/app/config"
911
"github.com/ProgrammingLab/prolab-accounts/infra/record"
12+
"github.com/ProgrammingLab/prolab-accounts/static"
1013
)
1114

1215
// Sender represents interface of email sender
1316
type Sender interface {
14-
SendInvitationEmail(email string, req *record.Invitation) error
17+
SendInvitationEmail(req *record.Invitation) error
1518
}
1619

1720
// NewSender creates new sender
@@ -29,7 +32,38 @@ type senderImpl struct {
2932
asset *static.EmailAsset
3033
}
3134

35+
type invitationEmailData struct {
36+
RegistrationURL string
37+
}
38+
39+
const (
40+
subjectPrefix = "[プロラボアカウント]"
41+
)
42+
3243
// SendInvitationEmail sends invitation email
33-
func (s *senderImpl) SendInvitationEmail(email string, inv *record.Invitation) error {
44+
func (s *senderImpl) SendInvitationEmail(inv *record.Invitation) error {
45+
tmpl, err := s.asset.GetTemplate("invitation.tmpl")
46+
if err != nil {
47+
return err
48+
}
49+
50+
d := invitationEmailData{
51+
RegistrationURL: s.cfg.ClientRegistrationURL + "/" + inv.Code,
52+
}
53+
buf := &bytes.Buffer{}
54+
err = tmpl.Execute(buf, d)
55+
if err != nil {
56+
return errors.WithStack(err)
57+
}
58+
59+
e := email.NewEmail()
60+
e.From = s.cfg.EmailFrom
61+
e.To = []string{inv.Email}
62+
e.Subject = subjectPrefix + "ユーザー登録"
63+
e.Text = buf.Bytes()
64+
err = e.Send(s.cfg.SMTPAddr, nil)
65+
if err != nil {
66+
return errors.WithStack(err)
67+
}
3468
return nil
3569
}

static/asset.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package static
22

33
import (
4+
"fmt"
45
"text/template"
56

67
"github.com/gobuffalo/packr/v2"
@@ -15,9 +16,12 @@ type EmailAsset struct {
1516
}
1617

1718
// GetTemplate returns email template
18-
func (a *EmailAsset) GetTemplate(name string) (tmpl *template.Template, ok bool) {
19-
tmpl, ok = a.templates[name]
20-
return
19+
func (a *EmailAsset) GetTemplate(name string) (*template.Template, error) {
20+
tmpl, ok := a.templates[name]
21+
if !ok {
22+
return nil, errors.WithStack(fmt.Errorf("template %v not found", name))
23+
}
24+
return tmpl, nil
2125
}
2226

2327
func (a *EmailAsset) addTemplate(name string, tmpl *template.Template) {

0 commit comments

Comments
 (0)