Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Lint free #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linters:
enable:
- depguard
- dupl
- errcheck
- goconst
- gocritic
- gofmt
Expand All @@ -30,8 +31,6 @@ linters:
- stylecheck
- unconvert
- unparam
disable:
- errcheck # TODO

linters-settings:
dupl:
Expand Down
1 change: 0 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
// efficiently.
//
// More info on Github: https://github.com/go-mail/mail
//
package gomail
4 changes: 2 additions & 2 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ func TestRename(t *testing.T) {
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetBody("text/plain", "Test")
name, copy := mockCopyFile("/tmp/test.pdf")
name, copyFile := mockCopyFile("/tmp/test.pdf")
rename := Rename("another.pdf")
m.Attach(name, copy, rename)
m.Attach(name, copyFile, rename)

want := &message{
from: "from@example.com",
Expand Down
28 changes: 22 additions & 6 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"fmt"
"io"
"log/slog"
"net"
"net/smtp"
"strings"
Expand Down Expand Up @@ -94,7 +95,9 @@ func (d *Dialer) Dial() (SendCloser, error) {
}

if d.Timeout > 0 {
conn.SetDeadline(time.Now().Add(d.Timeout))
if tErr := conn.SetDeadline(time.Now().Add(d.Timeout)); tErr != nil {
slog.Error("unable to set connection deadline", "error", tErr.Error())
}
}

if d.LocalName != "" {
Expand All @@ -113,7 +116,9 @@ func (d *Dialer) Dial() (SendCloser, error) {

if ok {
if err := c.StartTLS(d.tlsConfig()); err != nil {
c.Close()
if cErr := c.Close(); cErr != nil {
slog.Error("unable to close smtpClient after attempting to startTLS", "error", cErr.Error())
}
return nil, err
}
}
Expand All @@ -138,7 +143,9 @@ func (d *Dialer) Dial() (SendCloser, error) {

if d.Auth != nil {
if err = c.Auth(d.Auth); err != nil {
c.Close()
if cErr := c.Close(); cErr != nil {
slog.Error("unable to close smtpClient after attempting to set Auth", "error", cErr.Error())
}
return nil, err
}
}
Expand Down Expand Up @@ -205,7 +212,12 @@ func (d *Dialer) DialAndSend(m ...*Message) error {
if err != nil {
return err
}
defer s.Close()

defer func() {
if sErr := s.Close(); sErr != nil {
slog.Error("unable to close sendCloser after attempting to Close", "error", sErr.Error())
}
}()

return Send(s, m...)
}
Expand All @@ -230,7 +242,9 @@ func (c *smtpSender) retryError(err error) bool {

func (c *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
if c.d.Timeout > 0 {
c.conn.SetDeadline(time.Now().Add(c.d.Timeout))
if err := c.conn.SetDeadline(time.Now().Add(c.d.Timeout)); err != nil {
slog.Error("unable to set context deadline in send", "error", err.Error())
}
}

if err := c.Mail(from); err != nil {
Expand Down Expand Up @@ -260,7 +274,9 @@ func (c *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
}

if _, err = msg.WriteTo(w); err != nil {
w.Close()
if wErr := w.Close(); wErr != nil {
slog.Error("unable to close writeCloser after attempting to write message", "error", wErr.Error())
}
return err
}

Expand Down
30 changes: 23 additions & 7 deletions writeto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"errors"
"io"
"log/slog"
"mime"
"mime/multipart"
"path/filepath"
Expand Down Expand Up @@ -80,7 +81,9 @@ type messageWriter struct {
func (w *messageWriter) openMultipart(mimeType, boundary string) {
mw := multipart.NewWriter(w)
if boundary != "" {
mw.SetBoundary(boundary)
if err := mw.SetBoundary(boundary); err != nil {
slog.Error("unable to set boundary in openMultipart", "error", err.Error())
}
}
contentType := "multipart/" + mimeType + ";\r\n boundary=" + mw.Boundary()
w.writers[w.depth] = mw
Expand All @@ -102,7 +105,9 @@ func (w *messageWriter) createPart(h map[string][]string) {

func (w *messageWriter) closeMultipart() {
if w.depth > 0 {
w.writers[w.depth-1].Close()
if err := w.writers[w.depth-1].Close(); err != nil {
slog.Error("unable to close writers in closeMultipart", "error", err.Error())
}
w.depth--
}
}
Expand Down Expand Up @@ -270,13 +275,17 @@ func (w *messageWriter) writeBody(f func(io.Writer) error, enc Encoding) {
case Base64:
wc := base64.NewEncoder(base64.StdEncoding, newBase64LineWriter(subWriter))
w.err = f(wc)
wc.Close()
if err := wc.Close(); err != nil {
slog.Error("unable to close writer for base64 write body", "error", err.Error())
}
case Unencoded:
w.err = f(subWriter)
default:
wc := newQPWriter(subWriter)
w.err = f(wc)
wc.Close()
if err := wc.Close(); err != nil {
slog.Error("unable to close newQPWriter", "error", err.Error())
}
}
}

Expand All @@ -297,14 +306,21 @@ func newBase64LineWriter(w io.Writer) *base64LineWriter {
func (w *base64LineWriter) Write(p []byte) (int, error) {
n := 0
for len(p)+w.lineLen > maxLineLen {
w.w.Write(p[:maxLineLen-w.lineLen])
w.w.Write([]byte("\r\n"))
if _, err := w.w.Write(p[:maxLineLen-w.lineLen]); err != nil {
slog.Error("unable to write line", "error", err.Error())
}

if _, err := w.w.Write([]byte("\r\n")); err != nil {
slog.Error("unable to write newline characters", "error", err.Error())
}
p = p[maxLineLen-w.lineLen:]
n += maxLineLen - w.lineLen
w.lineLen = 0
}

w.w.Write(p)
if _, err := w.w.Write(p); err != nil {
slog.Error("unable to write final line", "error", err.Error())
}
w.lineLen += len(p)

return n + len(p), nil
Expand Down