Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

linux generic "mail" command cannot add custom headers?

1,736 views
Skip to first unread message

Nomen Nescio

unread,
Jul 5, 2012, 2:40:19 PM7/5/12
to
I want to add a custom header (e.g. "X-My-Custom-Header: my data") to
a message sent using the "mail" command.

I'm having a hard time seeing how this is done. No examples appear in
my web searches. The reference manual implies that one can hack a way
to do this by adding the line "~hX-My-Custom-Header: my data" to the
body of the message, and use the -I option to make a non-interactive
message appear interactive.

This approach is very sloppy, and more importantly, it doesn't work.
Some cryptic error appears.

What's the proper way to generate an e-mail from a script and add a
header field so that it can be filtered?

Chris Davies

unread,
Jul 5, 2012, 4:01:58 PM7/5/12
to
Nomen Nescio <nob...@dizum.com> wrote:
> I want to add a custom header (e.g. "X-My-Custom-Header: my data") to
> a message sent using the "mail" command.

> I'm having a hard time seeing how this is done.

On my Debian system the first option presented from "man mail" gives an
appropriate solution. (On my system "mail" is linked via /etc/alternatives
to bsd-mailx. Your system may be different.)

If you really need a custom header, the truly generic solution is to
create enough of the headers to make a message that can be fed directly
to /usr/lib/sendmail:

FROM=chris-...@roaima.co.uk
TO=doc...@tardis.galifrey.kasterborous.piscescetus.glx
(
echo "From: chris <$FROM>"
echo "To: the doctor <$TO>"
echo 'Subject: key please'
echo 'X-My-Custom-Header: my data'
echo
echo 'Please can I have a key to your tardis'
) | /usr/lib/sendmail -f "$FROM" "$TO"

Chris

Eli the Bearded

unread,
Jul 7, 2012, 1:42:41 AM7/7/12
to
In comp.mail.misc, Chris Davies <ch...@roaima.co.uk> wrote:
> Nomen Nescio <nob...@dizum.com> wrote:
> > I want to add a custom header (e.g. "X-My-Custom-Header: my data") to
> > a message sent using the "mail" command.
> > I'm having a hard time seeing how this is done.

The "mail" program could be a lot of things. Traditional "mail" might
be able to do it via crazy ~ commands, but "mailx" or "nail" has
likely fixed those issues (they can be used for security exploits in
some situations).

> If you really need a custom header, the truly generic solution is to
> create enough of the headers to make a message that can be fed directly
> to /usr/lib/sendmail:
> FROM=chris-...@roaima.co.uk
> TO=doc...@tardis.galifrey.kasterborous.piscescetus.glx
> (
...

Someone has already written a nice shell script that does this for
you. It's called "Rnmail" and it ships with trn. Just run it with
no arguments and it will prompt for To: and Subject: then dump you
in $EDITOR with fully modifiable headers.

> ) | /usr/lib/sendmail -f "$FROM" "$TO"

Although, as shipped Rnmail does not use -f to set envelope "From ".
It does use -t to read the recipients from the message, however.

I've hacked my version to add -f in, if I set From: in the message.
I use formail (from the procmail package) to extract the bare address,
since envelope "From " does not include a name field.

Here's what the "sendmail" state of the case statement looks like now:

case $mailer in
*sendmail)
# formail -X From: extract from header
# formail -rcx To: create a reply header, and then
# extract the value of the To: header
# tr -d ' ' remove whitespace
from=`formail -X From: <$tmpart |formail -rcx To: |tr -d ' '`
case "X$from" in
X) fromarg= ;; # no from available
X*) fromarg='-f "'"$from"'"' ;;
esac
echo "$mailer $fromarg -t <$tmpart"
$mailer $fromarg -t <$tmpart
;;

Elijah
------
has been using Pnews and Rnmail directly for years

Anonymous

unread,
Jul 13, 2012, 6:42:58 AM7/13/12
to
Chris Davies <chris-...@roaima.co.uk> wrote:
>
> On my Debian system the first option presented from "man mail" gives
> an appropriate solution. (On my system "mail" is linked via
> /etc/alternatives to bsd-mailx. Your system may be different.)

I checked other systems, and noticed the "-a" option. But that's not
available on the (redhat) system I need it on.

> If you really need a custom header, the truly generic solution is to
> create enough of the headers to make a message that can be fed
> directly to /usr/lib/sendmail:

Thanks. That's what I ended up doing.

0 new messages