115
pkg/mailer/mailer.go
Normal file
115
pkg/mailer/mailer.go
Normal file
@@ -0,0 +1,115 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package mailer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/coredata"
|
||||
"github.com/jhillyerd/enmime"
|
||||
"go.gearno.de/kit/log"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
Mailer struct {
|
||||
pg *pg.Client
|
||||
l *log.Logger
|
||||
cfg Config
|
||||
}
|
||||
|
||||
Config struct {
|
||||
SenderName string
|
||||
SenderEmail string
|
||||
Addr string
|
||||
}
|
||||
)
|
||||
|
||||
func NewMailer(pg *pg.Client, l *log.Logger, cfg Config) *Mailer {
|
||||
return &Mailer{pg: pg, l: l, cfg: cfg}
|
||||
}
|
||||
|
||||
func (m *Mailer) Run(ctx context.Context) error {
|
||||
LOOP:
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(1 * time.Second):
|
||||
ctx := context.Background()
|
||||
if err := m.batchSendEmails(ctx); err != nil {
|
||||
m.l.ErrorCtx(ctx, "cannot send email", log.Error(err))
|
||||
}
|
||||
|
||||
goto LOOP
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Mailer) batchSendEmails(ctx context.Context) error {
|
||||
for {
|
||||
err := m.pg.WithTx(
|
||||
ctx,
|
||||
func(tx pg.Conn) error {
|
||||
email := &coredata.Email{}
|
||||
err := email.LoadNextUnsentForUpdate(ctx, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mail := enmime.Builder().
|
||||
Subject(email.Subject).
|
||||
From(m.cfg.SenderName, m.cfg.SenderEmail).
|
||||
To(email.RecipientName, email.RecipientEmail).
|
||||
Text([]byte(email.TextBody))
|
||||
|
||||
envelope, err := mail.Build()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot build email: %w", err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := envelope.Encode(&buf); err != nil {
|
||||
return fmt.Errorf("cannot encode email: %w", err)
|
||||
}
|
||||
|
||||
if err := smtp.SendMail(m.cfg.Addr, nil, m.cfg.SenderEmail, []string{email.RecipientEmail}, buf.Bytes()); err != nil {
|
||||
return fmt.Errorf("cannot send email: %w", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
email.SentAt = &now
|
||||
email.UpdatedAt = now
|
||||
|
||||
if err := email.Update(ctx, tx); err != nil {
|
||||
return fmt.Errorf("cannot update email: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if errors.Is(err, coredata.ErrNoUnsentEmail) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user