This question already has an answer here:
I want to send a html message with Mailx. When I try the following command
mailx -s "Subject" user@gmail.com < email.html
I get the content of email.html in plain text. In the message the header Content-Type is set to text/plain. The -a option tries to send a file so I didn't find out how to modify the header. This answer almost worked, it sets well the Content-Type to text/html but doesn't substitute the default Content-Type which is text/plain.
mailx -s "$(echo -e "This is the subject\nContent-Type: text/html")" user@gmail.com < email.html
gives this result :
From: send@gmail.com
To: user@gmail.com
Subject: This is the subject
Content-Type: text/html
Message-ID: <538d7b66.Xs0x9HsxnJKUFWuI%maikeul06@gmail.com>
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
boundary="=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK"
This is a multi-part message in MIME format.
--=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<html>
<body>
<p>Helo wolrd</p>
</body>
</html>
PS : I also tried with uuencode. When I try to display the message in the webmail I get a blank page...
Well, the "-a" mail and mailx in Centos7 is "attach file" not "append header." My shortest path to a solution on Centos7 from here: stackexchange.com
Basically:
It's easy, if your
mailx
command supports the-a
(append header) option:If it doesn't, try using
sendmail
:There are many different versions of mail around. When you go beyond mail -s subject to1@address1 to2@address2
With some mailx implementations, e.g. from mailutils on Ubuntu or Debian's bsd-mailx, it's easy, because there's an option for that.
With the Heirloom mailx, there's no convenient way. One possibility to insert arbitrary headers is to set editheaders=1 and use an external editor (which can be a script).
With a general POSIX mailx, I don't know how to get at headers.
If you're going to use any mail or mailx, keep in mind that
This isn't portable even within a given Linux distribution. For example, both Ubuntu and Debian have several alternatives for mail and mailx.
When composing a message, mail and mailx treats lines beginning with ~ as commands. If you pipe text into mail, you need to arrange for this text not to contain lines beginning with ~.
If you're going to install software anyway, you might as well install something more predictable than mail/Mail/mailx. For example, mutt. With Mutt, you can supply most headers in the input with the -H option, but not Content-Type, which needs to be set via a mutt option.
Or you can invoke sendmail directly. There are several versions of sendmail out there, but they all support sendmail -t to send a mail in the simplest fashion, reading the list of recipients from the mail. (I think they don't all support Bcc:.) On most systems, sendmail isn't in the usual $PATH, it's in /usr/sbin or /usr/lib.
I had successfully used the following on Arch Linux (where the
-a
flag is used for attachments) for several years:This appended the Content-Type header to the subject header, which worked great until a recent update. Now the new line is filtered out of the
-s
subject. Presumably, this was done to improve security.Instead of relying on hacking the subject line, I now use a bash subshell:
And since we are really only using
mailx
's subject flag, it seems there is no reason not to switch tosendmail
as suggested by @dogbane:The use of bash subshells avoids having to create a temporary file.
If you use AIX try this This will attach a text file and include a HTML body If this does not work catch the output in the /var/spool/mqueue