Skip to content

Commit

Permalink
added go-dkim package and List-Unsubscribe header to the project (#54)
Browse files Browse the repository at this point in the history
* added go-dkim package to the project

* updated readme for dkim and unsubscribe examples

* Removed privatekey  because the parameter is not needed

* requested changes for dkim and list-unsubscribe adding
  • Loading branch information
atifceylan authored Dec 21, 2021
1 parent c20e8ef commit 780823a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import (
"log"

"github.com/xhit/go-simple-mail/v2"
"github.com/toorop/go-dkim"
)

const htmlBody = `<html>
Expand All @@ -109,6 +110,34 @@ const htmlBody = `<html>
</body>
</html>`

const privateKey = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAwGrscUxi9zEa9oMOJbS0kLVHZXNIW+EBjY7KFWIZSxuGAils
wBVl+s5mMRrR5VlkyLQNulAdNemg6OSeB0R+2+8/lkHiMrimqQckZ5ig8slBoZhZ
wUoL/ZkeQa1bacbdww5TuWkiVPD9kooT/+TZW1P/ugd6oYjpOI56ZjsXzJw5pz7r
DiwcIJJaaDIqvvc5C4iW94GZjwtmP5pxhvBZ5D6Uzmh7Okvi6z4QCKzdJQLdVmC0
CMiFeh2FwqMkVpjZhNt3vtCo7Z51kwHVscel6vl51iQFq/laEzgzAWOUQ+ZEoQpL
uTaUiYzzNyEdGEzZ2CjMMoO8RgtXnUo2qX2FDQIDAQABAoIBAHWKW3kycloSMyhX
EnNSGeMz+bMtYwxNPMeebC/3xv+shoYXjAkiiTNWlfJ1MbbqjrhT1Pb1LYLbfqIF
1csWum/bjHpbMLRPO++RH1nxUJA/BMqT6HA8rWpy+JqiLW9GPf2DaP2gDYrZ0+yK
UIFG6MfzXgnju7OlkOItlvOQMY+Y501u/h6xnN2yTeRqXXJ1YlWFPRIeFdS6UOtL
J2wSxRVdymHbGwf+D7zet7ngMPwFBsbEN/83KGLRjkt8+dMQeUeob+nslsQofCZx
iokIAvByTugmqrB4JqhNkAlZhC0mqkRQh7zUFrxSj5UppMWlxLH+gPFZHKAsUJE5
mqmylcECgYEA8I/f90cpF10uH4NPBCR4+eXq1PzYoD+NdXykN65bJTEDZVEy8rBO
phXRNfw030sc3R0waQaZVhFuSgshhRuryfG9c1FP6tQhqi/jiEj9IfCW7zN9V/P2
r16pGjLuCK4SyxUC8H58Q9I0X2CQqFamtkLXC6Ogy86rZfIc8GcvZ9UCgYEAzMQZ
WAiLhRF2MEmMhKL+G3jm20r+dOzPYkfGxhIryluOXhuUhnxZWL8UZfiEqP5zH7Li
NeJvLz4pOL45rLw44qiNu6sHN0JNaKYvwNch1wPT/3/eDNZKKePqbAG4iamhjLy5
gjO1KgA5FBbcNN3R6fuJAg1e4QJCOuo55eW6vFkCgYEA7UBIV72D5joM8iFzvZcn
BPdfqh2QnELxhaye3ReFZuG3AqaZg8akWqLryb1qe8q9tclC5GIQulTInBfsQDXx
MGLNQL0x/1ylsw417kRl+qIoidMTTLocUgse5erS3haoDEg1tPBaKB1Zb7NyF8QV
+W1kX2NKg5bZbdrh9asekt0CgYA6tUam7NxDrLv8IDo/lRPSAJn/6cKG95aGERo2
k+MmQ5XP+Yxd+q0LOs24ZsZyRXHwdrNQy7khDGt5L2EN23Fb2wO3+NM6zrGu/WbX
nVbAdQKFUL3zZEUjOYtuqBemsJH27e0qHXUls6ap0dwU9DxJH6sqgXbggGtIxPsQ
pQsjEQKBgQC9gAqAj+ZtMXNG9exVPT8I15reox9kwxGuvJrRu/5eSi6jLR9z3x9P
2FrgxQ+GCB2ypoOUcliXrKesdSbolUilA8XQn/M113Lg8oA3gJXbAKqbTR/EgfUU
kvYaR/rTFnivF4SL/P4k/gABQoJuFUtSKdouELqefXB+e94g/G++Bg==
-----END RSA PRIVATE KEY-----`

func main() {
server := mail.NewSMTPClient()

Expand Down Expand Up @@ -151,7 +180,8 @@ func main() {
email.SetFrom("From Example <[email protected]>").
AddTo("[email protected]").
AddCc("[email protected]").
SetSubject("New Go Email")
SetSubject("New Go Email").
SetListUnsubscribe("<mailto:[email protected]?subject=https://example.com/unsubscribe>").

email.SetBody(mail.TextHTML, htmlBody)

Expand All @@ -163,6 +193,21 @@ func main() {
// add inline
email.Attach(&mail.File{FilePath: "/path/to/image.png", Name:"Gopher.png", Inline: true})

// you can add dkim signature to the email.
// to add dkim, you need a private key already created one.
if privateKey != "" {
options := dkim.NewSigOptions()
options.PrivateKey = []byte(privateKey)
options.Domain = "example.com"
options.Selector = "default"
options.SignatureExpireIn = 3600
options.Headers = []string{"from", "date", "mime-version", "received", "received"}
options.AddSignatureTimestamp = true
options.Canonicalization = "relaxed/relaxed"

email.SetDkim(options)
}

// always check error after send
if email.Error != nil{
log.Fatal(email.Error)
Expand Down
42 changes: 41 additions & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/textproto"
"strconv"
"time"

"github.com/toorop/go-dkim"
)

// Email represents an email message.
Expand All @@ -27,6 +29,7 @@ type Email struct {
Encoding encoding
Error error
SMTPServer *smtpClient
DkimMsg string
}

/*
Expand Down Expand Up @@ -420,6 +423,36 @@ func (email *Email) SetSubject(subject string) *Email {
return email
}

// SetListUnsubscribe sets the Unsubscribe address.
func (email *Email) SetListUnsubscribe(address string) *Email {
if email.Error != nil {
return email
}

email.AddHeader("List-Unsubscribe", address)

return email
}

// SetDkim adds DomainKey signature to the email message (header+body)
func (email *Email) SetDkim(options dkim.SigOptions) *Email {
if email.Error != nil {
return email
}

msg := []byte(email.GetMessage())
err := dkim.Sign(&msg, options)

if err != nil {
email.Error = errors.New("Mail Error: cannot dkim sign message due: %s" + err.Error())
return email
}

email.DkimMsg = string(msg)

return email
}

// SetBody sets the body of the email message.
func (email *Email) SetBody(contentType contentType, body string) *Email {
if email.Error != nil {
Expand Down Expand Up @@ -488,6 +521,8 @@ func (email *Email) AddHeader(header string, values ...string) *Email {
return email
}
email.SetDate(values[0])
case "List-Unsubscribe":
fallthrough
default:
email.headers[header] = values
}
Expand Down Expand Up @@ -636,7 +671,12 @@ func (email *Email) SendEnvelopeFrom(from string, client *SMTPClient) error {
return errors.New("Mail Error: No recipient specified")
}

msg := email.GetMessage()
var msg string
if email.DkimMsg != "" {
msg = email.DkimMsg
} else {
msg = email.GetMessage()
}

return send(from, email.recipients, msg, client)
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module github.com/xhit/go-simple-mail/v2
require (
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208
)

go 1.13
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 h1:PM5hJF7HVfNWmCjMdEfbuOBNXSVF2cMFGgQTPdKCbwM=
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208/go.mod h1:BzWtXXrXzZUvMacR0oF/fbDDgUPO8L36tDMmRAf14ns=
github.com/xhit/go-simple-mail v2.2.2+incompatible h1:Hm2VGfLqiQJ/NnC8SYsrPOPyVYIlvP2kmnotP4RIV74=
github.com/xhit/go-simple-mail v2.2.2+incompatible/go.mod h1:I8Ctg6vIJZ+Sv7k/22M6oeu/tbFumDY0uxBuuLbtU7Y=

0 comments on commit 780823a

Please sign in to comment.