Skip to content

Commit 38347aa

Browse files
authored
Add settings to allow different SMTP envelope from address (#17479)
* Add settings to allow different SMTP envelope from address Sometimes it may be advisable to hide or alias the from address on an SMTP mail envelope. This PR adds two new options to the mailer to allow setting of an overriding from address. Fix #17477 Signed-off-by: Andrew Thornton <[email protected]>
1 parent d4e281b commit 38347aa

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

custom/conf/app.example.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,9 @@ PATH =
14571457
;; Mail from address, RFC 5322. This can be just an email address, or the `"Name" <[email protected]>` format
14581458
;FROM =
14591459
;;
1460+
;; Sometimes it is helpful to use a different address on the envelope. Set this to use ENVELOPE_FROM as the from on the envelope. Set to `<>` to send an empty address.
1461+
;ENVELOPE_FROM =
1462+
;;
14601463
;; Mailer user name and password
14611464
;; Please Note: Authentication is only supported when the SMTP server communication is encrypted with TLS (this can be via STARTTLS) or `HOST=localhost`.
14621465
;USER =

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
606606
- Otherwise if `IS_TLS_ENABLED=false` and the server supports `STARTTLS` this will be used. Thus if `STARTTLS` is preferred you should set `IS_TLS_ENABLED=false`.
607607
- `FROM`: **\<empty\>**: Mail from address, RFC 5322. This can be just an email address, or
608608
the "Name" \<[email protected]\> format.
609+
- `ENVELOPE_FROM`: **\<empty\>**: Address set as the From address on the SMTP mail envelope. Set to `<>` to send an empty address.
609610
- `USER`: **\<empty\>**: Username of mailing user (usually the sender's e-mail address).
610611
- `PASSWD`: **\<empty\>**: Password of mailing user. Use \`your password\` for quoting if you use special characters in the password.
611612
- Please note: authentication is only supported when the SMTP server communication is encrypted with TLS (this can be via `STARTTLS`) or `HOST=localhost`. See [Email Setup]({{< relref "doc/usage/email-setup.en-us.md" >}}) for more information.

modules/setting/mailer.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import (
1616
// Mailer represents mail service.
1717
type Mailer struct {
1818
// Mailer
19-
Name string
20-
From string
21-
FromName string
22-
FromEmail string
23-
SendAsPlainText bool
24-
MailerType string
25-
SubjectPrefix string
19+
Name string
20+
From string
21+
EnvelopeFrom string
22+
OverrideEnvelopeFrom bool `ini:"-"`
23+
FromName string
24+
FromEmail string
25+
SendAsPlainText bool
26+
MailerType string
27+
SubjectPrefix string
2628

2729
// SMTP sender
2830
Host string
@@ -73,6 +75,7 @@ func newMailService() {
7375
SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute),
7476
}
7577
MailService.From = sec.Key("FROM").MustString(MailService.User)
78+
MailService.EnvelopeFrom = sec.Key("ENVELOPE_FROM").MustString("")
7679

7780
if sec.HasKey("ENABLE_HTML_ALTERNATIVE") {
7881
log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT")
@@ -93,6 +96,21 @@ func newMailService() {
9396
MailService.FromName = parsed.Name
9497
MailService.FromEmail = parsed.Address
9598

99+
switch MailService.EnvelopeFrom {
100+
case "":
101+
MailService.OverrideEnvelopeFrom = false
102+
case "<>":
103+
MailService.EnvelopeFrom = ""
104+
MailService.OverrideEnvelopeFrom = true
105+
default:
106+
parsed, err = mail.ParseAddress(MailService.EnvelopeFrom)
107+
if err != nil {
108+
log.Fatal("Invalid mailer.ENVELOPE_FROM (%s): %v", MailService.EnvelopeFrom, err)
109+
}
110+
MailService.OverrideEnvelopeFrom = true
111+
MailService.EnvelopeFrom = parsed.Address
112+
}
113+
96114
if MailService.MailerType == "" {
97115
MailService.MailerType = "smtp"
98116
}

services/mailer/mailer.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,14 @@ func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
210210
}
211211
}
212212

213-
if err = client.Mail(from); err != nil {
214-
return fmt.Errorf("Mail: %v", err)
213+
if opts.OverrideEnvelopeFrom {
214+
if err = client.Mail(opts.EnvelopeFrom); err != nil {
215+
return fmt.Errorf("Mail: %v", err)
216+
}
217+
} else {
218+
if err = client.Mail(from); err != nil {
219+
return fmt.Errorf("Mail: %v", err)
220+
}
215221
}
216222

217223
for _, rec := range to {
@@ -242,7 +248,12 @@ func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
242248
var closeError error
243249
var waitError error
244250

245-
args := []string{"-f", from, "-i"}
251+
envelopeFrom := from
252+
if setting.MailService.OverrideEnvelopeFrom {
253+
envelopeFrom = setting.MailService.EnvelopeFrom
254+
}
255+
256+
args := []string{"-f", envelopeFrom, "-i"}
246257
args = append(args, setting.MailService.SendmailArgs...)
247258
args = append(args, to...)
248259
log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args)

0 commit comments

Comments
 (0)