Skip to content

Commit

Permalink
feat: add smtp headers config option (#1747)
Browse files Browse the repository at this point in the history
Closes #1725
  • Loading branch information
emtammaru committed Sep 14, 2021
1 parent ae86860 commit 7ffe0e9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
6 changes: 6 additions & 0 deletions courier/courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ func (m *Courier) DispatchMessage(ctx context.Context, msg Message) error {

gm.SetHeader("To", msg.Recipient)
gm.SetHeader("Subject", msg.Subject)

headers := m.d.Config(ctx).CourierSMTPHeaders()
for k, v := range headers {
gm.SetHeader(k, v)
}

gm.SetBody("text/plain", msg.Body)

tmpl, err := NewEmailTemplateFromMessage(m.d.Config(ctx), msg)
Expand Down
10 changes: 8 additions & 2 deletions courier/courier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ func TestSMTP(t *testing.T) {
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, id)

// The third email contains a sender name
// The third email contains a sender name and custom headers
conf.MustSet(config.ViperKeyCourierSMTPFromName, "Bob")
conf.MustSet(config.ViperKeyCourierSMTPHeaders+".test-stub-header1", "foo")
conf.MustSet(config.ViperKeyCourierSMTPHeaders+".test-stub-header2", "bar")
customerHeaders := conf.CourierSMTPHeaders()
require.Len(t, customerHeaders, 2)
id, err = c.QueueEmail(ctx, templates.NewTestStub(conf, &templates.TestStubModel{
To: "test-recipient-3@example.org",
Subject: "test-subject-3",
Expand Down Expand Up @@ -122,6 +126,8 @@ func TestSMTP(t *testing.T) {
assert.Contains(t, string(body), "test-stub@ory.sh")
}

// Assertion for the third email with sender name
// Assertion for the third email with sender name and headers
assert.Contains(t, string(body), "Bob")
assert.Contains(t, string(body), `"test-stub-header1":["foo"]`)
assert.Contains(t, string(body), `"test-stub-header2":["bar"]`)
}
15 changes: 15 additions & 0 deletions docs/docs/concepts/email-sms.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ Hi, please verify your account by clicking the following link:
Hi, please verify your account by clicking the following link: {{ .VerificationURL }}
```

### Custom Headers

You can configure custom SMTP headers. For example, if integrating with AWS SES
SMTP interface, the headers can be configured for cross-account sending:

```yaml title="path/to/my/kratos/config.yml"
# $ kratos -c path/to/my/kratos/config.yml serve
courier:
smtp:
headers:
X-SES-SOURCE-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com
X-SES-FROM-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com
X-SES-RETURN-PATH-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com
```

## Sending SMS

The Sending SMS feature is not supported at present. It will be available in a
Expand Down
40 changes: 40 additions & 0 deletions docs/docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2171,6 +2171,46 @@ courier:
#
from_name: Bob

## SMTP Headers ##
#
# These headers will be passed in the SMTP conversation -- e.g. when using the AWS SES SMTP interface for cross-account sending.
#
# Examples:
# - X-SES-SOURCE-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com
# X-SES-FROM-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com
# X-SES-RETURN-PATH-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com
#
headers:
## X-SES-SOURCE-ARN ##
#
# Set this value using environment variables on
# - Linux/macOS:
# $ export COURIER_SMTP_HEADERS_X-SES-SOURCE-ARN=<value>
# - Windows Command Line (CMD):
# > set COURIER_SMTP_HEADERS_X-SES-SOURCE-ARN=<value>
#
X-SES-SOURCE-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com

## X-SES-FROM-ARN ##
#
# Set this value using environment variables on
# - Linux/macOS:
# $ export COURIER_SMTP_HEADERS_X-SES-FROM-ARN=<value>
# - Windows Command Line (CMD):
# > set COURIER_SMTP_HEADERS_X-SES-FROM-ARN=<value>
#
X-SES-FROM-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com

## X-SES-RETURN-PATH-ARN ##
#
# Set this value using environment variables on
# - Linux/macOS:
# $ export COURIER_SMTP_HEADERS_X-SES-RETURN-PATH-ARN=<value>
# - Windows Command Line (CMD):
# > set COURIER_SMTP_HEADERS_X-SES-RETURN-PATH-ARN=<value>
#
X-SES-RETURN-PATH-ARN: arn:aws:ses:us-west-2:123456789012:identity/example.com

## SMTP Sender Address ##
#
# The recipient of an email will see this as the sender address.
Expand Down
12 changes: 12 additions & 0 deletions driver/config/.schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,18 @@
"examples": [
"Bob"
]
},
"headers": {
"title": "SMTP Headers",
"description": "These headers will be passed in the SMTP conversation -- e.g. when using the AWS SES SMTP interface for cross-account sending.",
"type": "object",
"examples": [
{
"X-SES-SOURCE-ARN": "arn:aws:ses:us-west-2:123456789012:identity/example.com",
"X-SES-FROM-ARN": "arn:aws:ses:us-west-2:123456789012:identity/example.com",
"X-SES-RETURN-PATH-ARN": "arn:aws:ses:us-west-2:123456789012:identity/example.com"
}
]
}
},
"required": [
Expand Down
5 changes: 5 additions & 0 deletions driver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
ViperKeyCourierTemplatesPath = "courier.template_override_path"
ViperKeyCourierSMTPFrom = "courier.smtp.from_address"
ViperKeyCourierSMTPFromName = "courier.smtp.from_name"
ViperKeyCourierSMTPHeaders = "courier.smtp.headers"
ViperKeySecretsDefault = "secrets.default"
ViperKeySecretsCookie = "secrets.cookie"
ViperKeyPublicBaseURL = "serve.public.base_url"
Expand Down Expand Up @@ -714,6 +715,10 @@ func (p *Config) CourierTemplatesRoot() string {
return p.p.StringF(ViperKeyCourierTemplatesPath, "courier/builtin/templates")
}

func (p *Config) CourierSMTPHeaders() map[string]string {
return p.p.StringMap(ViperKeyCourierSMTPHeaders)
}

func splitUrlAndFragment(s string) (string, string) {
i := strings.IndexByte(s, '#')
if i < 0 {
Expand Down

0 comments on commit 7ffe0e9

Please sign in to comment.