Documentation
¶
Overview ¶
Package email provides email sending functionality for the application, including welcome and verification emails with HTML templates.
Package email provides email template management and rendering functionality.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EmailSender ¶
type EmailSender interface { // SendWelcomeEmail sends a welcome email to new users SendWelcomeEmail(to, username string) error // SendVerificationEmail sends an email verification link SendVerificationEmail(to, username, token string) error SendPasswordResetEmail(email, resetLins string) error }
EmailSender defines the interface for sending various types of emails. This interface allows for easy mocking in tests and flexibility in implementation.
func NewMockEmailService ¶
func NewMockEmailService() EmailSender
type EmailService ¶
type EmailService struct {
// contains filtered or unexported fields
}
EmailService implements the EmailSender interface and handles email sending operations using SMTP.
func NewEmailService ¶
func NewEmailService(host string, port int, username, from, fromName, password, baseURL string) (*EmailService, error)
NewEmailService creates a new email service instance with the provided configuration.
Parameters:
- host: SMTP server hostname
- port: SMTP server port
- username: SMTP authentication username
- password: SMTP authentication password
- baseURL: Base URL for application links in emails
Returns:
- *EmailService: Configured email service
- error: Any error during template initialization
func (*EmailService) SendPasswordResetEmail ¶
func (s *EmailService) SendPasswordResetEmail(to, resetLink string) error
func (*EmailService) SendVerificationEmail ¶
func (s *EmailService) SendVerificationEmail(to, username, token string) error
SendVerificationEmail sends an email with a verification link to a user.
Parameters:
- to: Recipient email address
- username: Recipient's username
- token: Verification token
Returns:
- error: Any error encountered during email sending
Template Data:
- Username: User's display name
- VerificationLink: Complete verification URL with token
- Year: Current year for copyright
func (*EmailService) SendWelcomeEmail ¶
func (s *EmailService) SendWelcomeEmail(to, username string) error
SendWelcomeEmail sends a welcome email to a newly registered user.
Parameters:
- to: Recipient email address
- username: Recipient's username
Returns:
- error: Any error encountered during email sending
Template Data:
- Username: User's display name
- LoginURL: URL to the login page
- Year: Current year for copyright
type EmailTemplate ¶
type EmailTemplate struct {
// contains filtered or unexported fields
}
EmailTemplate manages HTML email templates for the application. It wraps the standard template.Template to provide email-specific template execution and management.
func NewEmailTemplate ¶
func NewEmailTemplate() (*EmailTemplate, error)
NewEmailTemplate initializes a new email template manager by loading all HTML templates from the templates/email directory.
The function expects templates to be located in "templates/email/*.html" and will parse all files matching this pattern.
Returns:
- *EmailTemplate: Template manager instance
- error: Any error encountered during template parsing
Example Usage:
templates, err := NewEmailTemplate() if err != nil { return fmt.Errorf("failed to initialize email templates: %w", err) }
Note: Templates must be valid HTML and follow Go template syntax.
func (*EmailTemplate) ExecuteTemplate ¶
func (et *EmailTemplate) ExecuteTemplate(name string, data interface{}) (string, error)
ExecuteTemplate renders a specific template with the provided data.
Parameters:
- name: Name of the template to execute (e.g., "welcome.html")
- data: Data to be passed to the template
Returns:
- string: The rendered template as a string
- error: Any error encountered during template execution
Example Usage:
data := WelcomeEmailData{ Username: "John", LoginURL: "http://example.com/login", Year: 2024, } body, err := templates.ExecuteTemplate("welcome.html", data)
Note: The data parameter must match the structure expected by the template.
type MockEmailService ¶
type MockEmailService struct{}
func (*MockEmailService) SendPasswordResetEmail ¶
func (s *MockEmailService) SendPasswordResetEmail(email, resetLins string) error
func (*MockEmailService) SendVerificationEmail ¶
func (s *MockEmailService) SendVerificationEmail(to, username, token string) error
func (*MockEmailService) SendWelcomeEmail ¶
func (s *MockEmailService) SendWelcomeEmail(to, username string) error
type PasswordResetEmailData ¶
type VerificationEmailData ¶
type VerificationEmailData struct { Username string VerificationLink string Year int CurrentTime string }
VerificationEmailData contains the data needed for the verification email template.
type WelcomeEmailData ¶
type WelcomeEmailData struct { Username string // User's display name LoginURL string // URL to the login page Year int // Current year for copyright notice }
WelcomeEmailData contains the data needed for the welcome email template.