question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending mail with MIME, "message" parameter expected as file (?)

See original GitHub issue

I tried to send message as MIME, I fall to this (with MIME) because I’m looking for sending inline attachment (from base64, file attachment is work), but all over the tutorial (included in this repository) are talking about mailgun-js, not mailgun.js.

So, here is my issue, I got this error message: [Error: BAD REQUEST] { status: 400, details: {\n "message": "'message' parameter is not a file"\n} }

This is my code:

    const formData = require('form-data');
    const Mailgun = require('mailgun.js');
    const mailgun = new Mailgun(formData);
    const mg = mailgun.client({ username: 'api', key: data.mailgun_key });

    let mail_data = {
        html: data.message.html
    };

    if (data.message.attachments) {
        mail_data.attachment = [];
        mail_data.inline = [];
        data.message.attachments.forEach(attachment => {
            if (attachment.disposition == 'inline') {
                mail_data.attachment.push({
                    filename: attachment.filename,
                    path: Buffer.from(attachment.content, 'base64'),
                    cid: 'logo.png'
                })
            }
        });
    }

    if (data.message.bcc) {
        // mail_data.bcc = data.message.bcc
    }

    const mail = new MailComposer(mail_data); // converting to MIME
    mail.compile().build()
    .then(message=>{
        console.log(message)
        
        return mg.messages.create(data.outbound_host, {
            from: data.message.from_name+" <"+data.message.from_email+">",
            to: data.message.to,
            subject: data.message.subject,
            message: message,
            'h:Content-type': 'multipart/form-data'
        })
    })
    .then(resp=>console.log('Message [%s]: %s', resp.id, resp.message))
    .catch(error => {
        console.log(error);
    });

I’ve tried to make MIME message to string (ascii), but it still asking for a file. I dunno where should looking for answer anymore, no one talking about mailgun.js.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TheArKaIDcommented, May 7, 2021

Same issue here with attachments ( and their outdated documentation ), specifically with inline images.

@lcoq @TheArKaID I see you use MailComposer, do you mind posting more complete code snippet with MailComposer? ( npm says it’s deprecated: https://www.npmjs.com/package/mailcomposer )

I see here, you dont run it through MailComposer, and will my-file.pdf if it was an image be used inline? I am just a little confused here.

let message = {
  from: `Me <from@email.com>`,
  to: `to@email.com`,
  subject: `My subject`,
  text: `My text message\nAs an example.`,
  attachment: [
    {
      data: Readable.from(file.buffer),
      filename: `my-file.pdf`
    }
  ]
};

mailgun.messages.create(`my-domain`, message)

Mailcomposer is submoduled to Nodemailer (check it). About my-file.pdf, that is another way to send an email, but inline image need a Content-ID so it could be a reference in html code.

Alternative I found another solution, by using Nodemailer with nodemailer-mailgun-transport, there’s an example about how to send an html mail with inline image, but I though it’s not as complex as mailgun.js that have domain, event, stat, etc. (idk, right now I just need to send email).

0reactions
olexandr-mazepacommented, Nov 25, 2021

@TheArKaID @spijs It was a bug with handling mime messages in SDK https://github.com/mailgun/mailgun.js/pull/213 I published a fix a minute ago. Starting version 4.0.1. sending mime messages should work well.

This is an example of sending a mime message from Node.js

const formData = require('form-data');
const Mailgun = require('mailgun.js');
const DOMAIN = 'Your domain here';
const mailgun = new Mailgun(formData);
const MailComposer = require('nodemailer/lib/mail-composer');

const mg = mailgun.client({ username: 'api', key: 'your key here' });

(async () => {
  const data = {
    message: {
      from: 'Excited User <mailgun@sandbox-123.mailgun.org>',
      subject: 'Test Sending mime messages from node',
      text: 'This is mime message',
      html: '<html>HTML version of the body</html>'
    }
  };

  try {
    const mail = new MailComposer(data.message); // converting to MIME
    const compiledMessage = await mail.compile().build();

    const resp = await mg.messages.create(DOMAIN, {
      to: ["test@example.com"],
      message: compiledMessage
    });
    console.log(resp );
  } catch (error) {
    console.error(error);
  }
})();

I used MailComposer to convert the message data object to a mime message but you may choose any other library to do that.

To send mime message from the browser you need to wrap a mime message in the blob in the next way:

new Blob([compiledMessage.toString('ascii')])

where compiledMessage is a message already in mime format.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending mail with MIME, "message" parameter expected as ...
I tried to send message as MIME, I fall to this (with MIME) because I'm looking for sending inline attachment (from base64, file...
Read more >
The Message Content-Type in MIME
It is frequently desirable, in sending mail, to encapsulate another mail message. For this common operation, a special Content-Type, "message", is defined.
Read more >
Sending an email via the Python email library throws error ...
Calling email() produces the error expected string or bytes-like object . Redefining server.sendmail("test@gmail.com", "testee@gmail.com", msg) ...
Read more >
Seeing errors when sending a mail with files attached ... - Drupal
When sending an email with a pdf attachment you get the following errors: Warning: realpath() expects parameter 1 to be a valid path,...
Read more >
MIME types (IANA media types) - HTTP - MDN Web Docs
MIME types are case-insensitive but are traditionally written in lowercase. The parameter values can be case-sensitive.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found