is it possible to send email with casperjs?

2019-09-02 14:37发布

问题:

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

回答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'
    }
});