all repos

onasty @ 943fcdb

a one-time notes service

onasty/internal/service/usersrv/email.go(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package usersrv

import (
	"context"
	"errors"
	"fmt"
	"log/slog"
)

var ErrFailedToSendVerifcationEmail = errors.New("failed to send verification email")

const (
	verificationEmailSubject = "Onasty: verifiy your email"
	verificationEmailBody    = `To verify your email, please follow this link:
<a href="%[1]s/api/v1/auth/verify/%[2]s">%[1]s/api/v1/auth/verify/%[2]s</a>
<br />
<br />
This link will expire after 24 hours.`
)

func (u *UserSrv) sendVerificationEmail(
	ctx context.Context,
	cancel context.CancelFunc,
	userEmail string,
	token string,
	url string,
) error {
	select {
	case <-ctx.Done():
		slog.ErrorContext(ctx, "failed to send verfication email", "err", ctx.Err())
		return ErrFailedToSendVerifcationEmail
	default:
		if err := u.mailer.Send(
			ctx,
			userEmail,
			verificationEmailSubject,
			fmt.Sprintf(verificationEmailBody, url, token),
		); err != nil {
			return errors.Join(ErrFailedToSendVerifcationEmail, err)
		}
		cancel()
	}

	return nil
}