127 lines
3.7 KiB
Go
127 lines
3.7 KiB
Go
package email
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/mrz1836/postmark"
|
|
)
|
|
|
|
type Client struct {
|
|
server *postmark.Client
|
|
fromEmail string
|
|
baseURL string
|
|
}
|
|
|
|
func NewClient(serverToken, fromEmail, baseURL string) *Client {
|
|
var server *postmark.Client
|
|
if serverToken != "" {
|
|
server = postmark.NewClient(serverToken, "")
|
|
}
|
|
return &Client{
|
|
server: server,
|
|
fromEmail: fromEmail,
|
|
baseURL: baseURL,
|
|
}
|
|
}
|
|
|
|
func (c *Client) SendVerificationEmail(to, name, token string) error {
|
|
if c.server == nil {
|
|
return fmt.Errorf("email client not configured")
|
|
}
|
|
|
|
verifyURL := fmt.Sprintf("%s/verify-email?token=%s", c.baseURL, token)
|
|
htmlBody := renderVerificationEmail(name, verifyURL)
|
|
textBody := fmt.Sprintf("Hi %s,\n\nPlease verify your email by visiting: %s\n\nThis link expires in 24 hours.", name, verifyURL)
|
|
|
|
_, err := c.server.SendEmail(context.Background(), postmark.Email{
|
|
From: c.fromEmail,
|
|
To: to,
|
|
Subject: "Verify your email address",
|
|
HTMLBody: htmlBody,
|
|
TextBody: textBody,
|
|
Tag: "verification",
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (c *Client) SendPasswordResetEmail(to, name, token string) error {
|
|
if c.server == nil {
|
|
return fmt.Errorf("email client not configured")
|
|
}
|
|
|
|
resetURL := fmt.Sprintf("%s/reset-password?token=%s", c.baseURL, token)
|
|
htmlBody := renderPasswordResetEmail(name, resetURL)
|
|
textBody := fmt.Sprintf("Hi %s,\n\nReset your password by visiting: %s\n\nThis link expires in 1 hour. If you didn't request this, please ignore this email.", name, resetURL)
|
|
|
|
_, err := c.server.SendEmail(context.Background(), postmark.Email{
|
|
From: c.fromEmail,
|
|
To: to,
|
|
Subject: "Reset your password",
|
|
HTMLBody: htmlBody,
|
|
TextBody: textBody,
|
|
Tag: "password-reset",
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (c *Client) SendTicketClosedNotification(to, name, ticketTitle, ticketID string) error {
|
|
if c.server == nil {
|
|
return fmt.Errorf("email client not configured")
|
|
}
|
|
|
|
ticketURL := fmt.Sprintf("%s/tickets/%s", c.baseURL, ticketID)
|
|
htmlBody := renderTicketClosedEmail(name, ticketTitle, ticketURL)
|
|
textBody := fmt.Sprintf("Hi %s,\n\nYour ticket \"%s\" has been resolved.\n\nView it at: %s", name, ticketTitle, ticketURL)
|
|
|
|
_, err := c.server.SendEmail(context.Background(), postmark.Email{
|
|
From: c.fromEmail,
|
|
To: to,
|
|
Subject: fmt.Sprintf("Your ticket \"%s\" has been resolved", ticketTitle),
|
|
HTMLBody: htmlBody,
|
|
TextBody: textBody,
|
|
Tag: "ticket-closed",
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (c *Client) SendAccountApprovedEmail(to, name string) error {
|
|
if c.server == nil {
|
|
return fmt.Errorf("email client not configured")
|
|
}
|
|
|
|
loginURL := fmt.Sprintf("%s/login", c.baseURL)
|
|
htmlBody := renderAccountApprovedEmail(name, loginURL)
|
|
textBody := fmt.Sprintf("Hi %s,\n\nYour account has been approved! You can now log in at %s.", name, loginURL)
|
|
|
|
_, err := c.server.SendEmail(context.Background(), postmark.Email{
|
|
From: c.fromEmail,
|
|
To: to,
|
|
Subject: "Your account has been approved",
|
|
HTMLBody: htmlBody,
|
|
TextBody: textBody,
|
|
Tag: "account-approved",
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (c *Client) SendWelcomeEmail(to, name, tempPassword string) error {
|
|
if c.server == nil {
|
|
return fmt.Errorf("email client not configured")
|
|
}
|
|
|
|
loginURL := fmt.Sprintf("%s/login", c.baseURL)
|
|
htmlBody := renderWelcomeEmail(name, to, tempPassword, loginURL)
|
|
textBody := fmt.Sprintf("Hi %s,\n\nAn account has been created for you.\n\nEmail: %s\nTemporary Password: %s\n\nPlease log in at %s and change your password.", name, to, tempPassword, loginURL)
|
|
|
|
_, err := c.server.SendEmail(context.Background(), postmark.Email{
|
|
From: c.fromEmail,
|
|
To: to,
|
|
Subject: "Welcome - Your account has been created",
|
|
HTMLBody: htmlBody,
|
|
TextBody: textBody,
|
|
Tag: "welcome",
|
|
})
|
|
return err
|
|
}
|