-
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.
* add schotsl/mailgun * remove pragma * fmt * check response * info@mg.swanfactory.online * exclude node_modules * only build manifest? --------- Co-authored-by: Dr. Ernie Prabhakar <19791+drernie@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
113 additions
and
6 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
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
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
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,41 @@ | ||
import { useState } from "preact/hooks"; | ||
|
||
export default function SendEmailButton() { | ||
const [status, setStatus] = useState<string | null>(null); | ||
|
||
const handleClick = async () => { | ||
setStatus("Sending..."); | ||
try { | ||
const response = await fetch("/api/send-email", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
to: "ernest@drernie.com", | ||
subject: "Hello from Deno Fresh", | ||
text: "This is a test email sent from a Deno Fresh application.", | ||
}), | ||
}); | ||
|
||
if (response.ok) { | ||
setStatus("Email sent successfully!"); | ||
} else { | ||
setStatus("Failed to send email."); | ||
} | ||
} catch (error) { | ||
console.error("Error sending email:", error); | ||
setStatus("An error occurred."); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={handleClick} | ||
class="px-4 py-2 bg-blue-500 text-white rounded" | ||
> | ||
Send Email | ||
</button> | ||
{status && <p>{status}</p>} | ||
</div> | ||
); | ||
} |
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,14 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { sendEmail } from "../../utils/mail.ts"; | ||
|
||
export const handler: Handlers = { | ||
async POST(req) { | ||
const { to, subject, text } = await req.json(); | ||
const status = await sendEmail(to, subject, text); | ||
if (status < 300) { | ||
return new Response("Email sent successfully", { status: status }); | ||
} else { | ||
return new Response("Failed to send email", { status: status }); | ||
} | ||
}, | ||
}; |
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
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,40 @@ | ||
import Mailgun from "@schotsl/mailgun"; | ||
|
||
const apiKey = Deno.env.get("MAILGUN_API_KEY"); | ||
const domain = Deno.env.get("MAILGUN_DOMAIN"); | ||
|
||
if (!apiKey || !domain) { | ||
throw new Error( | ||
"MAILGUN_API_KEY and MAILGUN_DOMAIN environment variables must be set", | ||
); | ||
} | ||
|
||
console.log("Mailgun API key:", apiKey); | ||
console.log("Mailgun domain:", domain); | ||
|
||
const mailgun = new Mailgun({ | ||
key: apiKey, | ||
domain: domain, | ||
region: "us", // or "eu" based on your Mailgun account | ||
}); | ||
|
||
console.log("Mailgun client:", mailgun); | ||
|
||
export async function sendEmail( | ||
to: string, | ||
subject: string, | ||
text: string, | ||
from: string = "info@mg.swanfactory.online", | ||
) { | ||
console.log(`Sending email to ${to}...`); | ||
const response = await mailgun.send({ to, from, subject, text }); | ||
if (response.ok) { | ||
console.log(`Email sent to ${to}`, response); | ||
} else { | ||
console.error( | ||
`Error[${response.status}]: ${response.statusText}`, | ||
response, | ||
); | ||
} | ||
return response.status; | ||
} |