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" [email protected] < ./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
81.4k42 gold badges174 silver badges228 bronze badges
asked Feb 17, 2016 at 21:58
0

2 Answers 2

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 <[email protected]>
To: Customer <[email protected]>
Subject: You are awesome
Content-Type: text/html
<html>...</html>

Postfix will add missing but required headers like Date.

answered Feb 18, 2016 at 8:41
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.

answered Feb 17, 2016 at 22:15
4
  • 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. unix.stackexchange.com/questions/15405/… Commented Feb 17, 2016 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. Commented Feb 17, 2016 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. Commented Feb 17, 2016 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. Commented Feb 17, 2016 at 23:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.