Skip to content

Commit

Permalink
feat: switch to loops for emails
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-bloom committed Oct 20, 2024
1 parent 9762f34 commit c4489b3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 45 deletions.
31 changes: 10 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"astro-icon": "^1.1.1",
"limax": "4.1.0",
"lodash.merge": "^4.6.2",
"nodemailer": "^6.9.15",
"loops": "^3.3.0",
"preact": "^10.24.3",
"typescript": "^5.6.3",
"unpic": "^3.18.0"
Expand All @@ -52,7 +52,6 @@
"@types/js-yaml": "^4.0.9",
"@types/lodash.merge": "^4.6.9",
"@types/mdx": "^2.0.13",
"@types/nodemailer": "^6.4.16",
"@typescript-eslint/eslint-plugin": "^8.8.1",
"@typescript-eslint/parser": "^8.8.1",
"astro-compress": "2.3.3",
Expand Down
2 changes: 1 addition & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

type KVNamespace = import('@cloudflare/workers-types').KVNamespace;
type ENV = {
SMTP: KVNamespace;
LOOPS: KVNamespace;
};

// use a default runtime configuration (advanced mode).
Expand Down
45 changes: 25 additions & 20 deletions src/pages/api/contact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { APIRoute } from 'astro';
import nodemailer from 'nodemailer';
import type SMTPTransport from 'nodemailer/lib/smtp-transport';
import { LoopsClient } from 'loops';
export const prerender = false;

export const POST: APIRoute = async ({ request, locals }) => {
Expand All @@ -24,28 +23,34 @@ export const POST: APIRoute = async ({ request, locals }) => {
return new Response(JSON.stringify({ errors: errors }), { status: 400 });
}

const smtpKv = locals.runtime.env.SMTP;
const smtpHost = await smtpKv.get('host');
const smtpUser = await smtpKv.get('user');
const smtpPassword = await smtpKv.get('password');
const toEmail = await smtpKv.get('toEmail');
const loopsKv = locals.runtime.env.LOOPS;
const loopsApiKey = await loopsKv.get('api_key');
const loopsEmail = await loopsKv.get('email');
const loopsTransactionalId = await loopsKv.get('transactionalId');

try {
const transporter = nodemailer.createTransport({
host: smtpHost,
port: 465,
secure: true,
auth: {
user: smtpUser,
pass: smtpPassword,
const loops = new LoopsClient(loopsApiKey as string);

const resp = await loops.sendTransactionalEmail({
transactionalId: loopsTransactionalId as string,
email: loopsEmail as string,
dataVariables: {
message: message as string,
from: name as string,
reply: email as string,
},
} as SMTPTransport.Options);
await transporter.sendMail({
from: `"${name}" <${email}>`,
to: toEmail as string,
subject: 'Website Contact Me Form',
text: message as string,
});
if (!resp.success) {
console.error(JSON.stringify(resp));
return new Response(
JSON.stringify({
errors: {
submit: 'Unable to send message. Please try again in a few moments.',
},
}),
{ status: 500 }
);
}
} catch (err) {
console.error(err);
return new Response(
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ compatibility_flags = [ "nodejs_compat" ]
compatibility_date = "2024-09-23"

kv_namespaces = [
{ binding = "SMTP", id = "fastmail-smtp" },
{ binding = "LOOPS", id = "loops" },
]

0 comments on commit c4489b3

Please sign in to comment.