Skip to content

A REST API for sending mails in NodeJS/Express using Mailgun as ESP.

Notifications You must be signed in to change notification settings

decimoseptimo/REST-mailer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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.body

    if (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 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=value2@example.com&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.

About

A REST API for sending mails in NodeJS/Express using Mailgun as ESP.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published