The problem is that the value for $from in drupal_mail is merged into the $headers variable, along with some other defaults, before the code for hook_mail_alter runs:

  // To prevent e-mail from looking like spam, the addresses in the Sender and
  // Return-Path headers should have a domain authorized to use the originating
  // SMTP server.  Errors-To is redundant, but shouldn't hurt.
  $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  if ($default_from) {
    $defaults['From'] = $defaults['Reply-To'] = $defaults['Sender'] = $defaults['Return-Path'] = $defaults['Errors-To'] = $default_from;
  }
  if ($from) {
    $defaults['From'] = $defaults['Reply-To'] = $from;
  }
  $headers = array_merge($defaults, $headers);
  // Custom hook traversal to allow pass by reference
  foreach (module_implements('mail_alter') AS $module) {
    $function = $module .'_mail_alter';
    $function($mailkey, $to, $subject, $body, $from, $headers);
  }

I think that the $headers variable should be merged with the from defaults after the code for hook_mail_alter is run:

  // Custom hook traversal to allow pass by reference
  foreach (module_implements('mail_alter') AS $module) {
    $function = $module .'_mail_alter';
    $function($mailkey, $to, $subject, $body, $from, $headers);
  }
  // To prevent e-mail from looking like spam, the addresses in the Sender and
  // Return-Path headers should have a domain authorized to use the originating
  // SMTP server.  Errors-To is redundant, but shouldn't hurt.
  $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  if ($default_from) {
    $defaults['From'] = $defaults['Reply-To'] = $defaults['Sender'] = $defaults['Return-Path'] = $defaults['Errors-To'] = $default_from;
  }
  if ($from) {
    $defaults['From'] = $defaults['Reply-To'] = $from;
  }
  $headers = array_merge($defaults, $headers);
CommentFileSizeAuthor
common.inc__3.patch1.38 KBjsenich

Comments

drumm’s picture

Status: Needs review » Closed (won't fix)

The desired functionality is to let implementations of hook_mail_alter() to have free reign over the outgoing email. Merging headers afterwards would make it impossible to alter some headers.