keystonejs / keystone-classic

Node.js CMS and web app framework
http://v4.keystonejs.com
MIT License
14.62k stars 2.21k forks source link

Wrapper around Mailgun not send attachments #4487

Open n-lavrenko opened 7 years ago

n-lavrenko commented 7 years ago

Hi there. I can't send an attachment file via MailGun. First of all: we don't have any documentation about attachments except on the MailGun site (but there is explaining about using module mailgun-js.

Details:

  1. I get a file into my req.files.file and have a path to the file (so it's a absolute path to the temp folder on my MacOS).
  2. Then I create an email instance from keystone class Email:
let email = new keystone.Email({
    templateName: 'request-notification',
    transport: 'mailgun'
});
  1. And then I send an email with options:
    
    let options = {
    to: 'email@email.com',
    from: {
        name: 'Nikitos',
        email: 'nikitos@email.com',
    },
    subject: 'Some subject',
    someDataForTemplate: data,
    brand: 'Some brand',
        attachment = file.path //   /var/folders/vj/947kggz13hq4gkx7sq_76xjc0000gn/T/7bda0904f4b5caddc78467bdb127a120.png
    };

email.send(options, callback);

4. After `emal.send` I have an error:

Mongoose model 'error' event fired on 'User' with error: source.on is not a function TypeError: source.on is not a function at Function.DelayedStream.create (/projects/metallita/node_modules/delayed-stream/lib/delayed_stream.js:33:10) at FormData.CombinedStream.append (/projects/metallita/node_modules/combined-stream/lib/combined_stream.js:43:37) at FormData.append (/projects/metallita/node_modules/keystone-email/node_modules/form-data/lib/form_data.js:68:3) at Request.prepareFormData (/projects/metallita/node_modules/keystone-email/node_modules/mailgun-js/lib/request.js:186:21) at Request.request (/projects/metallita/node_modules/keystone-email/node_modules/mailgun-js/lib/request.js:61:12) at Mailgun.request (/projects/metallita/node_modules/keystone-email/node_modules/mailgun-js/lib/mailgun.js:74:14) at constructor.(anonymous function) [as send] (/projects/metallita/node_modules/keystone-email/node_modules/mailgun-js/lib/build.js:101:24) at send (/projects/metallita/node_modules/keystone-email/lib/transports/mailgun/index.js:26:20) at /projects/metallita/node_modules/keystone-email/lib/Email.js:125:3 at /projects/metallita/node_modules/keystone-email/lib/Email.js:77:3 at Object.exports.renderFile (/projects/metallita/node_modules/pug/lib/index.js:412:12) at Email.exports.__express [as engine] (/projects/metallita/node_modules/pug/lib/index.js:455:11) at Email.render (/projects/metallita/node_modules/keystone-email/lib/Email.js:75:7) at Email.send (/projects/metallita/node_modules/keystone-email/lib/Email.js:123:7) at Email.email.send (/projects/metallita/node_modules/keystone/lib/email.js:102:8) at /projects/metallita/api/request.js:85:10 (anonymous) @ register.js:83 emitOne @ events.js:115 emit @ events.js:210 (anonymous) @ query.js:2333 runCallback @ timers.js:781 tryOnImmediate @ timers.js:743 processImmediate @ timers.js:714


5. I was tried different option object structures, tried to send the whole file, and via `mailgun.Attachment` constructor:
`var attch = new mailgun.Attachment({data: file, filename: filename});`
**So, nothing works.** 

P.S. I found a solution in using directly `mailgun-js`. This is works fine:

let mailgun = require('mailgun-js')({ apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN });

var data = { from: 'Excited User me@samples.mailgun.org', to: 'info@info.com', subject: 'Hello', text: 'Testing some Mailgun awesomness!', attachment: file.path };

mailgun.messages().send(data, function (error, body) { console.log(body); // success });



But it is so sad that keystone Email wrapper didn't work with attachments. I think that it is a critical bug. I spent about 4 hours for solve the problem...

However I still enjoy in keystonejs and trust that it will be evolve further!
Thank you!
ahhmarr commented 6 years ago

I just checked it's working

keystone-email : 1.0.5 which uses mailgun-js : 0.7.15 this is my code


new mailer('templates/emails/invoice.pug', {
        transport: 'mailgun',
        engine   : 'pug',
    }).send(
        { invoiceNumber: inv.invoiceNumber, orderNumber: this.ordernumber },
        {
            to        : 'myemai',
            from      : 'fromemail',
            apiKey    : process.env.MAILGUN_API_KEY,
            domain    : process.env.MAILGUN_DOMAIN,
            attachment: __dirname + `/../${pdf}`,
        },
        (err, resp) => {
            console.log(err, resp);
        }
    );```