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" fred@example.com

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" fred@example.com
  • Why should a perfectly valid Super User question with an accepted answer be moved to another SE site? – BinaryMisfit Feb 12 '11 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. – CW Holeman II Feb 13 '11 at 5:59
5

I've seen it done:

mail -a "Content-type: text/html;" -s Test address@example.com < mail_html.html

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

  • 2
    My mail doesn't have -a, could you be thinking of mutt, which does support it? – Rich Homolka Feb 8 '11 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. – grawity Feb 8 '11 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.) – grawity Feb 8 '11 at 19:42
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.

    rcpt=joe@example.com
    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"
    

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.