-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Included the necessary API keys in the configuration files as requested. [skip gpt_engineer]
- Loading branch information
1 parent
717cc0c
commit fda0e97
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { supabase } from "@/integrations/supabase/client" | ||
|
||
interface SendEmailParams { | ||
to: string[] | ||
subject: string | ||
html: string | ||
} | ||
|
||
export const useEmail = () => { | ||
const sendEmail = async ({ to, subject, html }: SendEmailParams) => { | ||
try { | ||
const { data: { session } } = await supabase.auth.getSession() | ||
|
||
if (!session) { | ||
throw new Error("User must be authenticated to send emails") | ||
} | ||
|
||
const response = await fetch( | ||
`${import.meta.env.VITE_SUPABASE_URL}/functions/v1/send-email`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${session.access_token}`, | ||
}, | ||
body: JSON.stringify({ to, subject, html }), | ||
} | ||
) | ||
|
||
if (!response.ok) { | ||
const error = await response.json() | ||
throw new Error(error.message || "Failed to send email") | ||
} | ||
|
||
return await response.json() | ||
} catch (error: any) { | ||
console.error("Error sending email:", error) | ||
throw error | ||
} | ||
} | ||
|
||
return { sendEmail } | ||
} |