|
1 | | -/** |
2 | | - * Email service for sponsor communications. |
3 | | - * |
4 | | - * STUBBED — needs RESEND_API_KEY |
5 | | - * TODO: Wire up Resend when API key is available |
6 | | - */ |
| 1 | +import { Resend } from 'resend' |
7 | 2 |
|
8 | | -export interface EmailResult { |
9 | | - success: boolean |
10 | | - messageId?: string |
11 | | -} |
| 3 | +const resend = new Resend(process.env.RESEND_API_KEY) |
| 4 | + |
| 5 | +const FROM_EMAIL = 'Alex Patterson <alex@codingcat.dev>' |
12 | 6 |
|
13 | 7 | /** |
14 | | - * Send an email to a sponsor contact. |
15 | | - * Currently stubbed — logs the email and returns success. |
| 8 | + * Send a sponsor-related email via Resend. |
| 9 | + * Falls back to console logging if RESEND_API_KEY is not set. |
16 | 10 | */ |
17 | 11 | export async function sendSponsorEmail( |
18 | 12 | to: string, |
19 | 13 | subject: string, |
20 | 14 | body: string |
21 | | -): Promise<EmailResult> { |
22 | | - // TODO: Wire up Resend when RESEND_API_KEY is available |
23 | | - // import { Resend } from 'resend' |
24 | | - // const resend = new Resend(process.env.RESEND_API_KEY) |
25 | | - // const { data, error } = await resend.emails.send({ |
26 | | - // from: 'Alex Patterson <alex@codingcat.dev>', |
27 | | - // to, |
28 | | - // subject, |
29 | | - // text: body, |
30 | | - // }) |
| 15 | +): Promise<{ success: boolean; messageId?: string }> { |
| 16 | + if (!process.env.RESEND_API_KEY) { |
| 17 | + console.warn('[SPONSOR] RESEND_API_KEY not set — logging email instead') |
| 18 | + console.log('[SPONSOR] Email:', { to, subject, body: body.slice(0, 200) + '...' }) |
| 19 | + return { success: true } |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + const { data, error } = await resend.emails.send({ |
| 24 | + from: FROM_EMAIL, |
| 25 | + to: [to], |
| 26 | + subject, |
| 27 | + text: body, |
| 28 | + }) |
31 | 29 |
|
32 | | - console.log('[SPONSOR] Email send (stubbed):', { |
33 | | - to, |
34 | | - subject, |
35 | | - bodyLength: body.length, |
36 | | - timestamp: new Date().toISOString(), |
37 | | - }) |
| 30 | + if (error) { |
| 31 | + console.error('[SPONSOR] Resend error:', error) |
| 32 | + return { success: false } |
| 33 | + } |
38 | 34 |
|
39 | | - return { |
40 | | - success: true, |
41 | | - messageId: `stub_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`, |
| 35 | + console.log('[SPONSOR] Email sent via Resend:', { to, subject, messageId: data?.id }) |
| 36 | + return { success: true, messageId: data?.id } |
| 37 | + } catch (error) { |
| 38 | + console.error('[SPONSOR] Failed to send email:', error) |
| 39 | + return { success: false } |
42 | 40 | } |
43 | 41 | } |
0 commit comments