6

I'm trying to send an HTML email from our CentOS server to users. In the long run I'll be building a COBOL program that runs the command to send reports to the user.

Here are a few details.

  • CentOS 6.4
  • MAU : Mailx
  • MTA : Postfix 2.6.6
  • Postfix is running a relay through an exchange server.

As for what commands I've tried running. The one I've seen the most today has been the following.

$ mailx -a 'Content-Type: text/html' -s "Command Line Test" andyv@example.com < ./bodytext.html

After running the shown command, I get an error saying "Content-Type: text/html: No such file or directory". I'm pretty sure that after a certain update they stopped allowing -a as a flag for Content-Type designation.

I've also tried adding the 'Content-Type: text/html' to the actual bodytext.html file as the very first line. I'm kind of just at a loss for what I can do to send the HTML email. Some of the sources I've found say that mailx and postfix can't properly send HTML emails. Hopefully that's not the case, but if it is than I'd like to know what your take on other MAU and MTA technologies?

Anthon
  • 79,293
AndyV
  • 63
  • 1
  • 1
  • 5

2 Answers2

2

Since you seem to have full control over the generated text file, the simplest and probably most portable way would be to involve /usr/sbin/sendmail directly.

/usr/sbin/sendmail -t < complete-mail.txt

This would require you to add all important headers yourself (From, To, Subject, and Content-Type). The file should look like this:

From: Company <noreply@company.example>
To: Customer <name@customer.example>
Subject: You are awesome
Content-Type: text/html

<html>…</html>

Postfix will add missing but required headers like Date.

tarleb
  • 2,077
1

Did you even read the manual for mailx, especially the -a option?

-a file Attach the given file to the message.

From Gilles answer, as CentOS is using this version of mailx:

With the Heirloom mailx, there's no convenient way. One possibility to insert arbitrary headers is to set editheaders=1 and use an external editor (which can be a script).

## Prepare a temporary script that will serve as an editor.
## This script will be passed to ed.
temp_script=$(mktemp)
cat <<'EOF' >>"$temp_script"
1a
Content-Type: text/html
.
$r test.html
w
q
EOF
## Call mailx, and tell it to invoke the editor script
EDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <<EOF
~e
.
EOF
rm -f "$temp_script"

Postfix certainly can send HTML mails.

Jakuje
  • 21,357
  • Yes, I did read the manual and noticed that the -a option is for an attachment. I've also used the -a flag for attachments properly across multiple jobs. However, that was what I was seeing as different answers on even this own forums. Here's an example where the highest rated answer uses the same format. http://unix.stackexchange.com/questions/15405/how-do-i-send-html-email-using-linux-mail-command – AndyV Feb 17 '16 at 22:23
  • There is difference between bsd-mailx and mailx provided by CentOS (Heirloom mailx as mentioned in your linked post). You need to read all the posts and manual pages on your system, since they might use different version then other people refer. – Jakuje Feb 17 '16 at 22:29
  • I was looking at a dead.letter file after trying a solution to my task. It does say that the User-Agent is Heirloom mailx 12.4. It also mentions that the Content-Type is text/plain; charset=us-ascii. I'm sorry for such a noob question, but I do appreciate both your answer and Jakuje's answer. – AndyV Feb 17 '16 at 22:54
  • Yes, you should be able to set up the mailx content type as described in the linked question, even if it is quite ugly for Heirloom one. I certainly didn't want you to discourage you, from asking questions. The mess between Unix/Linux/BSD/whatever tool might be confusing even if they are called the same way. – Jakuje Feb 17 '16 at 23:06