Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Email Contact Form function #3

Merged
merged 16 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions node/email-contact-form/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
6 changes: 6 additions & 0 deletions node/email-contact-form/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
102 changes: 102 additions & 0 deletions node/email-contact-form/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 📬 Node.js Email Contact Form Function

Sends an email with the contents of a HTML form.

## 🧰 Usage

### `GET`

HTML form for interacting with the function.

### `POST`

Submit form data to send an email

**Parameters**

| Name | Description | Location | Type | Sample Value |
| ------ | --------------------------------- | ---------- | ------ | -------------------------------- |
| \_next | URL for redirect after submission | Form Param | String | `https://mywebapp.org/success` |
| \* | Any form values to send in email | Form Param | String | `Hey, I'd like to get in touch!` |

**Response**

Sample `200` Response:

```text
Location: https://mywebapp.org/success
```

Sample `400` Response:

```text
Location: https://mywebapp.org/referer?error=Invalid+email+address
```

## ⚙️ Configuration

| Setting | Value |
| ----------------- | --------------- |
| Runtime | Node (18.0) |
| Entrypoint | `src/main.js` |
| Build Commands | `npm install` |
| | `npm run setup` |
| Permissions | `any` |
| Timeout (Seconds) | 15 |

## 🔒 Environment Variables

### SMTP_HOST

The address of your SMTP server. Many STMP providers will provide this information in their documentation. Some popular providers include: Mailgun, SendGrid, and Gmail.

| Question | Answer |
| ------------ | ------------------ |
| Required | Yes |
| Sample Value | `smtp.mailgun.org` |

### SMTP_PORT

The port of your STMP server. Commnly used ports include `25`, `465`, and `587`.

| Question | Answer |
| ------------ | ------ |
| Required | Yes |
| Sample Value | `25` |

### SMTP_USERNAME

The username for your SMTP server. This is commonly your email address.

| Question | Answer |
| ------------ | ----------------------- |
| Required | Yes |
| Sample Value | `[email protected]` |

### SMTP_PASSWORD

The password for your SMTP server.

| Question | Answer |
| ------------ | --------------------- |
| Required | Yes |
| Sample Value | `5up3r5tr0ngP4ssw0rd` |

### SUBMIT_EMAIL

The email address to send form submissions to.

| Question | Answer |
| ------------ | ----------------- |
| Required | Yes |
| Sample Value | `[email protected]` |

### ALLOWED_ORIGINS

An optional comma-separated list of allowed origins for CORS (defaults to `*`). This is an important security measure to prevent malicious users from abusing your function.

| Question | Answer |
| ------------- | ------------------------------------------------------------------- |
| Required | No |
| Sample Value | `https://mywebapp.org,https://mywebapp.com` |
| Documentation | [MDN: CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) |
14 changes: 14 additions & 0 deletions node/email-contact-form/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
STMP_HOST: string;
STMP_PORT?: string;
STMP_USERNAME: string;
STMP_PASSWORD: string;
SUBMIT_EMAIL: string;
ALLOWED_ORIGINS?: string;
}
}
}

export {};
42 changes: 42 additions & 0 deletions node/email-contact-form/package-lock.json

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

16 changes: 16 additions & 0 deletions node/email-contact-form/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "email-contact-form",
"version": "1.0.0",
"description": "",
"main": "src/main.js",
"type": "module",
"scripts": {
"format": "prettier --write ."
},
"dependencies": {
"nodemailer": "^6.9.3"
},
"devDependencies": {
"prettier": "^3.0.0"
}
}
15 changes: 15 additions & 0 deletions node/email-contact-form/src/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function isOriginPermitted(req) {
if (!process.env.ALLOWED_ORIGINS || process.env.ALLOWED_ORIGINS === '*')
return true;
const allowedOriginsArray = process.env.ALLOWED_ORIGINS.split(',');
return allowedOriginsArray.includes(req.headers['origin']);
}

export function getCorsHeaders(req) {
return {
'Access-Control-Allow-Origin':
!process.env.ALLOWED_ORIGINS || process.env.ALLOWED_ORIGINS === '*'
? '*'
: req.headers['origin'],
};
}
Loading
Loading