-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add option for mailer to override mail headers #27860
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
a89d298
mailer: add option to set Return-Path
6543 89a29c6
Update custom/conf/app.example.ini
6543 30db9c3
Update docs/content/administration/config-cheat-sheet.en-us.md
silverwind 9f8b1c7
Merge branch 'main' into mail_set-return-path
6543 853a563
Merge branch 'main' into mail_set-return-path
6543 be5beec
Merge branch 'main' into mail_set-return-path
6543 37073a4
use generic OverrideHeader
6543 a6298a6
Merge branch 'main' into mail_set-return-path
6543 9e7653e
cleanup docs
6543 bcda0f3
tewak docs
6543 b8d65d9
cleanup
6543 2a625a3
init map
6543 680f13a
sanitize config input on parsing
6543 15f2103
fix docu and it works :D
6543 1c65d9a
fix docu lint
6543 bc7ca47
fix misspell
6543 117b702
simple docs
6543 261d604
Apply suggestions from code review
6543 dc8f145
Apply suggestions from code review
6543 776c550
migrate to docusaurus note
6543 f1b7ee2
simple passthrough config
6543 8204fac
use warning and add example to delete an header
6543 035976e
Merge branch 'main' into mail_set-return-path
6543 f433b1c
make explizite everything overridable
6543 39352d9
Add test for mailer.ToMessage()
6543 a8a08d0
Merge branch 'main' into mail_set-return-path
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1687,6 +1687,16 @@ LEVEL = Info | |
;; convert \r\n to \n for Sendmail | ||
;SENDMAIL_CONVERT_CRLF = true | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;[mailer.override_header] | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; This is empty by default, use it only if you know what you need it for. | ||
;Reply-To = [email protected], [email protected] | ||
;Content-Type = text/html; charset=utf-8 | ||
;In-Reply-To = | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;[email.incoming] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -724,11 +724,13 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type | |
|
||
## Mailer (`mailer`) | ||
|
||
⚠️ This section is for Gitea 1.18 and later. If you are using Gitea 1.17 or older, | ||
:::warning | ||
This section is for Gitea 1.18 and later. If you are using Gitea 1.17 or older, | ||
please refer to | ||
[Gitea 1.17 app.ini example](https://github.com/go-gitea/gitea/blob/release/v1.17/custom/conf/app.example.ini) | ||
and | ||
[Gitea 1.17 configuration document](https://github.com/go-gitea/gitea/blob/release/v1.17/docs/content/doc/advanced/config-cheat-sheet.en-us.md) | ||
::: | ||
|
||
- `ENABLED`: **false**: Enable to use a mail service. | ||
- `PROTOCOL`: **_empty_**: Mail server protocol. One of "smtp", "smtps", "smtp+starttls", "smtp+unix", "sendmail", "dummy". _Before 1.18, this was inferred from a combination of `MAILER_TYPE` and `IS_TLS_ENABLED`._ | ||
|
@@ -761,6 +763,21 @@ and | |
- `SEND_BUFFER_LEN`: **100**: Buffer length of mailing queue. **DEPRECATED** use `LENGTH` in `[queue.mailer]` | ||
- `SEND_AS_PLAIN_TEXT`: **false**: Send mails only in plain text, without HTML alternative. | ||
|
||
## Override Email Headers (`mailer.override_header`) | ||
|
||
:::warning | ||
This is empty by default, use it only if you know what you need it for. | ||
::: | ||
|
||
examples would be: | ||
|
||
```ini | ||
[mailer.override_header] | ||
Reply-To = [email protected], [email protected] | ||
Content-Type = text/html; charset=utf-8 | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
In-Reply-To = | ||
``` | ||
|
||
## Incoming Email (`email.incoming`) | ||
|
||
- `ENABLED`: **false**: Enable handling of incoming emails. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
package mailer | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -36,3 +37,78 @@ func TestGenerateMessageID(t *testing.T) { | |
gm = m.ToMessage() | ||
assert.Equal(t, "<[email protected]>", gm.GetHeader("Message-ID")[0]) | ||
} | ||
|
||
func TestToMessage(t *testing.T) { | ||
oldConf := *setting.MailService | ||
defer func() { | ||
setting.MailService = &oldConf | ||
}() | ||
setting.MailService.From = "[email protected]" | ||
|
||
m1 := Message{ | ||
Info: "info", | ||
FromAddress: "[email protected]", | ||
FromDisplayName: "Test Gitea", | ||
To: "[email protected]", | ||
Subject: "Issue X Closed", | ||
Body: "Some Issue got closed by Y-Man", | ||
} | ||
|
||
buf := &strings.Builder{} | ||
_, err := m1.ToMessage().WriteTo(buf) | ||
assert.NoError(t, err) | ||
header, _ := extractMailHeaderAndContent(t, buf.String()) | ||
assert.EqualValues(t, map[string]string{ | ||
"Content-Type": "multipart/alternative;", | ||
"Date": "Mon, 01 Jan 0001 00:00:00 +0000", | ||
"From": "\"Test Gitea\" <[email protected]>", | ||
"Message-ID": "<autogen--6795364578871-69c000786adc60dc@localhost>", | ||
"Mime-Version": "1.0", | ||
"Subject": "Issue X Closed", | ||
"To": "[email protected]", | ||
"X-Auto-Response-Suppress": "All", | ||
}, header) | ||
|
||
setting.MailService.OverrideHeader = map[string][]string{ | ||
"Message-ID": {""}, // delete message id | ||
"Auto-Submitted": {"auto-generated"}, // suppress auto replay | ||
} | ||
|
||
buf = &strings.Builder{} | ||
_, err = m1.ToMessage().WriteTo(buf) | ||
assert.NoError(t, err) | ||
header, _ = extractMailHeaderAndContent(t, buf.String()) | ||
assert.EqualValues(t, map[string]string{ | ||
"Content-Type": "multipart/alternative;", | ||
"Date": "Mon, 01 Jan 0001 00:00:00 +0000", | ||
"From": "\"Test Gitea\" <[email protected]>", | ||
"Message-ID": "", | ||
"Mime-Version": "1.0", | ||
"Subject": "Issue X Closed", | ||
"To": "[email protected]", | ||
"X-Auto-Response-Suppress": "All", | ||
"Auto-Submitted": "auto-generated", | ||
}, header) | ||
} | ||
|
||
func extractMailHeaderAndContent(t *testing.T, mail string) (map[string]string, string) { | ||
header := make(map[string]string) | ||
|
||
parts := strings.SplitN(mail, "boundary=", 2) | ||
if !assert.Len(t, parts, 2) { | ||
return nil, "" | ||
} | ||
content := strings.TrimSpace("boundary=" + parts[1]) | ||
|
||
hParts := strings.Split(parts[0], "\n") | ||
|
||
for _, hPart := range hParts { | ||
parts := strings.SplitN(hPart, ":", 2) | ||
hk := strings.TrimSpace(parts[0]) | ||
if hk != "" { | ||
header[hk] = strings.TrimSpace(parts[1]) | ||
} | ||
} | ||
|
||
return header, content | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.