3

Currently I send an email from the Linux command like this:

echo "Here is the link: file:///S:/some_doc.html" | mail -s "Here is some_doc.html" [email protected]

In some email clients the "file:///S:/some_doc.html" in the message body becomes a link that the user can click on but in other clients it is presented as plain text requiring the user to copy and paste the string into a web browser. How do I modify the command to make the message be HTML so that the email client will know how to handle the link?

This gets the link into the message but it is still treated like text not HTML:

echo "Here is the link to: <a href='file:///S:/some_doc.html'>some_doc.html</a>" | mail -s "Here is some_doc.html" [email protected]
2
  • Why should a perfectly valid Super User question with an accepted answer be moved to another SE site? Feb 12, 2011 at 19:41
  • It is a Linux specific question which now that there is a SE site for Linux it would be the natural place that someone asking this question would go. If the question was regarding the distinction between text and HTML in email rather than about a "Linux command" then it would be better located here. Feb 13, 2011 at 5:59

2 Answers 2

5

I've seen it done:

mail -a "Content-type: text/html;" -s Test [email protected] < mail_html.html

I should have mentioned, I tested this on Ubuntu Server 10.04_x64 with Gnu Mail Utils -> Postfix

4
  • 2
    My mail doesn't have -a, could you be thinking of mutt, which does support it? Feb 8, 2011 at 19:38
  • A better way would be to create a MIME multipart/alternative message with text/plain and text/html parts containing identical text, instead of just sending HTML directly. Feb 8, 2011 at 19:40
  • 1
    @Rich: It's there in mail of GNU Mailutils. You probably have the more traditional BSD mailx. (Beware: mail of Heirloom mailx and mutt use -a for something entirely different – attaching a file.) Feb 8, 2011 at 19:42
  • not working in centos 8
    – MaXi32
    Jul 7, 2020 at 23:07
1

Message:

  • The best way is to create a MIME Content-Type: multipart/alternative message with text/plain and text/html parts.

  • For the lazy, Content-Type: text/html (with HTML straight in message body) will do.

    ...but remember that there are mail clients that are unable to display HTML.

  • Whichever you choose, add a charset=utf-8 (as in Content-Type: text/html; charset=utf-8) to the type header. (Any non-Unicode charset (except perhaps us-ascii and iso-8859-1) is evil.)

Sending:

  • Nathaniel suggested mail -a "Content-Type: text/html", but this only works with GNU Mailutils.

  • A more reliable way is to pipe the message sendmail, but you will have to construct all headers on your own.

    [email protected]
    echo -e "From: <$USER>\nTo: <$rcpt>\nDate: $(date "+%a, %d %b %Y %H:%M:%S %z")\nContent-Type: text/html; charset=utf-8\n\n<!DOCTYPE html>\n<p>Here is the link to: <a href=\"file:///S:/some_doc.html\">some_doc.html</a>" | sendmail -i "$rcpt"
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .