Skip to content

Commit

Permalink
Replaced gopkg.in/mail.v2 by wneessen/go-mail
Browse files Browse the repository at this point in the history
  • Loading branch information
JCoupalK committed Dec 25, 2023
1 parent bec8123 commit 598535f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
37 changes: 24 additions & 13 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 598535f

Please sign in to comment.