Modifying the $from variable in hook_mail_alter does not change the from address

jsenich - June 26, 2007 - 20:23
Project:Drupal
Version:5.x-dev
Component:base system
Category:bug report
Priority:critical
Assigned:Unassigned
Status:won't fix
Description

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);

AttachmentSize
common.inc__3.patch1.38 KB

#1

drumm - June 27, 2007 - 04:07
Status:patch (code needs review)» 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.

 
 

Drupal is a registered trademark of Dries Buytaert.