|
1 | 1 | import { type User } from 'next-auth'; |
| 2 | +import { Unsend } from 'unsend'; |
2 | 3 | import { Resend } from 'resend'; |
3 | 4 | import { env } from '~/env'; |
4 | 5 |
|
5 | 6 | const resend = new Resend(env.RESEND_API_KEY); |
6 | 7 |
|
| 8 | +const unsend = new Unsend(env.UNSEND_API_KEY); |
| 9 | + |
7 | 10 | export async function sendSignUpEmail(email: string, token: string, url: string) { |
8 | 11 | const { host } = new URL(url); |
9 | 12 |
|
@@ -47,31 +50,20 @@ export async function sendFeedbackEmail(feedback: string, user: User) { |
47 | 50 |
|
48 | 51 | async function sendMail(email: string, subject: string, text: string, html: string) { |
49 | 52 | try { |
50 | | - if (env.UNSEND_API_KEY && env.UNSEND_URL) { |
51 | | - const resp = await fetch(`${env.UNSEND_URL}/emails`, { |
52 | | - method: 'POST', |
53 | | - headers: { |
54 | | - 'Content-Type': 'application/json', |
55 | | - Authorization: `Bearer ${env.UNSEND_API_KEY}`, |
56 | | - }, |
57 | | - body: JSON.stringify({ |
58 | | - from: 'hello@auth.splitpro.app', |
59 | | - to: email, |
60 | | - subject, |
61 | | - text, |
62 | | - html, |
63 | | - }), |
| 53 | + if (env.UNSEND_API_KEY) { |
| 54 | + const response = await unsend.emails.send({ |
| 55 | + from: 'hello@auth.splitpro.app', |
| 56 | + to: email, |
| 57 | + subject, |
| 58 | + text, |
| 59 | + html, |
64 | 60 | }); |
65 | 61 |
|
66 | | - if (resp.status === 200) { |
67 | | - console.log('Email sent using unsend', await resp.json()); |
| 62 | + if (response.data) { |
| 63 | + console.log('Email sent using unsend', response.data); |
68 | 64 | return; |
69 | 65 | } else { |
70 | | - console.log( |
71 | | - 'Error sending email using unsend, so fallback to resend', |
72 | | - resp.status, |
73 | | - resp.statusText, |
74 | | - ); |
| 66 | + console.log('Error sending email using unsend', response.error); |
75 | 67 | } |
76 | 68 | } |
77 | 69 | } catch (error) { |
|
0 commit comments