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 POST unsubscribe link to the email headers #11

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ To send email user need to create a sender first and then use `Send` method. The
- parameters (`email.Params`)
```go
type Params struct {
From string // From email field
To []string // From email field
Subject string // Email subject
Attachments []string // Attachments
InlineImages []string // Embedding directly to email body. Autogenerated Content-Id (cid) equals to file name
From string // From email field
To []string // From email field
Subject string // Email subject
UnsubscribeLink string // POST, https://support.google.com/mail/answer/81126 -> "Use one-click unsubscribe"
Attachments []string // Attachments path
InlineImages []string // Embedding directly to email body. Autogenerated Content-Id (cid) equals to file name
}
```

Expand Down
16 changes: 11 additions & 5 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ type Sender struct {

// Params contains all user-defined parameters to send emails
type Params struct {
From string // From email field
To []string // From email field
Subject string // Email subject
Attachments []string // Attachments path
InlineImages []string // InlineImages images path
From string // From email field
To []string // From email field
Subject string // Email subject
UnsubscribeLink string // POST, https://support.google.com/mail/answer/81126 -> "Use one-click unsubscribe"
Attachments []string // Attachments path
InlineImages []string // InlineImages images path
}

// Logger is used to log errors and debug messages
Expand Down Expand Up @@ -212,6 +213,11 @@ func (em *Sender) buildMessage(text string, params Params) (message string, err
message = addHeader(message, "To", strings.Join(params.To, ","))
message = addHeader(message, "Subject", mime.BEncoding.Encode("utf-8", params.Subject))

if params.UnsubscribeLink != "" {
message = addHeader(message, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click")
message = addHeader(message, "List-Unsubscribe", "<"+params.UnsubscribeLink+">")
}

withAttachments := len(params.Attachments) > 0
withInlineImg := len(params.InlineImages) > 0

Expand Down
9 changes: 5 additions & 4 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,14 @@ func TestEmail_buildMessageWithMIME(t *testing.T) {
e := NewSender("localhost", ContentType("text/html"))

msg, err := e.buildMessage("this is a test\n12345\n", Params{
From: "from@example.com",
To: []string{"to@example.com"},
Subject: "non-ascii symbols: Привет",
From: "from@example.com",
To: []string{"to@example.com"},
Subject: "non-ascii symbols: Привет",
UnsubscribeLink: "https://example.com/unsubscribe",
})
require.NoError(t, err)
assert.Contains(t, msg, "Content-Transfer-Encoding: quoted-printable\nContent-Type: text/html; charset=\"UTF-8\"", msg)
assert.Contains(t, msg, "From: from@example.com\nTo: to@example.com\nSubject: =?utf-8?b?bm9uLWFzY2lpIHN5bWJvbHM6INCf0YDQuNCy0LXRgg==?=\nMIME-version: 1.0", msg)
assert.Contains(t, msg, "From: from@example.com\nTo: to@example.com\nSubject: =?utf-8?b?bm9uLWFzY2lpIHN5bWJvbHM6INCf0YDQuNCy0LXRgg==?=\nList-Unsubscribe-Post: List-Unsubscribe=One-Click\nList-Unsubscribe: <https://example.com/unsubscribe>\nMIME-version: 1.0", msg)
assert.Contains(t, msg, "\n\nthis is a test\r\n12345\r\n", msg)
assert.Contains(t, msg, "Date: ", msg)
}
Expand Down