diff --git a/error.go b/errors/error.go similarity index 97% rename from error.go rename to errors/error.go index d7d937d..a892df2 100644 --- a/error.go +++ b/errors/error.go @@ -1,4 +1,4 @@ -package gomail +package errors import "errors" diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..9776089 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,10 @@ +module gomailtesting + +go 1.22.1 + +require ( + github.com/Rhaqim/gomail v1.0.0-beta +) + +// add module from src folder +replace github.com/Rhaqim/gomail => ../ \ No newline at end of file diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..f1eb52a --- /dev/null +++ b/example/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "log" + + "github.com/Rhaqim/gomail" +) + +func main() { + auth := gomail.EmailAuthConfig{ + Host: "smtp.gmail.com", + Port: 587, + Username: "user", + Password: "password", + From: "me@gmail.com", + } + + var g gomail.Gomail = gomail.NewGomail(auth, "example/templates") + + App(g) + +} + +func App(mail gomail.Gomail) { + + email := &gomail.Email{ + Recipients: []string{"recipient1@gmail.com", "recipient2@gmail.com"}, + Subject: "Hello", + Body: "Hello, this is a test email", + TemplateFileName: "hello.html", + } + + err := mail.SendEmail(email) + if err != nil { + log.Fatal(err) + } +} diff --git a/example/templates/hello.html b/example/templates/hello.html new file mode 100644 index 0000000..1ae7b4c --- /dev/null +++ b/example/templates/hello.html @@ -0,0 +1,10 @@ + + + + My Go Template + + +

{{.Title }}

+

{{ .Body }}

+ + \ No newline at end of file diff --git a/gomail.go b/gomail.go index 0cfde40..9d9a83f 100644 --- a/gomail.go +++ b/gomail.go @@ -6,6 +6,8 @@ import ( "net/smtp" "sync" "text/template" + + "github.com/Rhaqim/gomail/errors" ) // Goemail is an interface for sending emails and parsing templates. @@ -39,31 +41,31 @@ func (g *GoemailConfig) validate(kind ValidateKind) error { switch kind { case auth: if g.Config.Host == "" { - return ErrEmptyHost + return errors.ErrEmptyHost } if g.Config.Port == 0 { - return ErrEmptyPort + return errors.ErrEmptyPort } if g.Config.Username == "" { - return ErrEmptyUsername + return errors.ErrEmptyUsername } if g.Config.Password == "" { - return ErrEmptyPassword + return errors.ErrEmptyPassword } if g.Config.From == "" { - return ErrEmptyFrom + return errors.ErrEmptyFrom } if g.TemplateDir == "" { - return ErrEmptyTemplateDir + return errors.ErrEmptyTemplateDir } case email: if len(g.email.Recipients) == 0 { - return ErrEmptyTo + return errors.ErrEmptyTo } }