Skip to content

Commit e446980

Browse files
author
Irae Hueck Costa
committed
Refactor email sending to use gomail instead of Mailgun
1 parent c574e4c commit e446980

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

backend/endpoints/feedback.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package endpoints
22

33
import (
4-
"context"
54
"fmt"
65
"github.com/ihucos/counter.dev/lib"
7-
"github.com/mailgun/mailgun-go/v4"
8-
"time"
6+
"gopkg.in/gomail.v2"
97
)
108

119
func init() {
@@ -16,21 +14,26 @@ func init() {
1614
ctx.ReturnBadRequest("No input given.")
1715
}
1816
user := ctx.GetUserId()
19-
mg := mailgun.NewMailgun("counter.dev", ctx.App.Config.MailgunSecretApiKey)
17+
m := gomail.NewMessage()
18+
19+
m.SetHeader("From", "hey@counter.dev")
20+
m.SetHeader("To", "hey@counter.dev")
21+
m.SetBody("text/plain", feedback)
22+
2023
var title string
2124
if user == "" {
2225
title = "User feedback received"
2326
} else {
2427
title = fmt.Sprintf("User feedback received from %s", user)
2528
}
26-
if mail != "" {
27-
title += fmt.Sprintf(" (%s)", mail)
28-
}
29-
message := mg.NewMessage("hey@counter.dev", title, feedback, "hey@counter.dev")
30-
c, cancel := context.WithTimeout(context.Background(), time.Second*30)
31-
defer cancel()
32-
_, _, err := mg.Send(c, message)
29+
if mail != "" {
30+
title += fmt.Sprintf(" (%s)", mail)
31+
}
32+
m.SetHeader("Subject", title)
33+
34+
d := gomail.NewDialer("smtp.protonmail.ch", 587, "hey@counter.dev", ctx.App.Config.SMTPSecret)
3335

36+
err := d.DialAndSend(m)
3437
if err != nil {
3538
ctx.CatchError(err)
3639
}

backend/endpoints/register.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func init() {
3535
ctx.CatchError(err)
3636
}
3737

38-
user.SendSurvey(ctx.App.Config.MailgunSecretApiKey)
38+
user.SendSurvey(ctx.App.Config.SMTPSecret)
3939
ctx.SetSessionUser(userId)
4040
ctx.ReturnUser()
4141

backend/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ require (
99
github.com/mailgun/mailgun-go/v4 v4.8.1
1010
github.com/xavivars/uasurfer v0.0.0-20210227184249-298e5855ffd5
1111
golang.org/x/text v0.3.8
12+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
13+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
1214
gorm.io/driver/sqlite v1.3.2
1315
gorm.io/gorm v1.23.5
1416
)

backend/go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
6868
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
6969
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
7070
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
71+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
72+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
7173
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
7274
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
75+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
76+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
7377
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
7478
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7579
gorm.io/driver/sqlite v1.3.2 h1:nWTy4cE52K6nnMhv23wLmur9Y3qWbZvOBz+V4PrGAxg=

backend/lib/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Config struct {
1414
ArchiveDatabase string
1515
ArchiveMaxAge time.Duration
1616
MailgunSecretApiKey string
17+
SMTPSecret string
1718
}
1819

1920
func env(env string) string {
@@ -50,6 +51,6 @@ func NewConfigFromEnv() Config {
5051
ArchiveDatabase: env("WEBSTATS_ARCHIVE_DATABASE"),
5152
ArchiveMaxAge: envDuration("WEBSTATS_ARCHIVE_MAX_AGE"),
5253
MailgunSecretApiKey: envDefault("WEBSTATS_MAILGUN_SECRET_API_KEY", "dummy"),
54+
SMTPSecret: envDefault("WEBSTATS_SMTP_SECREET", "dummy"),
5355
}
54-
5556
}

0 commit comments

Comments
 (0)