MAILFROM variable

Asked by Thomas Dejonghe

For personal reasons I needed a way to set the sender address for mails send by cron.

I read that some cron versions support a MAILFROM variable for this purpose (https://serverfault.com/questions/121121/how-to-change-from-field-for-emails-from-cron#437319).

I therefore wrote a patch for the current xenial cron package to support this variable.

diff --from-file cron-3.0pl1.orig cron-3.0pl1.orig/crontab.5 cron-patch/crontab.5
103c103,104
< owner of the crontab.
---
> owner of the crontab. If MAILFROM is defined (and non-empty), it will be used
> as the envelope sender address, otherwise, ''root'' will be used.
diff --from-file cron-3.0pl1.orig cron-3.0pl1.orig/do_command.c cron-patch/do_command.c
81a82,100
> /* Check for arguments */
> static char* check_env_argument(char *argument)
> {
> if (argument) {
> const char *end;
>
> /* These chars have to match those cron_popen()
> * uses to split the command string */
> argument += strspn(argument, " \t\n");
> end = argument + strcspn(argument, " \t\n");
> if (*argument == '-' || *end != '\0') {
> printf("Bad Mailto/Mailfrom karma.\n");
> log_it("CRON",getpid(),"error","bad mailto/mailfrom");
> argument = NULL;
> }
> }
> return argument;
> }
>
132c151
< char *usernm, *mailto;
---
> char *usernm, *mailto, *mailfrom;
155a175
> mailfrom = env_get("MAILFROM", e->envp);
158,171c178,180
< if (mailto) {
< const char *end;
<
< /* These chars have to match those cron_popen()
< * uses to split the command string */
< mailto += strspn(mailto, " \t\n");
< end = mailto + strcspn(mailto, " \t\n");
< if (*mailto == '-' || *end != '\0') {
< printf("Bad Mailto karma.\n");
< log_it("CRON",getpid(),"error","bad mailto");
< mailto = NULL;
< }
< }
<
---
> mailto = check_env_argument(mailto);
> mailfrom = check_env_argument(mailfrom);
>
532a542,545
> // get name of sender
> if (mailfrom && !*mailfrom)
> mailfrom = NULL;
>
561c574,578
< fprintf(mail, "From: root (Cron Daemon)\n");
---
> if (mailfrom) {
> fprintf(mail, "From: %s\n", mailfrom);
> } else {
> fprintf(mail, "From: root (Cron Daemon)\n");
> }

It has been tested on Ubuntu 16.04 in conjunction with the ssmtp package to send the mail.
Not setting MAILFROM, setting it to "" and setting a valid mail worked for my purposes.

I'll just leave this here

Question information

Language:
English Edit question
Status:
Expired
For:
Ubuntu cron Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.