Send html email with attachment

Enonic version: enonic-xp-6.6
OS: OsX El Capitain

Hey guys!

So, in the documentation for the mail library, there is a field that should’ve been specific to the content-type of the body.
It works perfectly when I’m sending mail without an attachment.

But, if I do have an attachment, the message is screwed up.

See the original messages below:
When I do include the content-type of the body:

[…]
MIME-Version: 1.0
Content-Type: text/html; charset=“UTF-8”

------=_Part_8_950604243.1470074040082
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
[…message…]

When I do not:

[…]
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=“----=_Part_4_795001575.1470073556778”

------=_Part_4_795001575.1470073556778
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
[…message…]

It would be ok just to not include the content-type of the mail, but, my body is html.
Can you guys help me here?

The boundary is being lost in the process, and since I have no control over the uid used there, it would be hard to include it myself.

Thanks!

I think this is a bug. We set the content-type header on the message, but when there are attachments we should have set it on the text part, not in the multipart.

You can try something as a workaround (until we fix it), but I’m not sure if it will work:

  • Leave the body of the message empty.
  • Add an extra attachment that will contain the body.
  • In the extra attachment set the mimeType=“text/html”.
  • In the extra attachment, set the message text as data using the io lib.

Something like this:

var mailLib = require('/lib/xp/mail');
var ioLib = require('/lib/xp/io');

mailLib.send({
    from: 'foo@bar',
    to: 'foo@bar',
    subject: 'Test',
    body: '',
    attachments: [
        {
            fileName: 'attach1.png',
            mimeType: 'image/png',
            data: attachmentStream1
        },
        {
            mimeType: 'text/html',
            data: ioLib.newStream('<h1>some html</h1>')
        }
    ]
});

I’m afraid this one won’t work :confused:

When we leave the filename off, nothing happens. When we put any filename, then it becomes a file to be downloaded.

Thanks for the help thou.

Ok. It was worth a try. :disappointed:
We will fix the bug in 6.7, expected at the beginning of September.

1 Like

We have decided to include the fix in 6.6.1 as well. Coming today or tomorrow.

1 Like

Those are really good news, thanks!

Looking forward to it!

We have just released 6.6.1 version with a fix for this issue.

Here is the changelog: https://github.com/enonic/xp/releases/tag/v6.6.1

1 Like