all repos

onasty @ 87e5f907f15cd68fab95e58ea44392d4cbcde839

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
refactor: deal with TODOs and typos (#30)..., 1 year ago
1
package usersrv
2
3
import (
4
	"context"
5
	"errors"
6
	"fmt"
7
	"log/slog"
8
)
9
10
var ErrFailedToSendVerifcationEmail = errors.New("failed to send verification email")
11
12
const (
13
	verificationEmailSubject = "Onasty: verify your email"
14
	verificationEmailBody    = `To verify your email, please follow this link:
15
<a href="%[1]s/api/v1/auth/verify/%[2]s">%[1]s/api/v1/auth/verify/%[2]s</a>
16
<br />
17
<br />
18
This link will expire after 24 hours.`
19
)
20
21
func (u *UserSrv) sendVerificationEmail(
22
	ctx context.Context,
23
	cancel context.CancelFunc,
24
	userEmail string,
25
	token string,
26
	url string,
27
) {
28
	select {
29
	case <-ctx.Done():
30
		slog.ErrorContext(ctx, "failed to send verification email", "err", ctx.Err())
31
	default:
32
		if err := u.mailer.Send(
33
			ctx,
34
			userEmail,
35
			verificationEmailSubject,
36
			fmt.Sprintf(verificationEmailBody, url, token),
37
		); err != nil {
38
			slog.ErrorContext(ctx, "failed to send verification email", "err", err)
39
		}
40
		cancel()
41
	}
42
}