Skip to content

Commit 1514e13

Browse files
authored
Add option to convert CRLF to LF line endings for sendmail (#18075)
It appears that several versions of sendmail require that the mail is sent to them with LF line endings instead of CRLF endings - which of course they will then convert back to CRLF line endings to comply with the SMTP standard. This PR adds another setting SENDMAIL_CONVERT_CRLF which will pass the message writer through a filter. This will filter out and convert CRLFs to LFs before writing them out to sendmail. Fix #18024 Signed-off-by: Andrew Thornton <[email protected]>
1 parent bf7b083 commit 1514e13

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

custom/conf/app.example.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,9 @@ PATH =
14941494
;;
14951495
;; Timeout for Sendmail
14961496
;SENDMAIL_TIMEOUT = 5m
1497+
;;
1498+
;; convert \r\n to \n for Sendmail
1499+
;SENDMAIL_CONVERT_CRLF = true
14971500

14981501
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14991502
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
667667
command or full path).
668668
- `SENDMAIL_ARGS`: **_empty_**: Specify any extra sendmail arguments.
669669
- `SENDMAIL_TIMEOUT`: **5m**: default timeout for sending email through sendmail
670+
- `SENDMAIL_CONVERT_CRLF`: **true**: Most versions of sendmail prefer LF line endings rather than CRLF line endings. Set this to false if your version of sendmail requires CRLF line endings.
670671
- `SEND_BUFFER_LEN`: **100**: Buffer length of mailing queue. **DEPRECATED** use `LENGTH` in `[queue.mailer]`
671672

672673
## Cache (`cache`)

modules/setting/mailer.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ type Mailer struct {
3737
IsTLSEnabled bool
3838

3939
// Sendmail sender
40-
SendmailPath string
41-
SendmailArgs []string
42-
SendmailTimeout time.Duration
40+
SendmailPath string
41+
SendmailArgs []string
42+
SendmailTimeout time.Duration
43+
SendmailConvertCRLF bool
4344
}
4445

4546
var (
@@ -71,8 +72,9 @@ func newMailService() {
7172
IsTLSEnabled: sec.Key("IS_TLS_ENABLED").MustBool(),
7273
SubjectPrefix: sec.Key("SUBJECT_PREFIX").MustString(""),
7374

74-
SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"),
75-
SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute),
75+
SendmailPath: sec.Key("SENDMAIL_PATH").MustString("sendmail"),
76+
SendmailTimeout: sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute),
77+
SendmailConvertCRLF: sec.Key("SENDMAIL_CONVERT_CRLF").MustBool(true),
7678
}
7779
MailService.From = sec.Key("FROM").MustString(MailService.User)
7880
MailService.EnvelopeFrom = sec.Key("ENVELOPE_FROM").MustString("")

services/mailer/mailer.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,20 @@ func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
290290
return err
291291
}
292292

293-
_, err = msg.WriteTo(pipe)
293+
if setting.MailService.SendmailConvertCRLF {
294+
buf := &strings.Builder{}
295+
_, err = msg.WriteTo(buf)
296+
if err == nil {
297+
_, err = strings.NewReplacer("\r\n", "\n").WriteString(pipe, buf.String())
298+
}
299+
} else {
300+
_, err = msg.WriteTo(pipe)
301+
}
294302

295303
// we MUST close the pipe or sendmail will hang waiting for more of the message
296304
// Also we should wait on our sendmail command even if something fails
297305
closeError = pipe.Close()
298306
waitError = cmd.Wait()
299-
300307
if err != nil {
301308
return err
302309
} else if closeError != nil {

0 commit comments

Comments
 (0)