Rob
Rob

Reputation: 1636

mailgun attachment from stream. How to apply filename

I'm using node html-pdf to create a PDF (wrapped it because I like promises)

 const createPDF = (html, options) => new Promise(((resolve, reject) => {
        pdf.create(html, options).toBuffer((err, buffer) => {
            if (err !== null) {
                reject(err);
            }
            else {
                resolve(buffer);
            }
        });
    }));

    const PDF = await createPDF(html, options);

PDF is a buffer - I can attach this 'as is' to mailgun, and it sends the file without issue.

In node mailgun-js, I pass the following:

 const data = {
        from,
        to,
        subject,
        text,
        attachment: PDF
    };

I've looked all over the docs - but cannot see where I can add a filename for the attached file. The received attachment defaults to "file"... with no PDF extension. I found a PHP example where a filename had been added... I tried the same param, and it didn't work with the node library.

Is it possible to add a filename?

Thanks

Upvotes: 1

Views: 2855

Answers (1)

Rob
Rob

Reputation: 1636

Ok... so you will not find this on mailguns website... this is not in their docs or examples. I stumbled upon the npm docs for their library (I should have gone there first). In order to attach a filename to a stream you have to create a mailgun.attachment object with the following:

const attch = new mailgun.Attachment({data: file, filename: filename});

file can be a stream, file path or buffer. Filename is what you want to call it...

Then you attach this object..

https://www.npmjs.com/package/mailgun-js

Upvotes: 3

Related Questions