package mailer import ( "bytes" "embed" "html/template" "log" // "time" "github.com/wneessen/go-mail" ) //go:embed "templates" var templateFS embed.FS type Mailer struct { dialer *mail.Client sender string } func New(host string, port int, username, password, sender string) Mailer { // Initialize a new mail.Dialer instance with the given SMTP server settings. We // also configure this to use a 5-second timeout whenever we send an email. dialer, err := mail.NewClient(host, mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover), mail.WithTLSPortPolicy(mail.TLSMandatory), mail.WithUsername(username), mail.WithPassword(password), ) if err != nil { log.Fatalf("failed to deliver mail: %s", err) } // dialer. = 5 * time.Second // Return a Mailer instance containing the dialer and sender information. return Mailer{ dialer: dialer, sender: sender, } } // Define a Send() method on the Mailer type. This takes the recipient email address // as the first parameter, the name of the file containing the templates, and any // dynamic data for the templates as an interface{} parameter. func (m Mailer) Send(recipient, templateFile string, data interface{}) error { // Use the ParseFS() method to parse the required template file from the embedded // file system. tmpl, err := template.New("email").ParseFS(templateFS, "templates/"+templateFile) if err != nil { return err } // Execute the named template "subject", passing in the dynamic data and storing the // result in a bytes.Buffer variable. subject := new(bytes.Buffer) err = tmpl.ExecuteTemplate(subject, "subject", data) if err != nil { return err } // Follow the same pattern to execute the "plainBody" template and store the result // in the plainBody variable. plainBody := new(bytes.Buffer) err = tmpl.ExecuteTemplate(plainBody, "plainBody", data) if err != nil { return err } // And likewise with the "htmlBody" template. htmlBody := new(bytes.Buffer) err = tmpl.ExecuteTemplate(htmlBody, "htmlBody", data) if err != nil { return err } // Use the mail.NewMessage() function to initialize a new mail.Message instance. // Then we use the SetHeader() method to set the email recipient, sender and subject // headers, the SetBody() method to set the plain-text body, and the AddAlternative() // method to set the HTML body. It's important to note that AddAlternative() should // always be called *after* SetBody(). msg := mail.NewMsg() msg.SetAddrHeader("To", recipient) msg.SetAddrHeader("From", m.sender) msg.SetGenHeader("Subject", subject.String()) msg.SetBodyString("text/plain", plainBody.String()) msg.AddAlternativeString("text/html", htmlBody.String()) // Call the DialAndSend() method on the dialer, passing in the message to send. This // opens a connection to the SMTP server, sends the message, then closes the // connection. If there is a timeout, it will return a "dial tcp: i/o timeout" // error. err = m.dialer.DialAndSend(msg) if err != nil { return err } return nil }