let querystring = require('querystring') let stream_buffers = require('stream-buffers') let XDate = require('xdate') return async env => { let navbar = await _require('/navbar.jst') let contact_form = await _require('/contact_form.jst') await navbar( env, async _out => {}, async _out => { h1 {'Contact NDCODE'} div(style="height: 15px;") {} if (env.request.method == 'POST') { let write_stream = new stream_buffers.WritableStreamBuffer() let data = new Promise( (resolve, reject) => { write_stream. on('finish', () => {resolve(write_stream.getContents())}). on('error', () => {reject()}) } ) env.request.pipe(write_stream) let query = querystring.parse((await data).toString()) let full_email = `${query.first_name} ${query.last_name} <${query.email}>` console.log('received contact form:', full_email) // save the form contents in a dated logfile, so that we can // recover manually if the email doesn't send for some reason date = new XDate() query.date = date.toUTCString() await env.site.ensure_dir('/_logs') env.site.modify_json( `/_logs/contact_${date.toUTCString('yyyyMMdd')}.json`, [], async result => {result.value.push(query)} ) // send email (asynchronously) ;(await env.site.get_emailjs('/_config/email_contact.json')).send( { text: query.message, from: 'Contact form ', 'reply-to': full_email, to: 'Nick Downing ', subject: Object.prototype.hasOwnProperty.call(query, 'company') ? 'Enquiry: ' + query.company : 'Enquiry' }, (err, message) => { if (err) console.error(err.stack || err.message) else console.log('sent contact email:', full_email) } ) p {'Thanks! We\'ll be in touch as soon as we can.'} } else { p {'Do you require more information or consulting assistance with integrating the projects on this site? We\'d love to hear from you.'} await contact_form(env, _out) } } ) }