Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscription link support to notification email verification template #573

Merged
merged 3 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ _this is the recommended way to run remark42_
| notify.telegram.timeout | NOTIFY_TELEGRAM_TIMEOUT | `5s` | telegram timeout |
| notify.email.fromAddress | NOTIFY_EMAIL_FROM | | from email address |
| notify.email.verification_subj | NOTIFY_EMAIL_VERIFICATION_SUBJ | `Email verification` | verification message subject |
| notify.email.subscribe-url | NOTIFY_EMAIL_SUBSCRIBE_URL | `` | URL parameters to add to SITE in order to include to subscription email instead of plain token |
| smtp.host | SMTP_HOST | | SMTP host |
| smtp.port | SMTP_PORT | | SMTP port |
| smtp.username | SMTP_USERNAME | | SMTP user name |
Expand Down
4 changes: 4 additions & 0 deletions backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type NotifyGroup struct {
Email struct {
From string `long:"fromAddress" env:"FROM" description:"from email address"`
VerificationSubject string `long:"verification_subj" env:"VERIFICATION_SUBJ" description:"verification message subject"`
SubscribeURL string `long:"subscribe-url" env:"SUBSCRIBE_URL" description:"URL parameters to add to SITE in order to include to subscription email instead of plain token"`
} `group:"email" namespace:"email" env-namespace:"EMAIL"`
}

Expand Down Expand Up @@ -783,6 +784,9 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore, authenticator *
return tkn, nil
},
}
if s.Notify.Email.SubscribeURL != "" {
emailParams.SubscribeURL = s.RemarkURL + s.Notify.Email.SubscribeURL
}
smtpParams := notify.SmtpParams{
Host: s.SMTP.Host,
Port: s.SMTP.Port,
Expand Down
23 changes: 15 additions & 8 deletions backend/app/notify/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type EmailParams struct {
MsgTemplate string // request message template
VerificationSubject string // verification message subject
VerificationTemplate string // verification message template
SubscribeURL string // full subscribe handler URL
UnsubscribeURL string // full unsubscribe handler URL

TokenGenFn func(userID, email, site string) (string, error) // Unsubscribe token generation function
Expand Down Expand Up @@ -91,10 +92,11 @@ type msgTmplData struct {

// verifyTmplData store data for verification message template execution
type verifyTmplData struct {
User string
Token string
Email string
Site string
User string
Token string
Email string
Site string
SubscribeURL string
}

const (
Expand Down Expand Up @@ -178,6 +180,10 @@ const (
<div style="text-align: center; font-family: Helvetica, Arial, sans-serif; font-size: 18px;">
<h1 style="position: relative; color: #4fbbd6; margin-top: 0.2em;">Remark42</h1>
<p style="position: relative; max-width: 20em; margin: 0 auto 1em auto; line-height: 1.4em; color:#000!important;">Confirmation for <b>{{.User}}</b> on site <b>{{.Site}}</b></p>
{{if .SubscribeURL}}
<p style="position: relative; margin: 0 0 0.5em 0;color:#000!important;"><a href="{{.SubscribeURL}}{{.Token}}">Click here to subscribe to email notifications</a></p>
<p style="position: relative; margin: 0 0 0.5em 0;color:#000!important;">Alternatively, you can use code below for subscription.</p>
{{ end }}
<div style="background-color: #eee; max-width: 20em; margin: 0 auto; border-radius: 0.4em; padding: 0.5em;">
<p style="position: relative; margin: 0 0 0.5em 0;color:#000!important;">TOKEN</p>
<p style="position: relative; font-size: 0.7em; opacity: 0.8;"><i style="color:#000!important;">Copy and paste this text into “token” field on comments page</i></p>
Expand Down Expand Up @@ -273,10 +279,11 @@ func (e *Email) buildVerificationMessage(user, email, token, site string) (strin
subject := e.VerificationSubject
msg := bytes.Buffer{}
err := e.verifyTmpl.Execute(&msg, verifyTmplData{
User: user,
Token: token,
Email: email,
Site: site,
User: user,
Token: token,
Email: email,
Site: site,
SubscribeURL: e.SubscribeURL,
})
if err != nil {
return "", errors.Wrapf(err, "error executing template to build verification message")
Expand Down
47 changes: 46 additions & 1 deletion backend/app/notify/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ func TestEmail_Send(t *testing.T) {
req := Request{
Comment: store.Comment{ID: "999", User: store.User{ID: "1", Name: "test_user"}, PostTitle: "test_title"},
parent: store.Comment{ID: "1", User: store.User{ID: "999", Name: "parent_user"}},
Email: "[email protected]"}
Email: "[email protected]",
}
assert.NoError(t, email.Send(context.TODO(), req))
assert.Equal(t, "[email protected]", fakeSmtp.readMail())
assert.Equal(t, 1, fakeSmtp.readQuitCount())
Expand All @@ -208,6 +209,50 @@ List-Unsubscribe: <https://remark42.com/api/v1/email/unsubscribe?site=&tkn=token
Date: `)
}

func TestEmail_SendVerification(t *testing.T) {
email, err := NewEmail(EmailParams{From: "[email protected]"}, SmtpParams{})
assert.NoError(t, err)
assert.NotNil(t, email)
fakeSmtp := fakeTestSMTP{}
email.smtp = &fakeSmtp
email.TokenGenFn = TokenGenFn
req := Request{
Email: "[email protected]",
Verification: VerificationMetadata{
SiteID: "remark",
User: "test_username",
Token: "secret_",
},
}
assert.NoError(t, email.Send(context.TODO(), req))
assert.Equal(t, "[email protected]", fakeSmtp.readMail())
assert.Equal(t, 1, fakeSmtp.readQuitCount())
assert.Equal(t, "[email protected]", fakeSmtp.readRcpt())
// test buildMessageFromRequest separately for message text
res, err := email.buildVerificationMessage(req.Verification.User, req.Email, req.Verification.Token, req.Verification.SiteID)
assert.NoError(t, err)
assert.Contains(t, res, `From: [email protected]
To: [email protected]
Subject: Email verification
Content-Transfer-Encoding: quoted-printable
MIME-version: 1.0
Content-Type: text/html; charset="UTF-8"
Date: `)
assert.Contains(t, res, `secret_`)
assert.NotContains(t, res, `https://example.org/`)
email.SubscribeURL = "https://example.org/subscribe.html?token="
res, err = email.buildVerificationMessage(req.Verification.User, req.Email, req.Verification.Token, req.Verification.SiteID)
assert.NoError(t, err)
assert.Contains(t, res, `From: [email protected]
To: [email protected]
Subject: Email verification
Content-Transfer-Encoding: quoted-printable
MIME-version: 1.0
Content-Type: text/html; charset="UTF-8"
Date: `)
assert.Contains(t, res, `https://example.org/subscribe.html?token=3Dsecret_`)
}

func Test_emailClient_Create(t *testing.T) {
creator := emailClient{}
client, err := creator.Create(SmtpParams{})
Expand Down