Change sender domain of system generated mail

Hello,
My hostname is a subdomain “hcp.mydomain.co.in”
But my mail domain is main domain “mydomain.co.in”
Now when I get “notification/system generated mail” about any updates then it comes from “[email protected]” so I didn’t get it, but then I change whitelable from settings to “[email protected]” and it’s work fine.
But when I enable cron notification it’s generated from “[email protected]” so I didn’t get it I want to change it “[email protected]” means change to main mail domain instead of sub domain.
Is it possible then please tell me the process to change it.
Also if there any way my system generate mail for any reason can I change it to main mail domain instead of sub domain ([email protected] to [email protected])

Hi @Chrisgayle,

If you are using Ubuntu 22.04, crontab allows using MAILFROM variable so you could add that variable to the users crontab:

Example for user admin:

crontab -e -u admin

At top of the file add: MAILFROM="[email protected]"

If you don’t want to do that per user, or you are not using Ubuntu 22.04, you could try to modify /etc/mailname and replace hostname hcp.mydomain.co.in by mydomain.co.in

Note: I didn’t test these “fixes”

2 Likes

Thanks sir
I select the first option and add mailfrom variable here crontab -e -u admin and save it. But after some time it automatically removed for some reason, it doesn’t stay permanent. I also check “var/spool/cron/crontabs” it shows mailfrom in that time but removed after logout/sometime.

Ok I find out when the mailfrom variable removed when I disable and enable again notification in Hestia panel crons then the MAILFROM removed from file.

  GNU nano 6.2               /tmp/crontab.XTXMji/crontab
[email protected]
[email protected]
CONTENT_TYPE="text/plain; charset=utf-8"
*/2 * * * * sudo /usr/local/hestia/bin/v-update-sys-queue restart
10 00 * * * sudo /usr/local/hestia/bin/v-update-sys-queue daily
15 02 * * * sudo /usr/local/hestia/bin/v-update-sys-queue disk
10 00 * * * sudo /usr/local/hestia/bin/v-update-sys-queue traffic
30 03 * * * sudo /usr/local/hestia/bin/v-update-sys-queue webstats
*/5 * * * * sudo /usr/local/hestia/bin/v-update-sys-queue backup
10 05 * * * sudo /usr/local/hestia/bin/v-backup-users
20 00 * * * sudo /usr/local/hestia/bin/v-update-user-stats
*/5 * * * * sudo /usr/local/hestia/bin/v-update-sys-rrd
45 2 * * * sudo /usr/local/hestia/bin/v-update-letsencrypt-ssl
31 2 * * * sudo /usr/local/hestia/bin/v-update-sys-hestia-all

And for second option I didn’t find my hostname in it.

root@hcp:/etc# cat mailname
ip-172-31-9-168.ap-south-1.compute.internal

v-rebuild-cronjobs over write it …

1 Like

Thanks sir
After

v-rebuild-cron-jobs admin

It does opposite it remove the MAILFROM variable.
And I also note that MAILFROM variable is only removed when I disable and enable notification in hestiapanel in cron tab. Beside that it stay same even after reboot.

v-rebuild-cronjobs

Command not found

That script uses sync_cron_jobs function from /usr/local/hestia/func/main.sh and that is the one overwriting the crontab conf file.

# Sync cronjobs with system cron
sync_cron_jobs() {
        source_conf "$USER_DATA/user.conf"
        if [ -e "/var/spool/cron/crontabs" ]; then
                crontab="/var/spool/cron/crontabs/$user"
        else
                crontab="/var/spool/cron/$user"
        fi

        # remove file if exists
        if [ -e "$crontab" ]; then
                rm -f $crontab
        fi

        # touch new crontab file
        touch $crontab

        if [ "$CRON_REPORTS" = 'yes' ]; then
                echo "MAILTO=$CONTACT" > $crontab
                echo 'CONTENT_TYPE="text/plain; charset=utf-8"' >> $crontab
        else
                echo 'MAILTO=""' > $crontab
        fi

        while read line; do
                parse_object_kv_list "$line"
                if [ "$SUSPENDED" = 'no' ]; then
                        echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" \
                                | sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" \
                                        >> $crontab
                fi
        done < $USER_DATA/cron.conf
        chown $user:$user $crontab
        chmod 600 $crontab
}

You could modify this function so MAILFROM is added but next Hestia upgrade will overwrite it so I can’t recommend it.

If you really want to send cron outputs via mail with a specific from, then you can add a pipe to the cron job and use mail command to send the output to the right recipienmt with the right from.

Let’s say your cron job launches this script:

echo "This is a test"

Then you should edit the cron job and add something like this:

echo "This is a test" 2>&1 | mail -s "Cron test output" -r "Cron outputs <[email protected]>" -- [email protected]
1 Like

Thanks @sahsanu sir
Your previous solution is work perfectly, unless I disable and enable cron notification. So right now I use tthis solution.

And I add pipe to cronjob in future too.
Thanks again sir @eris and @sahsanu for helping me this quickly, for my selfish request.
You all are awesome :+1:

2 Likes

See about Exim rewriting headers. You could do a rewrite for all the outgoing emails to change the from address and not only on cron.

1 Like

Thanks @schiwe I will check that too. Did you provide some guidance if possible.
Thanks

I don’t have a guide for this specifically but basically you can create a file inside /etc/exim4/conf.d/rewrite/00-my-rewrite-header
And use the basic rewriting of exim.

[email protected] [email protected] SFfrs

The flags have the following meanings:

  1. S - Do all rewriting at SMTP time. This means that all of the following flags are done immediately as the data they apply to is coming in, not delayed until later.
  2. F - Rewrite the envelope from field.
  3. f - Rewrite the From: header field.
  4. r - Rewrite the Reply-To: header field.
  5. s - Rewrite the Sender-To: header field.

Read Chapter 31 of the Exim Specification for more detail on message rewriting.

Credit for the answer on ServerFault: email - Exim: Change sender address when sending mails out of local network - Server Fault

2 Likes

Thanks @schiwe I will definitely try it.

If you want to try it (be careful) don’t add a dir and a conf file inside conf.d, exim won’t use it (it doesnt use split conf), instead, edit main conf file /etc/exim4/exim4.conf.template and add your rewrite rules at the end of file:

######################################################################
#                      REWRITE CONFIGURATION                         #
######################################################################
begin rewrite
[Here your rules]


######################################################################

2 Likes

Thanks @sahsanu sir, I honestly going to add a file inside conf.d, now i am not, you saved me by showing me actual file.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.