Charlie Fish
Charlie Fish

Reputation: 20576

Node Mailgun Not Attaching Files

I have the following code that I'm trying to use to send an email using Mailgun in Node. The code works fine and does send the email but doesn't attach either of the files.

// pdfA and pdfB are both buffers defined earlier
let attachmentA = new mailgun.Attachment({data: pdfA, filename: `pdfA.pdf`, contentType: "application/pdf", knownLength: pdfA.length});
let attachmentB = new mailgun.Attachment({data: pdfB, filename: `pdfB.pdf`, contentType: "application/pdf", knownLength: pdfB.length});
let attachments = [attachmentA, attachmentB];

var data = {
    from: "[email protected]",
    to: "[email protected]",
    subject: `My Test Email`,
    text: 'Hello',
    attachments
};

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

In the NPM readme it mentions the following.

If an attachment object does not satisfy those valid conditions it is ignored. Multiple attachments can be sent by passing an array in the attachment parameter. The array elements can be of any one of the valid types and each one will be handled appropriately.

This is the only thing I have been able to find in terms of what is going wrong. But it looks like my code satisfied all the conditions.

Any way to debug this? It's currently just failing silently. It sends the email and I receive it but no attachments are in the email.

Upvotes: 0

Views: 1255

Answers (1)

Ding
Ding

Reputation: 3085

I'm not 100% sure but I think you need to use the attachment instead of attachments.

Ex:

var data = {
    from: "[email protected]",
    to: "[email protected]",
    subject: `My Test Email`,
    text: 'Hello',
    attachment: attachments
};

Otherwise mailgun isn't seeing any attachments because they're not specified in attachment.

Upvotes: 2

Related Questions