42

I use remote SMTP via nullmailer and it requires set From field to the specific name, but cron set it as root@my.sweet.server.com.

How could I change it to something like me@ya.ru?

Paul
  • 2,755
  • 6
  • 24
  • 35
Alexander Artemenko
  • 1,323
  • 3
  • 13
  • 13

13 Answers13

48

Modern versions of cron do accept "MAILFROM=..." in the crontab format. I suggest that you try "man 5 crontab". If it mentions MAILFROM, your version should support it. The phrase to look for is towards the end of the paragraph discussing MAILTO, and should be something like this:

If MAILFROM is defined (and non-empty), it will be used as the envelope sender address, otherwise, ''root'' will be used.

Jon Green
  • 581
  • 1
  • 4
  • 4
17

I don't think you can change the FROM address, (someone should add a MAILFROM option).

You can do something like this though to achieve a similar result:

* * * * * /path/to/script 2>&1 | mail -s "Output of /path/to/script" toaddress@example.com -- -r "fromaddress@example.com" -F"Full Name of sender"

All output is piped to the mail command so the MAILTO variable isn't used at all.

The to address would need to be set but you may be able to use $MAILTO variable. The -- sets the rest of the options to be sendmail options so you can use the -r and and -F options.

-s is the subject

-r is the reply address

-F is the Full name of the sender (makes it look nice in email clients)

Richard Holloway
  • 7,256
  • 2
  • 24
  • 30
11

/etc/mailname contains the domain name part of the FROM address. If /etc/mailname contains 'somecompany.com' then cron running for root would have sender as root@somecompany.com

user171601
  • 111
  • 1
  • 2
  • 3
    Some info here: https://wiki.debian.org/EtcMailName - does not say if nullmailer use it. I use sendmail and it did not seem to work for it. – Zitrax Jan 30 '15 at 14:24
5

You can set the nullmailer from address via environment variables or command line. The command line arguments are -f and -F for sender address and full name respectively.

Usually you can set environment variables in the crontab.

NULLMAILER_USER=webmaster
NULLMAILER_HOST=host.example.com
NULLMAILER_NAME="Mr Cron"

5 0 * * * /usr/local/bin/daily.sh
Lachlan Roche
  • 151
  • 1
  • 3
5

For me, the easiest way to change the from address on a system, is to create a ~/.mailrc file with contents like this:

set name="My Full Name"
set from="myrealemail@example.com"

Any of the mail commands that run as my user, now use these settings.

Niels de Vos
  • 51
  • 1
  • 1
1

Here are few things you can apply to change your sender domain:

Edit this file: /etc/mailname and change to:
    example.org
sudo postconf -e 'myhostname= example.org'
sudo systemctl restart postfix
Tarik
  • 111
  • 4
0

In my case, I followed these steps to resolve the issue:

  1. edited the file /etc/postfix/main.cf

set the value of this variable:

mydomain = my-domain-name.xxx

uncomment this line:

#myorigin = $myhostname

  1. restart postfix using this command; systemctl restart postfix
Prasad DLV
  • 101
  • 1
0

This is an old question, but it remains valid. There is no easy solution for Debian (and possibly others) because MAILFROM remains unsupported. This has bugged me for some time, as I receive cron generated mail from a number of different servers. But most of them are on subdomains of the same domain, and most of the subdomains do not support mail. Hence I'm forced to configure postfix to use the domain as the origin, and all the mail seems to have come from the same place.

I've finally got round to creating a solution, by adapting cronic to create mycronic. This does much the same as cronic, except it sends its output directly to mail and suppresses all output to cron. It assumes that the server is configured so as to give correct answers to different requests to hostname. For example:

hostname -f => webserver.example.com
hostname -d => example.com
hostname => webserver

This is achieved by having just webserver in /etc/hostname and having a line in /etc/hosts:

127.0.0.1     webserver.example.com    webserver

Postfix is configured to have myorigin set to mydomain. The script also relies on the fact that I have postfix configured to redirect root@example.com to my own mail box. The actual script is:

#!/bin/bash

# MyCronic v1 - cron job report wrapper to send results directly to mail
# Copyright 2020 Martin Brampton. No rights reserved, whatsoever.
# Based on Cronic v3 - cron job report wrapper
# Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/

set -eu

DOMAIN=`hostname -d`
FQDN=`hostname -f`

RECIPIENT="root@$DOMAIN"

MAILER=`which mail`

TMP=$(mktemp -d)
OUT=$TMP/cronic.out
ERR=$TMP/cronic.err
TRACE=$TMP/cronic.trace

set +e
"$@" >$OUT 2>$TRACE

RESULT=$?
set -e

PATTERN="^${PS4:0:1}\\+${PS4:1}"
if grep -aq "$PATTERN" $TRACE; then
    ! grep -av "$PATTERN" $TRACE > $ERR
else
    ERR=$TRACE
fi

if [ $RESULT -ne 0 -o -s "$ERR" ]; then
        mailtext="
My cronic detected failure or error output for the command:
$@

RESULT CODE: $RESULT

ERROR OUTPUT:
$(< $ERR)

STANDARD OUTPUT:
$(< $OUT)
"

        if [ $TRACE != $ERR ]; then
        $mailtext="
$mailtext

TRACE-ERROR OUTPUT:
$(< $TRACE)
"
        fi

        $MAILER -s "My Cronic detected a failure on $FQDN" -aFrom:MyCronic\<$HOSTNAME@$DOMAIN\> $RECIPIENT <<< "$mailtext"

fi

rm -rf "$TMP"

You may need to adapt it a bit to suit your own purposes, but it should work for most situations with minimal alteration.

mbrampton
  • 301
  • 3
  • 12
0

An alternative solution is to use msmtp-mta as system mailer with the following configuration in order to override the From header:

from x@y.com
set_from_header on
piec
  • 113
  • 4
0

See this question if using Exim:

exim: Rewrite "From" header to envelope "FROM"

It should set the "From" address to the cronjob owner's. You can replace $header_from: with your custom address if you'd like to hard-code it to something else.

Vladimir Panteleev
  • 1,705
  • 4
  • 20
  • 34
0

Another simple alternate is to use mutt,

  • create a .muttrc in the home directory of the user that runs cron with the following

    set realname="Joe User" set from="user@host" set use_from=yes

  • Run a script with mutt command in it or pipe the cron command to mutt from to send email.

Before mutt sends and email, it will set the From header from the .muttrc file.

Chida
  • 2,471
  • 1
  • 16
  • 29
0

I had to change /etc/mail/sendmail.cf and /etc/mail/sendmail.mc, because /etc/mailname wasn't used. It only changes the from domain though, not the user.

Rudie
  • 335
  • 1
  • 2
  • 13
-2

it is mailed from the user@domain -- the user is the login name the cron is running under -- so you'd have to create a user 'me' -- and run the cron job as that user.

Then to change the domain, there are different possibilities -- it could be that you need to change the hosts file entry (or, as on my machine, when I was configuring this -- an ubuntu box) -- change /etc/mailname -- to be the domain you want it to come from.

Kem Mason
  • 22
  • 1
  • 2
    This isn't actually true--at least not in many cases. On my system, for instance, it is only sent from 'user', not 'user@domain'. It is the responsibility of the MTA to add the local domain name. This is significant in some cases (such as mine) where my MTA (for reasons too complicated and boring to describe) is not adding the domain name. – Flimzy Dec 24 '12 at 21:00