There are many different versions of mail
around. When you go beyond mail -s subject to1@address1 to2@address2 <body
(for sending, that's all POSIX guarantees — and even -s
didn't exist in the old days), they tend to have different command line options. Adding an additional header isn't always easy.
With some mailx
implementations, e.g. from mailutils
on Ubuntu or Debian's bsd-mailx
, it's easy, because there's an option for that.
mailx -a 'Content-Type: text/html' -s "Subject" to@address <test.html
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"
With a general POSIX mailx
, I don't know how to get at headers.
If you're going to use any mail
or mailx
, keep in mind that
- This isn't portable even within a given Linux distribution. For example, both Ubuntu and Debian have several alternatives for
mail
and mailx
.
- When composing a message,
mail
and mailx
treats lines beginning with ~
as commands. If you pipe text into mail
, you need to arrange for this text not to contain lines beginning with ~
.
If you're going to install software anyway, you might as well install something more predictable than mail
/Mail
/mailx
. For example, mutt. With Mutt, you can supply most headers in the input with the -H
option, but not Content-Type
, which needs to be set via a mutt option.
mutt -e 'set content_type=text/html' -s 'hello' 'to@address' <test.html
Or you can invoke sendmail
directly. There are several versions of sendmail
out there, but they all support sendmail -t
to send a mail in the simplest fashion, reading the list of recipients from the mail. (I think they don't all support Bcc:
.) On most systems, sendmail
isn't in the usual $PATH
, it's in /usr/sbin
or /usr/lib
.
cat <<'EOF' - test.html | /usr/sbin/sendmail -t
To: to@address
Subject: hello
Content-Type: text/html
EOF
First some context:
I am using heirloom-mailx version as the one on the following thread:
https://serverfault.com/questions/136106/what-package-to-install-for-sending-emails-from-localhost-ubuntu
I am using Ubuntu 16.04 Xenial. Tried also on Ubuntu Server 16.04.
In order to send emails I am using the following function for sending mail using mailx (heirloom mailx in bash):
sendmail() {
#Sending Report Email
heirloom-mailx -a $2 -v -s ""$(echo -e "subject 3\nContent-Type: text/html")"" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://mail.mymailserver:port \
-S from="example@mymailserver.com" \
-S smtp-auth-user=example@mymailserver.com \
-S smtp-auth-password='password' \
-S ssl-verify=ignore \
$1 < body.html
}
Where $2 is the attachment and $1 is the destination.
Notes:
1. Files attached are printed inside the body as well but this could be useful to you if you wish just to send an html file with no attachments.
2. Using "-v" option prints verbose so you might get an issue with .mime.types that can be ignored. Remove the option if you don't want verbose on mailx.
3. You will still get the following in the body if you use the "-a" option:
This is a multi-part message in MIME format. --=-=fFPa7dLqoSF1TGj-YDc2k8bdvmjpix_4sKFT=-= Content-Type: text/plain; charset=US-ASCII Content-Disposition: inline
In this case I am attaching a plain text file.
Removing "-a $2" from the command and you are all set to print the html message.
So the final result would be:
sendmail() {
#Sending Report Email
heirloom-mailx -s ""$(echo -e "subject 3\nContent-Type: text/html")"" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://mail.mymailserver:port \
-S from="example@mymailserver.com" \
-S smtp-auth-user=example@mymailserver.com \
-S smtp-auth-password='password' \
-S ssl-verify=ignore \
$1 < body.html
}
Try it out and let me know. I have tested on my end and it works.
Best Answer
You need to make it a multipart/mixed message, boundaries and all.