- /* Generate mailbox delimiter */
- mbox_delimiter = expand_string(US"From ${sender_address} ${tod_bsdinbox}\n");
- if (mbox_delimiter != NULL) {
- if (mbox_delimiter[0] != 0) {
- i = fwrite(mbox_delimiter, 1, Ustrlen(mbox_delimiter), mbox_file);
- if (i != Ustrlen(mbox_delimiter)) {
- debug_printf("error/short write on writing in: %s", mbox_path);
- (void)fclose(mbox_file);
- return NULL;
- };
- };
- };
- /* Generate X-Envelope-From header */
- envelope_from = expand_string(US"${sender_address}");
- if (envelope_from != NULL) {
- if (envelope_from[0] != 0) {
- uschar *my_envelope_from;
- my_envelope_from = string_sprintf("X-Envelope-From: <%s>\n", envelope_from);
- i = fwrite(my_envelope_from, 1, Ustrlen(my_envelope_from), mbox_file);
- if (i != Ustrlen(my_envelope_from)) {
- debug_printf("error/short write on writing in: %s", mbox_path);
- (void)fclose(mbox_file);
- return NULL;
- };
- };
- };
- /* Generate X-Envelope-To header */
- envelope_to = expand_string(US"${if def:received_for{$received_for}}");
- if (envelope_to != NULL) {
- if (envelope_to[0] != 0) {
- uschar *my_envelope_to;
- my_envelope_to = string_sprintf("X-Envelope-To: <%s>\n", envelope_to);
- i = fwrite(my_envelope_to, 1, Ustrlen(my_envelope_to), mbox_file);
- if (i != Ustrlen(my_envelope_to)) {
- debug_printf("error/short write on writing in: %s", mbox_path);
- (void)fclose(mbox_file);
- return NULL;
- };
+ /* Generate mailbox headers. The $received_for variable is (up to at least
+ Exim 4.64) never set here, because it is only set when expanding the
+ contents of the Received: header line. However, the code below will use it
+ if it should become available in future. */
+
+ temp_string = expand_string(
+ US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n"
+ "${if def:sender_address{X-Envelope-From: <${sender_address}>\n}}"
+ "${if def:received_for{X-Envelope-To: <${received_for}>\n}}");
+
+ if (temp_string != NULL) {
+ i = fwrite(temp_string, Ustrlen(temp_string), 1, mbox_file);
+ if (i != 1) {
+ log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
+ mailbox headers to %s", mbox_path);
+ goto OUT;