Skip to content

Commit

Permalink
fix(app): use aws-ses in contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewklau committed Sep 14, 2024
1 parent 599ea9a commit ee9a788
Show file tree
Hide file tree
Showing 8 changed files with 886 additions and 41 deletions.
8 changes: 5 additions & 3 deletions apps/app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ NEXT_PUBLIC_LOADER_URL=http://127.0.0.1:3030
NEXT_PUBLIC_OG_URL=https://meta.nearblocks.io/api
NEXT_PUBLIC_MAINNET_URL=https://nearblocks.io
NEXT_PUBLIC_TESTNET_URL=https://testnet.nearblocks.io
BREVO_URL=https://api.brevo.com/v3/smtp/email
BREVO_API_KEY=
BREVO_TO_EMAIL=
AWS_ACCESS_KEY_ID=your-aws-access-key-id
AWS_SECRET_ACCESS_KEY=your-aws-secret-access-key
AWS_REGION=your-aws-region
AWS_SES_FROM_EMAIL=AWS_SES_FROM_EMAIL
AWS_SES_TO_EMAIL=AWS_SES_TO_EMAIL
NEXT_PUBLIC_GTM_ID=GTM-P285ZPV2
NEXT_PUBLIC_USER_API_URL=https://api.exploreblocks.io/api/
NEXT_PUBLIC_USER_DASHBOARD_URL=https://dash.nearblocks.io/
Expand Down
7 changes: 4 additions & 3 deletions apps/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ Configure the nearblocks Gateway by modifying the .env file. Customize settings
`NEXT_PUBLIC_OG_URL=https://meta.exploreblocks.io/api`
`NEXT_PUBLIC_MAINNET_URL=https://nearblocks.io`
`NEXT_PUBLIC_TESTNET_URL=https://testnet.nearblocks.io`
`BREVO_URL=https://api.brevo.com/v3/smtp/email`
`BREVO_API_KEY=`
`BREVO_TO_EMAIL=`
`AWS_ACCESS_KEY_ID=your-aws-access-key-id`
`AWS_SECRET_ACCESS_KEY=your-aws-secret-access-key`
`AWS_REGION=your-aws-region`
`[email protected]`
`NEXT_PUBLIC_GTM_ID=GTM-P285ZPV2`

## Deployment
Expand Down
1 change: 1 addition & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint:check": "tsc --noEmit && eslint ./"
},
"dependencies": {
"@aws-sdk/client-ses": "^3.651.1",
"@braintree/sanitize-url": "7.0.1",
"@near-wallet-selector/bitget-wallet": "8.9.13",
"@near-wallet-selector/bitte-wallet": "8.9.13",
Expand Down
56 changes: 34 additions & 22 deletions apps/app/src/pages/api/contact.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
const API_URL = process.env.BREVO_URL;
const API_KEY = process.env.BREVO_API_KEY;
const TO_EMAIL = process.env.BREVO_TO_EMAIL;
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";

const TO_EMAIL = process.env.AWS_SES_TO_EMAIL;
const FROM_EMAIL = process.env.AWS_SES_FROM_EMAIL;

// Initialize the AWS SES Client
const sesClient = new SESClient({
region: process.env.AWS_REGION,
});

export default async function handler(req: any, res: any) {
const { method } = req;

if (method === 'POST') {
try {
const { body } = req;

console.log(body);
const { name, email, subject, description } = body;

if (!API_URL) {
return res.status(500).json({ status: 0, err: 'API URL not defined' });
if (!TO_EMAIL || !FROM_EMAIL) {
return res.status(500).json({ status: 0, err: 'Sender or recipient email not defined' });
}

const requestOptions = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Api-Key': API_KEY || '',
// Set up the email parameters
const params = {
Source: FROM_EMAIL, // Verified sender email
Destination: {
ToAddresses: [TO_EMAIL], // Recipient's email
},
Message: {
Subject: {
Data: subject,
},
Body: {
Html: {
Data: `<p><strong>From:</strong> ${name} (${email})</p><p>${description}</p>`,
},
Text: {
Data: `From: ${name} (${email})\n\n${description}`, // Plaintext fallback
},
},
},
body: JSON.stringify({
sender: { name, email },
to: [{ email: TO_EMAIL || '' }],
subject,
htmlContent: description,
}),
};

const response = await fetch(API_URL, requestOptions);
const data = await response.json();
// Send the email using AWS SES
const command = new SendEmailCommand(params);
const data = await sesClient.send(command);

return res.status(200).json(data);
return res.status(200).json({ status: 1, data });
} catch (err: any) {
return res.status(500).json({ status: 0, err: err.message });
}
Expand Down
8 changes: 5 additions & 3 deletions mainnet-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ services:
NEXT_PUBLIC_OG_URL: https://meta.exploreblocks.io/api
NEXT_PUBLIC_MAINNET_URL: https://nearblocks.io
NEXT_PUBLIC_TESTNET_URL: https://testnet.nearblocks.io
BREVO_URL: https://api.brevo.com/v3/smtp/email
BREVO_API_KEY: ${BREVO_API_KEY}
BREVO_TO_EMAIL: ${BREVO_TO_EMAIL}
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_REGION: ${AWS_REGION}
AWS_SES_FROM_EMAIL: ${AWS_SES_FROM_EMAIL}
AWS_SES_TO_EMAIL: ${AWS_SES_TO_EMAIL}
NEXT_PUBLIC_GTM_ID: GTM-P285ZPV2
NEXT_PUBLIC_USER_API_URL: https://api.exploreblocks.io/api/
NEXT_PUBLIC_USER_DASHBOARD_URL: https://dash.nearblocks.io/
Expand Down
8 changes: 5 additions & 3 deletions testnet-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ services:
NEXT_PUBLIC_OG_URL: https://meta.exploreblocks.io/api
NEXT_PUBLIC_MAINNET_URL: https://nearblocks.io
NEXT_PUBLIC_TESTNET_URL: https://testnet.nearblocks.io
BREVO_URL: https://api.brevo.com/v3/smtp/email
BREVO_API_KEY: ${BREVO_API_KEY}
BREVO_TO_EMAIL: ${BREVO_TO_EMAIL}
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_REGION: ${AWS_REGION}
AWS_SES_FROM_EMAIL: ${AWS_SES_FROM_EMAIL}
AWS_SES_TO_EMAIL: ${AWS_SES_TO_EMAIL}
NEXT_PUBLIC_GTM_ID: GTM-P285ZPV2
NEXT_PUBLIC_USER_API_URL: https://api.exploreblocks.io/api/
NEXT_PUBLIC_USER_DASHBOARD_URL: https://dash.nearblocks.io/
Expand Down
8 changes: 5 additions & 3 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
"NEXT_PUBLIC_OG_URL",
"NEXT_PUBLIC_MAINNET_URL",
"NEXT_PUBLIC_TESTNET_URL",
"BREVO_URL",
"BREVO_API_KEY",
"BREVO_TO_EMAIL",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_REGION",
"AWS_SES_FROM_EMAIL",
"AWS_SES_TO_EMAIL",
"NEXT_PUBLIC_GTM_ID",
"NODE_ENV",
"NEXT_PUBLIC_USER_API_URL",
Expand Down
Loading

0 comments on commit ee9a788

Please sign in to comment.