-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an admin form for sending emails.
- Loading branch information
Showing
8 changed files
with
116 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -2,13 +2,20 @@ BACKEND_URL="http://127.0.0.1:7431" | |
BACKEND_SECRET="temporarydevelopmentkey" | ||
INSTANCE_SUBSPACE_SECRET=gqvkucapoouch6xhhnn3pnqu7zpwpd3a4nctz2vkm2qrsnfbf6ha | ||
RAUTHY_URL="http://localhost:8921" | ||
|
||
# SMTP for rauthy | ||
SMTP_HOST="localhost" | ||
SMTP_PORT="2525" | ||
SMTP_FROM="[email protected]" | ||
SMTP_SECURE="false" | ||
SMTP_USER="dummy" | ||
SMTP_PASS="dummy" | ||
|
||
# SMTP for Weird | ||
SMTP_URL="smtp://dummy:dummy@localhost:2525/?pool=true" | ||
|
||
# SMTP for Weird & Rauthy | ||
SMTP_FROM="[email protected]" | ||
|
||
DISCORD_CLIENT_ID= | ||
DISCORD_TOKEN= | ||
|
||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
import { env } from '$env/dynamic/private'; | ||
import nodemailer from 'nodemailer'; | ||
|
||
export const emailer = nodemailer.createTransport( | ||
{ | ||
url: env.SMTP_URL | ||
}, | ||
{ from: env.SMTP_FROM } | ||
); |
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
48 changes: 48 additions & 0 deletions
48
src/routes/(internal)/__internal__/admin/send-emails/+page.server.ts
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,48 @@ | ||
import { usernames } from '$lib/usernames/index'; | ||
import type { ServerLoad } from '@sveltejs/kit'; | ||
import type { Actions } from './$types'; | ||
import { marked } from 'marked'; | ||
import { emailer } from '$lib/email'; | ||
import { listRauthyUsers } from '$lib/rauthy/server'; | ||
|
||
export const load: ServerLoad = async ({}) => {}; | ||
|
||
export const actions = { | ||
sendEmail: async ({ request, fetch }) => { | ||
const formData = await request.formData(); | ||
const rawRecipient = formData.get('recipient')?.toString(); | ||
const subject = formData.get('subject')?.toString(); | ||
const bodyMarkdown = formData.get('bodyMarkdown')?.toString(); | ||
if (!(subject && bodyMarkdown && rawRecipient)) return { error: 'You must fill in all fields' }; | ||
|
||
const bodyHtml = await marked.parse(bodyMarkdown); | ||
|
||
let recipients = []; | ||
if (rawRecipient == '___everyone___') { | ||
// TODO: should we send to all rauthy-registered users, not just ones that have registered a username? | ||
// | ||
// We need to double-check the consequences of this, though, because a user that deletes their | ||
// profile probably doesn't want to be emailed by us, and if they delete their profile, their | ||
// rauthy account still exists. | ||
for (const user of await listRauthyUsers(fetch, request)) { | ||
const username = await usernames.getByRauthyId(user.id); | ||
if (username) { | ||
recipients.push(user.email); | ||
} | ||
} | ||
} else { | ||
recipients.push(rawRecipient); | ||
} | ||
|
||
for (const recipient of recipients) { | ||
await emailer.sendMail({ | ||
to: recipient, | ||
subject, | ||
text: bodyMarkdown, | ||
html: bodyHtml | ||
}); | ||
} | ||
|
||
return { success: `Email sent!` }; | ||
} | ||
} satisfies Actions; |
28 changes: 28 additions & 0 deletions
28
src/routes/(internal)/__internal__/admin/send-emails/+page.svelte
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,28 @@ | ||
<script lang="ts"> | ||
import type { ActionData } from './$types'; | ||
const { form }: { form: ActionData } = $props(); | ||
</script> | ||
|
||
{#if form?.error} | ||
<article class="pico-background-red-550"> | ||
{form.error} | ||
</article> | ||
{:else if form?.success} | ||
<article class="pico-background-green-550"> | ||
{form.success} | ||
</article> | ||
{/if} | ||
|
||
<h2>Send Email From Weird</h2> | ||
|
||
<p> | ||
Set recipient to <code>___everyone___</code> to send an email to all users with a username set. | ||
</p> | ||
|
||
<form method="post" action="?/sendEmail"> | ||
<input name="recipient" placeholder="recipient" /> | ||
<input name="subject" placeholder="subject" /> | ||
<textarea name="bodyMarkdown" placeholder="Markdown body"></textarea> | ||
<button>Send</button> | ||
</form> |