Mailx command unable to send inline HTML content rather printing the html tags

Hello Friends,

Recently we have moved to Kubernetes cluster from on-premise infra. I was able to send HTML format emails (inline) in sendmail unix command but when I tried to do the same in mailx command it is not sending the HTML format rather printing the HTML tags in the mail body .

I had searched the other topics mentioning the same issue and also tried to use the proposed code but it is giving the same issue. The solution which worked for others is not working for me.

mailx -s "$(echo -e "Report Automation\nContent-Type: text/html")" -S smtp="xxxxxxxxxxxxxxxxx.com" wwwwwww.ffffff@xxx.com < 11.html

Thanks.

The "content-type" header should be in the text body:

(
echo "Content-Type: text/html"
cat 11.html
) |
mailx -s "Report Automation" -S smtp="xxxxxxxxxxxxxxxxx.com" wwwwwww.ffffff@xxx.com

Thanks for the reply.
It is still showing the html tags in the body along with the
Content-Type: text/html on the top.

The issue is that mailx is not properly interpreting the Content-Type: text/html header when you pass it in the subject using -s. That is not the correct way to send an HTML email.

To send HTML emails using mailx (specifically the Heirloom or BSD version), you should use the -a option to specify the content type.

Example

mailx -a "Content-Type: text/html" -s "Report Automation" -S smtp="xxxxxxxxxxxxxxxxx.com" wwwwwww.ffffff@xxx.com < 11.html

Not sure about your mailx version @Showdown . Did you mention that?

Thank you.
I have tried -a option already but the -a is rather interpreting it is an attachment and Producing the error as No such directory .

the version I could see is

mailx -V
12.5 7/5/10

Thanks — you’re using the BSD mailx 12.5 version, which does not support -a "Content-Type: text/html" like Heirloom mailx does. In BSD mailx, the -a flag is for attachments only, not headers.

OBTW, @Showdown

You might find this helpful since you are using the BSD version above:

Building the MIME message manually

(
echo "Subject: Report Automation"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo
cat 11.html
) | mailx -S smtp="xxxxxxxxxxxxxxxxx.com" -t wwwwwww.ffffff@xxx.com

Untested: Debug and Adjust as Needed.....

Yes, perhaps the MIME-Version is needed. And perhaps the empty line.
But all mailx versions have -s for the Subject.

Yes, but from my reading, if you manually create the subject (as above) the -s option is not required - but folks are welcome to use -s if they prefer.

This is easy enough to confirm or not, but I don't have access to this version of mailx, sorry.

$ mailx -V
mailx (GNU Mailutils) 3.4

Thanks for replying but it is producing the same output in the mail body.

Subject: Report Automation
MIME-Version: 1.0
Content-Type: text/html

CTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a very basic HTML page.</p>
</body>
</html>

Did you try sendmail, something like:

Untested.....

{
  echo "Subject: Report Automation"
  echo "MIME-Version: 1.0"
  echo "Content-Type: text/html"
  echo ""
  cat 11.html
} | /usr/sbin/sendmail -t wwwwwww.ffffff@xxx.com

EDIT: I see you had success with sendmail. so why not just use what works, @Showdown ?

Sendmail utility is not installed (may be purposefully) as a part of the base docker image in k8 pods.

Well, since mailx does not handle HTML mime-types well, then you have decide how to move forward.

Sometimes you have to install code in containers when you want to customize it; but then you must maintain the new image.

Does your base image have mutt ?

Unfortunately no mutt too. So I was exploring all possibilities to send inline HTML in mailx.

Alternatively, you could create a bin shared volume between your host and container and run a staticly compiled version of sendmail or mutt or a different version of mailx from the host in your container.

That way you can keep the base image clean - and use a shared vol.

  • Same architecture: e.g., x86_64 host ↔ x86_64 container.
  • Statically linked binaries: These don’t rely on host/container shared libraries (glibc, libc, etc.), so they’re portable across distros.

Obviously mailx adds a newline before the body text starts, so the header ends there.
Linux mailx has option -t that promises to take the header from the top of the message, and ignoring all header options.

{
  echo "Subject: Report Automation"
  echo "To: wwwwwww.ffffff@xxx.com"
  echo "MIME-Version: 1.0"
  echo "Content-Type: text/html"
  echo ""
  cat 11.html
} | mailx -t wwwwwww.ffffff@xxx.com