-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresend.ts
More file actions
131 lines (115 loc) · 3.34 KB
/
resend.ts
File metadata and controls
131 lines (115 loc) · 3.34 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Resend } from 'resend';
import {
getNewPostEmailHTML,
getUnsubscribeEmailHTML,
getVerificationEmailHTML,
} from './templates';
if (!process.env.RESEND_API_KEY) {
throw new Error('RESEND_API_KEY is not defined in environment variables');
}
const resend = new Resend(process.env.RESEND_API_KEY);
const FROM_EMAIL = process.env.EMAIL_FROM || '';
const BASE_URL =
process.env.NEXT_PUBLIC_DEPLOYMENT_URL ||
process.env.NEXT_PUBLIC_URL ||
'https://shipfriend.dev';
export async function sendVerificationEmail(
email: string,
nickname: string,
verificationToken: string
): Promise<{ success: boolean; error?: string }> {
try {
const verifyUrl = `${BASE_URL}/api/subscribe/verify?token=${verificationToken}`;
const html = getVerificationEmailHTML(nickname, verifyUrl);
const { data, error } = await resend.emails.send({
from: FROM_EMAIL,
to: email,
subject: '블로그 구독 인증을 완료해주세요',
html,
});
if (error) {
console.error('Failed to send verification email:', error);
return { success: false, error: error.message };
}
console.log('Verification email sent successfully:', data);
return { success: true };
} catch (error) {
console.error('Unexpected error sending verification email:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}
export async function sendNewPostEmail(
subscriber: {
email: string;
nickname: string;
unsubscribeToken: string;
},
post: {
title: string;
subTitle?: string;
slug: string;
thumbnailImage?: string;
}
): Promise<{ success: boolean; error?: string }> {
try {
const postUrl = `${BASE_URL}/posts/${post.slug}`;
const unsubscribeUrl = `${BASE_URL}/api/subscribe/unsubscribe?token=${subscriber.unsubscribeToken}`;
const html = getNewPostEmailHTML(
subscriber.nickname,
post,
postUrl,
unsubscribeUrl
);
const { data, error } = await resend.emails.send({
from: FROM_EMAIL,
to: subscriber.email,
subject: `새 글: ${post.title}`,
html,
});
if (error) {
console.error(
`Failed to send new post email to ${subscriber.email}:`,
error
);
return { success: false, error: error.message };
}
return { success: true };
} catch (error) {
console.error(
`Unexpected error sending new post email to ${subscriber.email}:`,
error
);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}
export async function sendUnsubscribeConfirmation(
email: string,
nickname: string
): Promise<{ success: boolean; error?: string }> {
try {
const html = getUnsubscribeEmailHTML(nickname);
const { data, error } = await resend.emails.send({
from: FROM_EMAIL,
to: email,
subject: '구독이 취소되었습니다',
html,
});
if (error) {
console.error('Failed to send unsubscribe confirmation:', error);
return { success: false, error: error.message };
}
return { success: true };
} catch (error) {
console.error('Unexpected error sending unsubscribe confirmation:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}