The mailer module provides a convenient way to send emails using Go. It includes functionality for validating email data and sending emails with SMTP authentication.
- Simple and intuitive API for sending emails.
- Support for email authentication configuration.
- Templating support for email content customization.
To use the mailer module in your Go project, you can simply import it using:
import "github.com/vidarlx/mailer"
To create a new mailer instance, use the NewMailer function:
authConfig := mailer.EmailAuthConfig{
Host: "smtp.example.com",
Port: 587,
Username: "user@example.com",
Password: "password",
From: "user@example.com",
}
templateDir := "templates"
mailService := mailer.NewMailer(authConfig, templateDir)
To send an email, create an Email struct with the necessary data and pass it to the SendEmail method of the mailer instance:
email := &mailer.Email{
Recipients: []string{"recipient@example.com"},
Subject: "Test Subject",
Body: "Test Body",
TemplateFileName: "test_template.tmpl",
Data: nil, // Assuming no data for simplicity
}
err := mailService.SendEmail(email)
if err != nil {
// Handle error
}
You can use templates to customize the content of your emails. Simply provide the template file name and data to be parsed with the template:
email := &mailer.Email{
Recipients: []string{"recipient@example.com"},
Subject: "Test Subject",
TemplateFileName: "template.tmpl",
Data: map[string]interface{}{
"Name": "John Doe",
},
}
The mailer module provides validation for email data to ensure required fields are present:
email := &mailer.Email{
Recipients: []string{},
Subject: "Test Subject",
Body: "Test Body",
TemplateFileName: "test_template.tmpl",
Data: nil,
}
err := mailer.ValidateEmail(email)
if err != nil {
// Handle validation error
}
License
This module is released under the MIT License. Feel free to use and modify it according to your needs. I don't care.