Skip to content

Commit 641aaa1

Browse files
committed
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 go-gitea#17477 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 157de0f commit 641aaa1

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

custom/conf/app.example.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,10 @@ PATH =
14501450
;; Mail from address, RFC 5322. This can be just an email address, or the `"Name" <[email protected]>` format
14511451
;FROM =
14521452
;;
1453+
;; Sometimes it is helpful to use a different address on the envelope. Set USE_DIFFERENT_ENVELOPE_FROM to true to use ENVELOPE_FROM as the from on the envelope
1454+
;USE_DIFFERENT_ENVELOPE_FROM=false
1455+
;ENVELOPE_FROM =
1456+
;;
14531457
;; Mailer user name and password
14541458
;; Please Note: Authentication is only supported when the SMTP server communication is encrypted with TLS (this can be via STARTTLS) or `HOST=localhost`.
14551459
;USER =

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
598598
- 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`.
599599
- `FROM`: **\<empty\>**: Mail from address, RFC 5322. This can be just an email address, or
600600
the "Name" \<[email protected]\> format.
601+
- `USE_DIFFERENT_ENVELOPE_FROM`: **false**: Set to **true** to use the value of `ENVELOPE_FROM` as the From address on the SMTP mail envelope.
602+
- `ENVELOPE_FROM`: **\<empty\>**: Address set as the From address on the SMTP mail envelope if `USE_DIFFERENT_ENVELOPE_FROM` is set to true.
601603
- `USER`: **\<empty\>**: Username of mailing user (usually the sender's e-mail address).
602604
- `PASSWD`: **\<empty\>**: Password of mailing user. Use \`your password\` for quoting if you use special characters in the password.
603605
- 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

Lines changed: 11 additions & 7 deletions
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+
UseDifferentEnvelopeFrom bool
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,8 @@ func newMailService() {
7375
SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute),
7476
}
7577
MailService.From = sec.Key("FROM").MustString(MailService.User)
78+
MailService.UseDifferentEnvelopeFrom = sec.Key("USE_DIFFERENT_ENVELOPE_FROM").MustBool()
79+
MailService.EnvelopeFrom = sec.Key("ENVELOPE_FROM").MustString("")
7680

7781
if sec.HasKey("ENABLE_HTML_ALTERNATIVE") {
7882
log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT")

services/mailer/mailer.go

Lines changed: 14 additions & 3 deletions
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.UseDifferentEnvelopeFrom {
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.UseDifferentEnvelopeFrom {
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)