How to Send HTML Email from Linux Command Line (mail/mutt examples)


2 views

When sending emails via Linux command line using basic tools like mail, the content gets treated as plain text by default. This means any HTML formatting in your source file gets rendered as literal tags rather than being interpreted as markup.


# What you tried (doesn't work for HTML)
mail -s "Test Email" recipient@domain.com < webpage.html

To properly send HTML email from command line, you need:

  • Content-Type header set to text/html
  • Proper MIME formatting
  • Either mailx with HTML support or alternative tools

For RedHat systems with mailx installed:


echo "<h1>Header</h1><p>This is <b>HTML</b> content</p>" | \
mailx -s "$(echo -e 'Test Email\nContent-Type: text/html')" recipient@domain.com

Or with an HTML file:


mailx -a "Content-Type: text/html" -s "Test Email" recipient@domain.com < webpage.html

For more advanced formatting, install mutt:


sudo yum install mutt
mutt -e "set content_type=text/html" -s "Test Email" recipient@domain.com < webpage.html

For inline images and richer content:


mutt -e "my_hdr Content-Type: text/html" -s "HTML Email" recipient@domain.com \
-a image1.jpg image2.png < email_template.html

For complete control over headers:


(
echo "From: sender@domain.com"
echo "To: recipient@domain.com"
echo "Subject: Test HTML Email"
echo "Content-Type: text/html"
echo
cat webpage.html
) | sendmail -t

Before sending to actual recipients, test with:


# View raw output first
cat your_email.html | less

# Send to yourself first
mutt -e "set content_type=text/html" -s "TEST" your_email@domain.com < your_email.html

For complex cases, consider Python's smtplib:


#!/usr/bin/python3
import smtplib
from email.mime.text import MIMEText

with open('webpage.html') as f:
    html_content = f.read()

msg = MIMEText(html_content, 'html')
msg['Subject'] = 'Test HTML Email'
msg['From'] = 'sender@domain.com'
msg['To'] = 'recipient@domain.com'

s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

When using the basic mail command in Linux, emails are sent as plain text by default. This causes HTML tags to be displayed literally rather than being rendered as formatted content. The recipient sees something like:

<html>
<body>
<h1>Heading</h1>
<p>This is <b>bold</b> text</p>
</body>
</html>

To send proper HTML emails that render correctly in clients like Outlook or Gmail, you need to:

  1. Set the correct MIME type headers
  2. Include both HTML and plaintext alternatives
  3. Use proper email headers

The modern mailx command (often symlinked to mail) supports MIME types:

echo "This is the plaintext fallback" | mailx -a "Content-Type: text/html" -s "HTML Test" user@example.com < webpage.html

The mutt email client provides better HTML support:

mutt -e "set content_type=text/html" -s "HTML Email" user@example.com < webpage.html

For more control, create a proper email with both HTML and text alternatives:

mutt -e "set content_type=text/html" -s "Dual Format Email" user@example.com << EOF
This is the plaintext version that shows in basic email clients.



HTML Version

This appears in rich text email clients

EOF

For maximum control, craft the entire email including headers:

(
echo "From: sender@example.com"
echo "To: recipient@example.com"
echo "Subject: HTML Email Test"
echo "Content-Type: text/html"
echo "MIME-Version: 1.0"
echo ""
cat webpage.html
) | sendmail -t

To include inline images, you'll need to create a multipart email:

boundary="BOUNDARY_$(date +%s)"

(
echo "From: sender@example.com"
echo "To: recipient@example.com"
echo "Subject: Email with Images"
echo "Content-Type: multipart/related; boundary=\"$boundary\""
echo "MIME-Version: 1.0"
echo ""
echo "--$boundary"
echo "Content-Type: text/html"
echo ""
echo ""
echo "

Image Example

" echo "" echo "" echo "" echo "--$boundary" echo "Content-Type: image/png; name=\"logo.png\"" echo "Content-Transfer-Encoding: base64" echo "Content-ID: " echo "Content-Disposition: inline; filename=\"logo.png\"" echo "" base64 logo.png echo "--$boundary--" ) | sendmail -t
  • Always test with both HTML and plaintext email clients
  • Check your email headers with mutt -e "set verbose=yes"
  • For corporate environments, ensure your SMTP server allows HTML emails
  • Consider using wkhtmltopdf to convert HTML to PDF if formatting is critical