changing cron's From: address in emails

Matthew Seaman m.seaman at infracaninophile.co.uk
Sat Oct 24 17:02:32 UTC 2009


Paul Schmehl wrote:
> --On Saturday, October 24, 2009 4:16 AM -0500 Matthew Seaman 
> <m.seaman at infracaninophile.co.uk> wrote:
> 
>> Kelly Martin wrote:
>>> Greetings, here's a simple question for the FreeBSD gurus out there. I
>>> have several servers running cron scripts daily for me, and they all
>>> send me e-mail with their output. Regardless of which server it is,
>>> each of these e-mails have the From: address looking exactly the same.
>>> They all say they are from the "Cron Daemon". Fine, but I'd like to
>>> know more clearly which server the cron output is from.
>>>
>>> How can I change the From: address of these emails to "Myserver Cron
>>> Daemon" instead? I know cron runs as the user, so it's not immediately
>>> obvious to me how to change the From: field. Already the subject line
>>> says something like "Cron <root at Myserver> ..." but this doesn't stand
>>> out enough for my tired eyes.
>>
>> Hmmm... that's actually quite tricky.  There's no facility within
>> cron(8) for changing the address it sends /from/, and as the bit you
>> want to change is technically a comment on the From: line, and not
>> the actual sender address (the bit in the <angle brackets>) all the
>> address rewriting-fu in sendmail won't really help.  Besides, root at ...
>> is listed as a member of the 'Exposed User' class: that is, addresses
>> that should be exempt from address rewriting, so you'ld also have to
>> modify that.
>>
>> Do you control the mail server where you read your e-mail?  Can you use
>> eg. procmail(1) as a delivery agent?  You should be able to match e-mails
>> from Cron and rewrite headers, or deliver cron e-mails into per-machine
>> mailboxes.  Something like this:
>>
>>    :0 h
>>    * From:.*Cron <root@\/[^\.]+
>>      $MATCH
>>
>> The other alternative is to re-write the cron scripts to send e-mail
>> themselves, rather than relying on cron(8) to capture their stdout/stderr
>> and e-mail it for you.  Here's a handy shell programming trick that can
>> make that easier.  Somewhere near the top of the script, you can add
>> something like this:
>>
>>    exec 2>&1 | /usr/sbin/sendmail -oi -t
>>    echo "From: Sender Name <sender at add.ress>
>>    echo "To: <recipient at some.where>
>>    echo "Subject: e-mail from cron job"
>>    echo ""
>>
>> Then everything you print out in the script will be captured as the body
>> of the e-mail and sent to the specified recipient.  You might get some
>> warnings about forgery in the mail headers if the userid the script runs
>> as is not the same as the 'From:' address.
>>
> 
> Why not just echo `hostname` as the first line of every script?  Isn't 
> that what he really wants to know?
> 
> Or echo "This script came from `hostname`"?

There is that, but I had assumed the idea was to be able to better
distinguish the different e-mails /before/ opening them up in the
MUA.

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
                                                  Kent, CT11 9PW

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20091024/709ced2c/signature.pgp


More information about the freebsd-questions mailing list