0

I am having a hard time finding examples of how to send an email with casperjs. This is my attempt so far... - I successfully got an API key from Mailgun. - I found 2 npm modules such as 'mailgun', and 'mailgun-js' - I loaded them and verified mailgun is loaded globablly and locally.. When I run my casper file. $ casperjs casper-test.js I get the following error: 'undefined' is not a constructor (evaluating 'new mailgun')

var Mailgun = require('mailgun').Mailgun;
var mg = new Mailgun('key-xxxxxxxxxxxxxxxa078a515af');

sendText('no-reply@at.com',
     ['myemail@gmail.com'],
     'Testing, casper-test',
     {'X-Campaign-Id': 'something'},
     function(err) { err && console.log(err) });

My questions are... Am I even going in the right direction? is casperjs capable of firing off one of these modules? If so please let me know how I can fix

William Howley
  • 483
  • 4
  • 20
  • CasperJS/PhantomJS is not node.js. If you need node.js, then run it. – Artjom B. Feb 09 '16 at 16:21
  • http://stackoverflow.com/a/24389819/4179009 This answer explains the confusion between CasperJS and node.js NPM modules. They are not the same. That mailgun module is to be used with node.js, not CasperJS – greg_diesel Feb 09 '16 at 16:26
  • so than does anyone know how to send an email from within casperjs? – William Howley Feb 09 '16 at 17:35

1 Answers1

1

Yes, You can do it with http api:

casper.setHttpAuth('api', 'YOUR-SECRET-API-KEY');
casper.thenOpen('https://api.mailgun.net/v3/YOUR-MG-DOMAIN/messages', {
    method: 'post',
    data:   {
        'from': 'mailgun@YOUR-MG-DOMAIN',
        'to': 'target',
        'subject':  'Hello.',
        'text':  'message'
    }
});
Fabien Fleureau
  • 693
  • 5
  • 14