'use strict';

var Mailgun = require('mailgun-js');
var packageData = require('../package.json');

module.exports = function (options) {
  return new MailgunTransport(options);
};

function MailgunTransport(options) {
  this.options = options || {};
  this.name = 'Mailgun';
  this.version = packageData.version;

  this.mailgun = Mailgun({
    apiKey: this.options.auth.api_key,
    domain: this.options.auth.domain || ''
  });
}


MailgunTransport.prototype.send = function send(mail, callback) {
  var mailData = mail.data;
  // convert nodemailer attachments to mailgun-js attachements
  if(mailData.attachments){
    var a, b, aa = [];
    for(var i in mailData.attachments){
      a = mailData.attachments[i];
      b = new this.mailgun.Attachment({
        data        : a.path || undefined,
        filename    : a.filename || undefined,
        contentType : a.contentType || undefined,
        knownLength : a.knownLength || undefined
      });

      aa.push(b);
    }
    mailData.attachment = aa;

  }

  this.mailgun.messages().send({
    type       : mailData.type,
    to         : mailData.to,
    from       : mailData.from,
    subject    : mailData.subject,
    bcc        : mailData.bcc,
    text       : mailData.text,
    html       : mailData.html,
    attachment : mailData.attachment
  }, callback);

};


@@ -1012,40 +1012,27 @@
   %7D%0A
+  
 %0A  
-this.mailgun.messages().send(
+var options = 
 %7B%0A  
@@ -1162,39 +1162,8 @@
 ct,%0A
-    bcc        : mailData.bcc,%0A
     
@@ -1259,19 +1259,119 @@
 achment%0A
-
   %7D
+%0A  %0A  if( mailData.bcc )%7B%0A    options.bcc = mailData.bcc%0A  %7D%0A%0A  this.mailgun.messages().send(options
 , callba

fix send failing if no bcc provided