feat: send mail

This commit is contained in:
2025-08-24 13:11:31 +02:00
parent 5b664234e9
commit 0f3da902dc
8 changed files with 144 additions and 17 deletions

15
mailer/model.go Normal file
View File

@@ -0,0 +1,15 @@
package mailer
import "io"
type Mail struct {
TO string
Subject string
Body string
Attachments []Attachment
}
type Attachment struct {
Name string
MimeType string
Content io.Reader
}

52
mailer/resource.go Normal file
View File

@@ -0,0 +1,52 @@
package mailer
import (
"crypto/tls"
"net/smtp"
"github.com/jordan-wright/email"
)
type Config struct {
SMTP SMTPConfig
From string
}
type SMTPConfig struct {
Host string
Port string
User string
Password string
}
type Service struct {
from string
pool *email.Pool
cfg Config
}
func New(cfg Config) (s *Service, err error) {
p, err := email.NewPool(
cfg.SMTP.Host+":"+cfg.SMTP.Port,
1,
smtp.PlainAuth(
"",
cfg.SMTP.User,
cfg.SMTP.Password,
cfg.SMTP.Host,
), &tls.Config{
InsecureSkipVerify: true, // set false in production
ServerName: cfg.SMTP.Host, // must match the SMTP host
})
if err != nil {
return
}
s = &Service{
from: cfg.From,
pool: p,
cfg: cfg,
}
return
}

33
mailer/send.go Normal file
View File

@@ -0,0 +1,33 @@
package mailer
import (
"crypto/tls"
"net/smtp"
"github.com/jordan-wright/email"
)
func (s Service) Send(m Mail) (err error) {
e := email.NewEmail()
e.To = []string{m.TO}
e.Bcc = []string{"niklas@sunway.ch"}
e.From = s.from
e.Subject = m.Subject
e.Text = []byte(m.Body)
for _, a := range m.Attachments {
e.Attach(a.Content, a.Name, a.MimeType)
}
err = e.SendWithTLS(s.cfg.SMTP.Host+":"+s.cfg.SMTP.Port, smtp.PlainAuth(
s.cfg.SMTP.User,
s.cfg.SMTP.User,
s.cfg.SMTP.Password,
s.cfg.SMTP.Host,
), &tls.Config{
ServerName: s.cfg.SMTP.Host,
})
// err = s.pool.Send(e, time.Second*5)
return
}