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 settings to allow different SMTP envelope from address #17479

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,10 @@ PATH =
;; Mail from address, RFC 5322. This can be just an email address, or the `"Name" <email@example.com>` format
;FROM =
;;
;; 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
;USE_DIFFERENT_ENVELOPE_FROM=false
;ENVELOPE_FROM =
;;
;; Mailer user name and password
;; Please Note: Authentication is only supported when the SMTP server communication is encrypted with TLS (this can be via STARTTLS) or `HOST=localhost`.
;USER =
Expand Down
2 changes: 2 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
- 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`.
- `FROM`: **\<empty\>**: Mail from address, RFC 5322. This can be just an email address, or
the "Name" \<email@example.com\> format.
- `USE_DIFFERENT_ENVELOPE_FROM`: **false**: Set to **true** to use the value of `ENVELOPE_FROM` as the From address on the SMTP mail envelope.
- `ENVELOPE_FROM`: **\<empty\>**: Address set as the From address on the SMTP mail envelope if `USE_DIFFERENT_ENVELOPE_FROM` is set to true.
- `USER`: **\<empty\>**: Username of mailing user (usually the sender's e-mail address).
- `PASSWD`: **\<empty\>**: Password of mailing user. Use \`your password\` for quoting if you use special characters in the password.
- 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.
Expand Down
18 changes: 11 additions & 7 deletions modules/setting/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
// Mailer represents mail service.
type Mailer struct {
// Mailer
Name string
From string
FromName string
FromEmail string
SendAsPlainText bool
MailerType string
SubjectPrefix string
Name string
From string
EnvelopeFrom string
UseDifferentEnvelopeFrom bool
FromName string
FromEmail string
SendAsPlainText bool
MailerType string
SubjectPrefix string

// SMTP sender
Host string
Expand Down Expand Up @@ -73,6 +75,8 @@ func newMailService() {
SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute),
}
MailService.From = sec.Key("FROM").MustString(MailService.User)
MailService.UseDifferentEnvelopeFrom = sec.Key("USE_DIFFERENT_ENVELOPE_FROM").MustBool()
MailService.EnvelopeFrom = sec.Key("ENVELOPE_FROM").MustString("")

if sec.HasKey("ENABLE_HTML_ALTERNATIVE") {
log.Warn("ENABLE_HTML_ALTERNATIVE is deprecated, use SEND_AS_PLAIN_TEXT")
Expand Down
17 changes: 14 additions & 3 deletions services/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,14 @@ func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
}
}

if err = client.Mail(from); err != nil {
return fmt.Errorf("Mail: %v", err)
if opts.UseDifferentEnvelopeFrom {
if err = client.Mail(opts.EnvelopeFrom); err != nil {
return fmt.Errorf("Mail: %v", err)
}
} else {
if err = client.Mail(from); err != nil {
return fmt.Errorf("Mail: %v", err)
}
}

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

args := []string{"-f", from, "-i"}
envelopeFrom := from
if setting.MailService.UseDifferentEnvelopeFrom {
envelopeFrom = setting.MailService.EnvelopeFrom
}

args := []string{"-f", envelopeFrom, "-i"}
args = append(args, setting.MailService.SendmailArgs...)
args = append(args, to...)
log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args)
Expand Down