Cron job mail change sender address

1

I have cron jobs setup in my website and they do pretty well but my problem is that the output sent as email by these cron jobs are delivered with 'from name' as Cron Daemon. Please is there any way I could change that to something more personal. Thanks

From comment from the OP:

The command line that runs my file is:

php -q /home/username/public_html/x_y_z.php. 

The output of the file (x_y_z_.php) is then sent as email by the cron job.

Onwa

Posted 2013-03-15T09:04:03.863

Reputation:

How do you send them? – Rob – 2013-03-15T09:05:38.837

The command line that runs my file is: php -q /home/username/public_html/x_y_z.php. The output of the file (x_y_z_.php) is then sent as email by the cron job. – None – 2013-03-15T10:09:11.800

Yes, how are you sending the email? Do you use sendmail? What command is run by cron? – terdon – 2013-03-15T14:29:29.333

Answers

1

Seems like there's a lot of bad info on this, but there are two options that can work without wrapper scripts.

First, on some systems you can set MAILFROM just like you do MAILTO. man 5 crontab will tell you if MAILFROM is available.

Failing that, the other option is to change the mailname file.

sudo vi /etc/mailname

Change it.

mlissner

Posted 2013-03-15T09:04:03.863

Reputation: 860

1

The "real name" of the cron user cannot be changed unless you have root access and you know which MTA is used to send these mails (sendmail, exim, postfix ?), and each MTA has its own way of assigning real names to unix users (cron is very likely running as user "cron").

Your best bet is to send the mails from your PHP script instead of relying on cron to do it.

A quick way to do it with little changes to your code :

<?php

ob_start();

/* >>> your existing php code goes there */

$output = ob_get_contents();
ob_end_clean();

/* the text to be mailed is now in variable $output */

mail("reciptient@host.com", "Mail from cron job" , $output, "From: Webmaster <webmaster@example.com>\r\nReply-To: webmaster@example.com");

?>

If the mail function somehow does not work in your PHP installation, you will have to use third partiy libraries, like PEAR::Mail.

wldsvc

Posted 2013-03-15T09:04:03.863

Reputation: 111

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

Posted 2013-03-15T09:04:03.863

Reputation: 519