'use strict';

const request = require('request');
const config = require('../config');

module.exports = (toAddress, matchName, subject, message) => {
    let url = `https://api.mailgun.net/v3/${config.mailgunDomain}/messages`;

    let handleResponse= (err, httpResponse, body) => {
        if (err) {
            throw err;
        } else {
            console.log(body);
        }
    };

    request.post(url, handleResponse).form({
        from: 'Secret Santa <secretsanta@mattgmade.me>',
        to: toAddress,
        subject: subject || 'Your "Secret Santa" match',
        text: message.replace('{{name}}', matchName) || `Your Secret Santa match is ${matchName}!`,
        'o:tracking-opens': 'yes'
    }).auth(config.mailgunUsername, config.mailgunPassword);
};


@@ -581,16 +581,47 @@
    text:
+ typeof(message) === 'string' ?
  message
@@ -652,18 +652,17 @@
 chName) 
-%7C%7C
+:
  %60Your S

fix errors when message is not provided on /send