diff --git a/go.mod b/go.mod index 093ff54..858bb61 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,6 @@ module github.com/KeepSec-Technologies/Mail2Go go 1.20 -require gopkg.in/mail.v2 v2.3.1 - -require gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect +require ( + github.com/wneessen/go-mail v0.4.0 // indirect +) diff --git a/go.sum b/go.sum index 54dde6d..3d2a4c7 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= -gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk= -gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw= +github.com/wneessen/go-mail v0.4.0 h1:Oo4HLIV8My7G9JuZkoOX6eipXQD+ACvIqURYeIzUc88= +github.com/wneessen/go-mail v0.4.0/go.mod h1:zxOlafWCP/r6FEhAaRgH4IC1vg2YXxO0Nar9u0IScZ8= diff --git a/smtp.go b/smtp.go index 72c93f3..58e5774 100644 --- a/smtp.go +++ b/smtp.go @@ -4,35 +4,46 @@ import ( "fmt" "strings" - "gopkg.in/mail.v2" + "github.com/wneessen/go-mail" ) // sendEmail constructs and sends an email with the specified HTML body and attachments. func sendEmail(smtpServer string, smtpPort int, username string, password string, from string, to []string, subject, body, bodyFile string, attachments []string) error { - m := mail.NewMessage() + // Create a new message + m := mail.NewMsg() + if err := m.From(from); err != nil { + return err + } - // Set the main email parts - m.SetHeader("From", from) - m.SetHeader("To", to...) - m.SetHeader("Subject", subject) + // Set recipient(s) + if err := m.To(to...); err != nil { + return err + } - // Check if the body is provided via a file + m.Subject(subject) + + // Set the body if bodyFile != "" { - m.SetBody("text/html", body) + m.SetBodyString(mail.TypeTextHTML, body) } else { - m.SetBody("text/plain", body) + m.SetBodyString(mail.TypeTextPlain, body) } // Add attachments for _, attachment := range attachments { - m.Attach(attachment) + m.AttachFile(attachment) } - // Set up SMTP information - d := mail.NewDialer(smtpServer, smtpPort, username, password) + // Create a new client + c, err := mail.NewClient(smtpServer, mail.WithPort(smtpPort), mail.WithSMTPAuth(mail.SMTPAuthPlain), + mail.WithUsername(username), mail.WithPassword(password)) + if err != nil { + return err + } + defer c.Close() // Send the email - if err := d.DialAndSend(m); err != nil { + if err := c.DialAndSend(m); err != nil { fmt.Printf("Error sending email: %v", err) return err }