Skip to content

Commit 2298ac3

Browse files
committed
Set ReplyTo for SMTP based notifications
Ensure all replies go back to person who initiated the notification.
1 parent 3b0291d commit 2298ac3

File tree

9 files changed

+720
-697
lines changed

9 files changed

+720
-697
lines changed

domain/mail/document.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ import (
2020
)
2121

2222
// DocumentApprover notifies user who has just been granted document approval rights.
23-
func (m *Mailer) DocumentApprover(recipient, inviter, url, document string) {
23+
func (m *Mailer) DocumentApprover(recipient, inviterName, inviterEmail, url, document string) {
2424
method := "DocumentApprover"
2525
m.Initialize()
2626

2727
// check inviter name
28-
if inviter == "Hello You" || len(inviter) == 0 {
29-
inviter = "Your colleague"
28+
if inviterName == "Hello You" || len(inviterName) == 0 {
29+
inviterName = "Your colleague"
3030
}
3131

3232
em := smtp.EmailMessage{}
33-
em.Subject = fmt.Sprintf("%s has granted you document approval", inviter)
33+
em.Subject = fmt.Sprintf("%s has granted you document approval", inviterName)
3434
em.ToEmail = recipient
3535
em.ToName = recipient
36+
em.ReplyTo = inviterEmail
37+
em.ReplyName = inviterName
3638

3739
parameters := struct {
3840
Subject string
@@ -41,7 +43,7 @@ func (m *Mailer) DocumentApprover(recipient, inviter, url, document string) {
4143
Document string
4244
}{
4345
em.Subject,
44-
inviter,
46+
inviterName,
4547
url,
4648
document,
4749
}

domain/mail/space.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ import (
1818
)
1919

2020
// ShareSpaceExistingUser provides an existing user with a link to a newly shared space.
21-
func (m *Mailer) ShareSpaceExistingUser(recipient, inviter, url, folder, intro string) {
21+
func (m *Mailer) ShareSpaceExistingUser(recipient, inviterName, inviterEmail, url, folder, intro string) {
2222
method := "ShareSpaceExistingUser"
2323
m.Initialize()
2424

2525
// check inviter name
26-
if inviter == "Hello You" || len(inviter) == 0 {
27-
inviter = "Your colleague"
26+
if inviterName == "Hello You" || len(inviterName) == 0 {
27+
inviterName = "Your colleague"
2828
}
2929

3030
em := smtp.EmailMessage{}
31-
em.Subject = fmt.Sprintf("%s has shared %s with you", inviter, folder)
31+
em.Subject = fmt.Sprintf("%s has shared %s with you", inviterName, folder)
3232
em.ToEmail = recipient
3333
em.ToName = recipient
34+
em.ReplyTo = inviterEmail
35+
em.ReplyName = inviterName
3436

3537
parameters := struct {
3638
Subject string
@@ -40,7 +42,7 @@ func (m *Mailer) ShareSpaceExistingUser(recipient, inviter, url, folder, intro s
4042
Intro string
4143
}{
4244
em.Subject,
43-
inviter,
45+
inviterName,
4446
url,
4547
folder,
4648
intro,
@@ -63,19 +65,21 @@ func (m *Mailer) ShareSpaceExistingUser(recipient, inviter, url, folder, intro s
6365
}
6466

6567
// ShareSpaceNewUser invites new user providing Credentials, explaining the product and stating who is inviting them.
66-
func (m *Mailer) ShareSpaceNewUser(recipient, inviter, url, space, invitationMessage string) {
68+
func (m *Mailer) ShareSpaceNewUser(recipient, inviterName, inviterEmail, url, space, invitationMessage string) {
6769
method := "ShareSpaceNewUser"
6870
m.Initialize()
6971

7072
// check inviter name
71-
if inviter == "Hello You" || len(inviter) == 0 {
72-
inviter = "Your colleague"
73+
if inviterName == "Hello You" || len(inviterName) == 0 {
74+
inviterName = "Your colleague"
7375
}
7476

7577
em := smtp.EmailMessage{}
76-
em.Subject = fmt.Sprintf("%s has shared %s with you on Documize", inviter, space)
78+
em.Subject = fmt.Sprintf("%s has shared %s with you on Documize", inviterName, space)
7779
em.ToEmail = recipient
7880
em.ToName = recipient
81+
em.ReplyTo = inviterEmail
82+
em.ReplyName = inviterName
7983

8084
parameters := struct {
8185
Subject string
@@ -85,7 +89,7 @@ func (m *Mailer) ShareSpaceNewUser(recipient, inviter, url, space, invitationMes
8589
Folder string
8690
}{
8791
em.Subject,
88-
inviter,
92+
inviterName,
8993
url,
9094
invitationMessage,
9195
space,

domain/mail/user.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ import (
1818
)
1919

2020
// InviteNewUser invites someone new providing credentials, explaining the product and stating who is inviting them.
21-
func (m *Mailer) InviteNewUser(recipient, inviter, url, username, password string) {
21+
func (m *Mailer) InviteNewUser(recipient, inviterName, inviterEmail, url, username, password string) {
2222
method := "InviteNewUser"
2323
m.Initialize()
2424

2525
// check inviter name
26-
if inviter == "Hello You" || len(inviter) == 0 {
27-
inviter = "Your colleague"
26+
if inviterName == "Hello You" || len(inviterName) == 0 {
27+
inviterName = "Your colleague"
2828
}
2929

3030
em := smtp.EmailMessage{}
31-
em.Subject = fmt.Sprintf("%s has invited you to Documize", inviter)
31+
em.Subject = fmt.Sprintf("%s has invited you to Documize", inviterName)
3232
em.ToEmail = recipient
3333
em.ToName = recipient
34+
em.ReplyTo = inviterEmail
35+
em.ReplyName = inviterName
3436

3537
parameters := struct {
3638
Subject string
@@ -40,7 +42,7 @@ func (m *Mailer) InviteNewUser(recipient, inviter, url, username, password strin
4042
Password string
4143
}{
4244
em.Subject,
43-
inviter,
45+
inviterName,
4446
url,
4547
recipient,
4648
password,
@@ -63,27 +65,29 @@ func (m *Mailer) InviteNewUser(recipient, inviter, url, username, password strin
6365
}
6466

6567
// InviteExistingUser invites a known user to an organization.
66-
func (m *Mailer) InviteExistingUser(recipient, inviter, url string) {
68+
func (m *Mailer) InviteExistingUser(recipient, inviterName, inviterEmail, url string) {
6769
method := "InviteExistingUser"
6870
m.Initialize()
6971

7072
// check inviter name
71-
if inviter == "Hello You" || len(inviter) == 0 {
72-
inviter = "Your colleague"
73+
if inviterName == "Hello You" || len(inviterName) == 0 {
74+
inviterName = "Your colleague"
7375
}
7476

7577
em := smtp.EmailMessage{}
76-
em.Subject = fmt.Sprintf("%s has invited you to their Documize account", inviter)
78+
em.Subject = fmt.Sprintf("%s has invited you to their Documize account", inviterName)
7779
em.ToEmail = recipient
7880
em.ToName = recipient
81+
em.ReplyTo = inviterEmail
82+
em.ReplyName = inviterName
7983

8084
parameters := struct {
8185
Subject string
8286
Inviter string
8387
URL string
8488
}{
8589
em.Subject,
86-
inviter,
90+
inviterName,
8791
url,
8892
}
8993

domain/permission/endpoint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (h *Handler) SetSpacePermissions(w http.ResponseWriter, r *http.Request) {
200200
}
201201

202202
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
203-
go mailer.ShareSpaceExistingUser(existingUser.Email, inviter.Fullname(), url, sp.Name, model.Message)
203+
go mailer.ShareSpaceExistingUser(existingUser.Email, inviter.Fullname(), inviter.Email, url, sp.Name, model.Message)
204204
h.Runtime.Log.Info(fmt.Sprintf("%s is sharing space %s with existing user %s", inviter.Email, sp.Name, existingUser.Email))
205205
}
206206
}
@@ -701,7 +701,7 @@ func (h *Handler) SetDocumentPermissions(w http.ResponseWriter, r *http.Request)
701701
}
702702

703703
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
704-
go mailer.DocumentApprover(existingUser.Email, inviter.Fullname(), url, doc.Title)
704+
go mailer.DocumentApprover(existingUser.Email, inviter.Fullname(), inviter.Email, url, doc.Title)
705705
h.Runtime.Log.Info(fmt.Sprintf("%s has made %s document approver for: %s", inviter.Email, existingUser.Email, doc.Title))
706706
}
707707
}

domain/smtp/send.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ func Connect(c Config) (d *mail.Dialer, err error) {
8888

8989
// EmailMessage represents email to be sent.
9090
type EmailMessage struct {
91-
ToEmail string
92-
ToName string
93-
Subject string
94-
BodyHTML string
91+
ToEmail string
92+
ToName string
93+
Subject string
94+
BodyHTML string
95+
ReplyTo string
96+
ReplyName string
9597
}
9698

9799
// SendMessage sends email using specified SMTP connection
@@ -102,6 +104,17 @@ func SendMessage(d *mail.Dialer, c Config, em EmailMessage) (b bool, err error)
102104
m.SetHeader("From", m.FormatAddress(c.SenderEmail, c.SenderName))
103105
m.SetHeader("To", m.FormatAddress(em.ToEmail, em.ToName))
104106

107+
// Where do replies go?
108+
reply := c.SenderEmail
109+
replyName := c.SenderName
110+
if len(em.ReplyTo) > 0 {
111+
reply = em.ReplyTo
112+
}
113+
if len(em.ReplyName) > 0 {
114+
replyName = em.ReplyName
115+
}
116+
m.SetAddressHeader("Reply-To", reply, replyName)
117+
105118
// content
106119
m.SetHeader("Subject", em.Subject)
107120
m.SetBody("text/html", em.BodyHTML)

domain/space/endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ func (h *Handler) Invite(w http.ResponseWriter, r *http.Request) {
868868

869869
url := ctx.GetAppURL(fmt.Sprintf("s/%s/%s", sp.RefID, stringutil.MakeSlug(sp.Name)))
870870
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
871-
go mailer.ShareSpaceExistingUser(email, inviter.Fullname(), url, sp.Name, model.Message)
871+
go mailer.ShareSpaceExistingUser(email, inviter.Fullname(), inviter.Email, url, sp.Name, model.Message)
872872

873873
h.Runtime.Log.Info(fmt.Sprintf("%s is sharing space %s with existing user %s", inviter.Email, sp.Name, email))
874874
} else {

domain/space/space.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func inviteNewUserToSharedSpace(ctx domain.RequestContext, rt *env.Runtime, s *d
7979
mailer := mail.Mailer{Runtime: rt, Store: s, Context: ctx}
8080

8181
url := fmt.Sprintf("%s/%s", baseURL, u.Salt)
82-
go mailer.ShareSpaceNewUser(u.Email, invitedBy.Fullname(), url, sp.Name, invitationMessage)
82+
go mailer.ShareSpaceNewUser(u.Email, invitedBy.Fullname(), invitedBy.Email, url, sp.Name, invitationMessage)
8383

8484
return
8585
}

domain/user/endpoint.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
218218

219219
url := fmt.Sprintf("%s/%s", ctx.GetAppURL("auth/sso"), url.QueryEscape(string(encrypted)))
220220
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
221-
go mailer.InviteNewUser(userModel.Email, inviter.Fullname(), url, userModel.Email, requestedPassword)
221+
go mailer.InviteNewUser(userModel.Email, inviter.Fullname(), inviter.Email, url, userModel.Email, requestedPassword)
222222

223223
h.Runtime.Log.Info(fmt.Sprintf("%s invited by %s on %s", userModel.Email, inviter.Email, ctx.AppURL))
224224
} else {
225225
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
226-
go mailer.InviteExistingUser(userModel.Email, inviter.Fullname(), ctx.GetAppURL(""))
226+
go mailer.InviteExistingUser(userModel.Email, inviter.Fullname(), inviter.Email, ctx.GetAppURL(""))
227227

228228
h.Runtime.Log.Info(fmt.Sprintf("%s is giving access to an existing user %s", inviter.Email, userModel.Email))
229229
}
@@ -847,12 +847,12 @@ func (h *Handler) BulkImport(w http.ResponseWriter, r *http.Request) {
847847

848848
url := fmt.Sprintf("%s/%s", ctx.GetAppURL("auth/sso"), url.QueryEscape(string(encrypted)))
849849
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
850-
go mailer.InviteNewUser(userModel.Email, inviter.Fullname(), url, userModel.Email, requestedPassword)
850+
go mailer.InviteNewUser(userModel.Email, inviter.Fullname(), inviter.Email, url, userModel.Email, requestedPassword)
851851

852852
h.Runtime.Log.Info(fmt.Sprintf("%s invited by %s on %s", userModel.Email, inviter.Email, ctx.AppURL))
853853
} else {
854854
mailer := mail.Mailer{Runtime: h.Runtime, Store: h.Store, Context: ctx}
855-
go mailer.InviteExistingUser(userModel.Email, inviter.Fullname(), ctx.GetAppURL(""))
855+
go mailer.InviteExistingUser(userModel.Email, inviter.Fullname(), inviter.Email, ctx.GetAppURL(""))
856856

857857
h.Runtime.Log.Info(fmt.Sprintf("%s is giving access to an existing user %s", inviter.Email, userModel.Email))
858858
}

0 commit comments

Comments
 (0)