termbox/internal/email/service.go

51 lines
1.2 KiB
Go

package email
import (
"context"
"fmt"
"os"
"github.com/resend/resend-go/v2"
)
type Service struct {
client *resend.Client
}
func NewService() *Service {
apiKey := os.Getenv("RESEND_API_KEY")
if apiKey == "" {
panic("RESEND_API_KEY environment variable is required")
}
client := resend.NewClient(apiKey)
return &Service{client: client}
}
func (s *Service) SendVerificationCode(ctx context.Context, email, username, code string) error {
params := &resend.SendEmailRequest{
From: "noreply@termbox.keircn.com",
To: []string{email},
Subject: "Verify your Termbox account",
Html: fmt.Sprintf(`
<h2>Welcome to Termbox, %s!</h2>
<p>Please verify your account with the following code:</p>
<h1 style="font-family: monospace; letter-spacing: 5px; color: #2563eb;">%s</h1>
<p>This code will expire in 10 minutes.</p>
<p>If you didn't create an account, please ignore this email.</p>
`, username, code),
Text: fmt.Sprintf(`
Welcome to Termbox, %s!
Please verify your account with the following code: %s
This code will expire in 10 minutes.
If you didn't create an account, please ignore this email.
`, username, code),
}
_, err := s.client.Emails.Send(params)
return err
}