https://github.com/decimoseptimo/rest-mailer
A REST API for sending mails in NodeJS/Express using Mailgun as ESP.
https://github.com/decimoseptimo/rest-mailer
express jamstack mailgun nodejs rest
Last synced: 2 months ago
JSON representation
A REST API for sending mails in NodeJS/Express using Mailgun as ESP.
- Host: GitHub
- URL: https://github.com/decimoseptimo/rest-mailer
- Owner: decimoseptimo
- Created: 2018-05-01T00:41:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-27T23:44:10.000Z (about 6 years ago)
- Last Synced: 2025-03-18T13:36:07.061Z (2 months ago)
- Topics: express, jamstack, mailgun, nodejs, rest
- Language: JavaScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# REST mailer
A REST API for sending mails in NodeJS/Express using Mailgun as ESP.
Specifically, this is a backend service that handles the actual mail sending of a contact form in a static website as part of a JAMstack architecture.
## Installation
- `npm install`
- Review server config in `server.js`
- Setup configuration file in `config/mail-config.js`.\
`config.example/` is provided as example.
- `npm start`## API
##### Send Mail
* POST /api/v1/contact-form_Model_
```js
contact-form {
name (not_null),
email (not_null, email),
subject (not_null),
}
```## Advanced
##### Multiple forms
Creating another form is easy, just clone an existing route. From there you can set the endpoint URL, configuration file, and validate input.```
router.post('/new-endpoint', function (req, res){
const config = require('./config/new-config')
const {name, email, message} = req.bodyif (name && email && message){
sendMail(res, config.mailgun, config.mail(name, email, message))
}
else{
error = {error: 'Missing parameters. Name, email, & message are required.'}
console.log(error)
res.status(400)
res.send(JSON.stringify(error))
}
})
```##### Different domains
If POSTing from a different domain, add the domain(s) to the `allowedOrigins` array. This is in compliance with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) policy. Otherwise the browser will fail to load the endpoint and its console may show `No 'Access-Control-Allow-Origin' header is present ` errors.## Testing
To test from localhost you can issue the following commands in the terminal.##### Root entrypoint
```
curl http://localhost:9980/api/v1
```Returns the confirmation string `/`
##### Contact form entrypoint
```
curl -d "name=value1&[email protected]&message=value3" -X POST http://localhost:9980/api/v1/contact-form
```
Returns a JSON string returned by the mailgun service. It contains a success or failure message.