-1

Using MAILTO where can i give the subject of the email without using echo Test | mail -s Test xyz@example.com because if i use like this for sender to a one user it is okay if i want to add multiple user mails then for every script i have to add the mail id's which will be not good when comapare to MAILTO command.

Right now i have the script to send the mail to a user and send the STDOUT AND STDERR to logs.txt.

MAILTO="xyz@example.com,abc@example.com,rer@example.com"

25 07 * * * /usr/local/bin/curator --dry-run --config /home/itadmin/.curator/curator.yml /home/itadmin/.curator/snapshot.yml 2>&1 | /usr/bin/tee -a /home/itadmin/.curator/logs.txt

What i want is that in the MAILTO command is it possible to subject to all the users listed in that command like this MAILTO="xyz@example.com,abc@example.com,rer@example.com?subject=Mail Tested"

Thanks

Private
  • 277
  • 1
  • 2
  • 9
  • 1
    You have asked a [number](https://serverfault.com/q/849033/37681) of cron related [questions](https://serverfault.com/q/849282/37681) now and I just want to point out that Cron is only a simple scheduler. If you need more advanced functionality than just *run a specific command at a specific time* usually it is better to instead encode that logic in a script and just let cron call that script. Then you can do useful stuff like for instance catching the exit code of a command and send different messages based on succes or failure, send log files as attachments instead of body text etc. – HBruijn May 11 '17 at 08:40

1 Answers1

1

MAILTO in crontab is not a command but an environment variable.

For example, in Cron by Paul Vixie, mailto variable is set to NULL if it's empty and to username, if not present. Then, the From:, To: and Subject: are hard-coded to the program in do_command.c:

358     /* if we are supposed to be mailing, MAILTO will
359      * be non-NULL.  only in this case should we set
360      * up the mail command and subjects and stuff...
361      */
362
363     if (mailto) {

375         fprintf(mail, "From: root (Cron Daemon)\n");
376         fprintf(mail, "To: %s\n", mailto);
377         fprintf(mail, "Subject: Cron <%s@%s> %s\n",
378             usernm, first_word(hostname, "."),
379             e->cmd);

Therefore, you can't use MAILTO for changing the Subject. Instead, you could pipe the output to an external command sending mail or use an intermediate file and send its contents.

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122