Simple (but useful) email sender written in pure Go v1.17
. Yes, yet another email package here! 😅
Support HTML templates and attachments.
Method signature:
func (s *Sender) SendHTMLEmail(
templatePath string,
to []string,
cc []string,
subject string,
data interface{},
files []string,
) error
Example:
// Create a new struct for the email data.
type HTMLEmailData struct {
Name string
Website string
}
// Create a new SMTP sender instance with your auth params.
sender := NewEmailSender("mail@test.com", "secret", "smtp.test.com", 25)
// Send the email with an HTML template.
if err := sender.SendHTMLEmail(
"my/templates/welcome.html", // path to the HTML template
[]string{
"mail@example.com", // slice of emails to send
},
[]string{
"copy-mail@example.com", // slice of emails to send message copy
},
"It's a test email!", // subject of the email
&HTMLEmailData{
Name: "Vic",
Website: "https://shostak.dev/",
},
[]string{
"my/files/image.jpg", // slice of files to send
},
); err != nil {
// Throw error message, if something went wrong.
return fmt.Errorf("Something went wrong: %v", err)
}
Method signature:
func (s *Sender) SendPlainEmail(
to []string,
cc []string,
subject string,
data interface{},
files []string,
) error
Example:
// Create a new SMTP sender instance with your auth params.
sender := NewEmailSender("mail@test.com", "secret", "smtp.test.com", 25)
// Send the email with a plain text.
if err := sender.SendPlainEmail(
[]string{
"mail@example.com", // slice of emails to send
},
[]string{
"copy-mail@example.com", // slice of emails to send message copy
},
"It's a test email!", // subject of the email
"Here is a plain text body.", // body of the email
[]string{
"my/files/image.jpg", // slice of files to send
},
); err != nil {
// Throw error message, if something went wrong.
return fmt.Errorf("Something went wrong: %v", err)
}
Apache-2.0 © Vic Shóstak & True web artisans.