I have some text in a file in UNIX. I send that file to my mail using mail command. What I want is the text to be appeared in some color in the body of the mail. The mail will be opened from Windows. Any help would be greatly appreciated.

  • 1
    This is a bad idea. Adding any sort of formatting to an email message tremendously increases the odds that it will be discarded as spam. – zwol Mar 9 '16 at 16:55
  • 1
    @zwol While converting an email. to html can slightly increase its spammyness, doing so properly will not significantly increase its odds of being misclassified as spam. I know because I run an email service ! – davidgo Mar 9 '16 at 18:07
  • Thanks a lot @zwol to bring the discussion of spam here. I need to take care of formatting as I don't want this mail to be a spam to recipient as this is so important to be read by recipient. +1 for this ☺ – learner1 Mar 10 '16 at 2:50

You would need to convert the contents to HTML, and ensure the header of the email includes

Content-Type: text/html; charset=UTF-8

How you would do this would depend on what program your "mail" command is, and indeed may not be possible. You could modify the script to inject the email into sendmail or similar instead by something like

echo 'Content-Type: text/html; charset=UTF-8
Subject: My Subject

<b>Email Header</b>
<br /><br />
<font color="red">This text is red</font>
</html>' | sendmail user@domain.name

Note that this is a slightly crude implementation and may be more likely to dropped by spam filters unless tuned.

  • Upvoted since yours is the practical answer. Wrote another one with some background since I felt that editing in that much extra information would distord your answer. – Hennes Mar 9 '16 at 7:50
  • <font>? Is the email world lagging that much behind or should you just be using css in one form or another? – Jasper Mar 9 '16 at 12:13
  • 3
    @Jasper Outlook 2010 and up use Word's engine to render emails. So, no, it isn't lagging: it's walking backwards! Thunderbird is really good and supports inline CSS and HTML4.01. For web-based services (Hotmail, GMail, Yahoo ...), you should use HTML4.01 with as little inline CSS as possible. – Ismael Miguel Mar 9 '16 at 12:35
  • 1
    Please, do not use <br /><br />. You should use <h1>Email header</h1> <p><font color="red">This text is red</font></p>. Also, most email clients use HTML4.01, which requires a <body>. Other than that, spot on! – Ismael Miguel Mar 9 '16 at 12:38
  • 1
    @Jasper You are welcome. The email world is insanelly insane! Just so you see how crazy it is, Outlook Express 6.0 and Outlook 2003/2007 use IE to render emails, but, at least Outlook Express 6.0, freaks out when it sees an UTF-8 message. It supports it, but will insist in using whatever default encoding you picked. Some web-based services block external CSS (like GMail), other's don't support some styles (like Hotmail doesn't like paddings, as far as I know) and Outlook 2010 and up don't handle <div> tags (they are converted to hacky paragraphs). The email world is hair-pulling insane! – Ismael Miguel Mar 9 '16 at 15:18

Some more background to davidgo's good answer:

Email standard did not start with colour or font supports. Thus there is no guarantee that the recipient can see any colour, no matter how hard you try.

Most clients however do interpreted mail a bit broader, and if you sent rich text or as webpage then it might render in colour. Alternatively, if you really need a specific markup you will need to send it as a picture (with all downsides, sunch as not being able to select text or edit).

Basic mail from a Unix like platform can be send as follows:

sendmail user_to_mail_to@domain.tld < myfile

Or, if you like cat or start with a pipe:

cat myfile | sendmail user_to_mail_to@domain.tld

Or, from echoing via shell:

echo 'content here' | sendmail user_to_mail_to@domain.tld

All of these will send the contents of myfile, with no subject, no colour, no text markup. Just a plain simple mail. It is a good way to start with to understand the basics.

Now for David answer, it adds a line which tells the mailclient to treat the text as HTML ('Content-Type: text/html). Most mail client will happily recognise that. Some (such as elm which I still use to read suspicious emails) will not.

It also adds a subject which is quite user friendly.

And finally it uses HTML markup (start with <HTML> and with </HTML>, Bold tags, and font setting to set a colour. In this case red, but you can specify any colour with the "color="FF0000" format (in RGB, in this case maxed out red, no green, no blue).

If you have the need for a more complex, variable mail then a shell script can append parts to a file and you can use the first method ( sendmail user < input) to mail the final file.

The mail will be opened from Windows.

The mail client matters here. I expect thunderbird to render this just fine on windows as well on linux or BSD. Similar I expect mutt to fail, both on windows and on BSD's. Desgining a multipart mail with a text alternative might be a good idea. For this see RFC1341.

  • 1
    Do note that if sending an image or other binary, (POSIX) cat needs needs the -v or --show-nonprinting, otherwise much non-ascii data will be lost. Moreover, the recipient will need a special decoder script to turn the ^ caret notation escape sequences back to bytes. Don't use cat in the first place; mail user@domain < myfile is much better, or use something that handles arbitrary bytes properly. – cat Mar 9 '16 at 23:07
  • I agree. And my first example uses < redirection. I was trying to be complete though and a many people seem to use cat for testing. I guess I could expand on that. And optionally add things like uuencode and its more modern variants. – Hennes Mar 10 '16 at 6:56
  • @Hennes I tried writing html code to a file and then redirected but it's now working and even cat file ! mail also didn't work for me. Only echoing and then piping worked. Why is it so ? – learner1 Mar 12 '16 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.