Send some text to mail from UNIX such that the text will appear in some color in the body of mail

4

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.

learner1

Posted 2016-03-09T05:11:21.160

Reputation: 155

1This 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 – 2016-03-09T16:55:22.890

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 – 2016-03-09T18:07:42.277

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 – 2016-03-10T02:50:38.053

Answers

14

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

<html>
<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.

davidgo

Posted 2016-03-09T05:11:21.160

Reputation: 49 152

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 – 2016-03-09T07:50:30.097

<font>? Is the email world lagging that much behind or should you just be using css in one form or another? – Jasper – 2016-03-09T12:13:32.217

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 – 2016-03-09T12:35:08.833

1Please, 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 – 2016-03-09T12:38:39.090

@IsmaelMiguel Oh wow. Thanks for educating me. – Jasper – 2016-03-09T14:59:56.830

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 – 2016-03-09T15:18:13.180

It worked :) @davidgo. But when I use mail command instead of sendmail it's not working. And how is Subjet taken as email subject ? I didn't understand. Can you please clarify ? – learner1 – 2016-03-12T03:43:35.733

the mail command does not usually allow you to inject headers - you need to inject the content-type header, so thats why I used sendmail rather then mail. I'm not sure I understand what you are talking about with the "Subject taken as mail subject" - if you look at my command the second line of output includes the subject header - you would replace the words "My Subject" with whatever the subject is. Emails are broken up into the header (which includes from, to, subject, content-type and other stuff, seperated by a blank line - the rest is the body. – davidgo – 2016-03-12T06:17:10.880

7

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.

Hennes

Posted 2016-03-09T05:11:21.160

Reputation: 60 739

1Do 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 – 2016-03-09T23:07:53.497

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 – 2016-03-10T06:56:38.833

@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 – 2016-03-12T03:46:24.127