From 780823a4f0ad60e6e4eba93dd23794e114df3924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?At=C4=B1f=20Ceylan?= Date: Wed, 22 Dec 2021 01:16:23 +0300 Subject: [PATCH] added go-dkim package and List-Unsubscribe header to the project (#54) * 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 --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++++- email.go | 42 +++++++++++++++++++++++++++++++++++++++++- go.mod | 3 +++ go.sum | 4 ++++ 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 go.sum diff --git a/README.md b/README.md index e7e5707..a7a3ebc 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ import ( "log" "github.com/xhit/go-simple-mail/v2" + "github.com/toorop/go-dkim" ) const htmlBody = ` @@ -109,6 +110,34 @@ const htmlBody = ` ` +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 +UIFG6MfzXgnju7OlkOIt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ATE KEY-----` + func main() { server := mail.NewSMTPClient() @@ -151,7 +180,8 @@ func main() { email.SetFrom("From Example "). AddTo("xhit@example.com"). AddCc("otherto@example.com"). - SetSubject("New Go Email") + SetSubject("New Go Email"). + SetListUnsubscribe(""). email.SetBody(mail.TextHTML, htmlBody) @@ -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) diff --git a/email.go b/email.go index 599ac38..64af7b5 100644 --- a/email.go +++ b/email.go @@ -10,6 +10,8 @@ import ( "net/textproto" "strconv" "time" + + "github.com/toorop/go-dkim" ) // Email represents an email message. @@ -27,6 +29,7 @@ type Email struct { Encoding encoding Error error SMTPServer *smtpClient + DkimMsg string } /* @@ -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 { @@ -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 } @@ -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) } diff --git a/go.mod b/go.mod index c706275..7502869 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bd730ca --- /dev/null +++ b/go.sum @@ -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=