12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package smtp
- import (
- "bytes"
- "embed"
- "fmt"
- "html/template"
- "net/smtp"
- "git.linuxforward.com/byom/byom-core/config"
- "github.com/sirupsen/logrus"
- )
- type Service struct {
- client *smtp.Client
- template *template.Template
- logger *logrus.Entry
- }
- //go:embed templates/invite.html
- var inviteTpl embed.FS
- type EmailData struct {
- Token string
- WorkspaceName string
- URL string
- }
- func NewService(cnf *config.Smtp) *Service {
- logger := logrus.WithField("core", "email")
- //mock the smtp client
- client := &smtp.Client{}
- //parse the email template
- tmpl, err := template.ParseFS(inviteTpl, "templates/invite.html")
- if err != nil {
- logger.WithError(err).Error("failed to parse email template")
- panic(err)
- }
- return &Service{
- logger: logger,
- client: client,
- template: tmpl,
- }
- }
- func (s *Service) SendEmail(to, subject, body string) error {
- s.logger.WithField("to", to).WithField("subject", subject).Info("sending email")
- //since we are not actually sending emails, we will just log the email
- s.logger.WithField("body", body).Info("email body")
- return nil
- }
- func (s *Service) SendInviteEmail(to, token, workspace string) error {
- var body bytes.Buffer
- data := EmailData{
- Token: token,
- WorkspaceName: workspace,
- URL: fmt.Sprintf("https://%s.domain.com/invite/%s", workspace, token),
- }
- if err := s.template.Execute(&body, data); err != nil {
- return fmt.Errorf("template execution failed: %w", err)
- }
- return s.SendEmail(to, "Join "+workspace+" workspace", body.String())
- }
- func (s *Service) Close() error {
- if s.client != nil {
- err := s.client.Close()
- if err != nil {
- s.logger.WithError(err).Error("failed to close SMTP client")
- return fmt.Errorf("close SMTP client: %w", err)
- }
- s.logger.Info("SMTP client closed successfully")
- }
- s.template = nil
- return nil
- }
|