Today a customer called me and asked why it is, that in their MS Outlook mails from their (drupal) website are marked as something like "this mail is sent to foo@customer.com IN BEHALF OF bar@customer.com".
After some investigations I figured out that in the mail headers the recipient is written nicely by webform (like To: foo@customer.com). But there is also a field Sender: bar@customer.com with the main website e-mail address (the one from /admin/settings/site-information), what is causing the confusion at my customers staff.
Maybe it's cool to have this Sender hader-information in webform mails. But is there a way to switch it off on this website of my customer?

I have not read through the sourcecode of webform yet. Maybe there is some obvious explanation for this header-field....
...do you know?

Comments

quicksketch’s picture

Using the site e-mail address is the default when new webforms are created. You just need to edit the webform node, change the sender e-mail address to "Custom" and enter a different e-mail address you'd like the e-mails to be sent from.

guedressel’s picture

I'm not talking about the Sender-field in the edit-form. I'm talking about the "Sender: " header in the mail text.

Here is a mail header from a generated mail:
visitor@domain.com is the address the visitor entered in the webform
foo@customer.com is the recipient address entered in the edit-form of the webform
bar@customer.com is the site-mail entered in the site-information form

Return-Path: visitor@domain.com
Received: from mail.domain.com (127.0.0.1) by mail.server.com with LMTP; Mon, 11 May 2009 08:02:10 +0200 (CEST)
To: foo@customer.com
Subject: Webform
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal Webform (PHP/5.2.0-8+etch13)
Errors-To: bar@customer.com
Sender: bar@customer.com
Reply-To: "visitor" <visitor@domain.com>
From: "visitor" <visitor@domain.com>
Message-ID: <xxx@some@id@xxx>
Date: Mon, 11 May 2009 08:02:05 +0200 (CEST)

Submitted on 05/11/2009 - 07:01

Submitted by anonymous user: [127.0.0.1]

TrickerTreater’s picture

I've had the same problem. I always found Outlook & Entourage to be the culprits. I'd love to know the solution.

quicksketch’s picture

It looks like Drupal core is responsible for these headers, I know that Webform isn't adding them. In includes/mail.inc around line 104, there's this bit of code:

  if ($default_from) {
    // 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.
    $headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from;
  }

So basically, it's going to be forced onto any e-mails sent by Webform. The spam argument is valid though, since Drupal is sending the e-mail from it's server, giving e-mails a different Sender could cause e-mails to be marked as spam on the receiving end.

If you really want to get rid of these headers, you can implement hook_mail_alter() in a custom module and remove the headers from all e-mails that Drupal sends out:

function mymodule_mail_alter(&$message) {
  unset($message['headers']['Sender']);
  unset($message['headers']['Errors-To']);
}
jwinton’s picture

Just wanted to note that I was having this problem in Lotus Notes and the custom module solution worked great. Thanks, quicksketch!

quicksketch’s picture

Status: Active » Closed (fixed)
Marko B’s picture

Version: » 7.x-3.9

I see this is still the case with drupal 7 and webform and other mail sending modules. Is there some other way to fix this. No module or thinking in drupal core dev team this isn't way to do this (limit this)?

Maybe using http://drupal.org/project/smtp can help on this, haven't tried it yet.

Also, will try your custom module, but wondering is this the right way? unseting something that is usually in emails could also make it ready to be spam flaged?

Marko B’s picture

Marko B’s picture

And here is a module that should help on this issue http://drupal.org/project/mail_headers

puddyglum’s picture

Thanks quicksketch for your snippet above. In order to prevent this problem completely, we had to get rid of the return path as well

function mymodule_mail_alter(&$message) {
  unset($message['headers']['Sender']);
  unset($message['headers']['Errors-To']);
  unset($message['headers']['Return-Path']);
}
jo_as_neo’s picture

Issue summary: View changes

In our project, the problem with IN BEHALF was resolved with :

    unset($message['from']);
    unset($message['headers']['From']);
    unset($message['headers']['Errors-To']);
    unset($message['headers']['Return-Path']);

Unsetting "Sender" as well meant that we lost the Reply-to information we provided (we wanted to put the email of the user who submitted the form, there).

mmarinescu’s picture

Hello all,

Could you please advise where do you write that code?

function mymodule_mail_alter(&$message) {
  unset($message['headers']['Sender']);
  unset($message['headers']['Errors-To']);
  unset($message['headers']['Return-Path']);
}

Thanks

puddyglum’s picture

@mmarinescu You place that code in a custom module created just your site's hooks. If you do not have a custom module, you need to create one. See https://www.drupal.org/node/1074362

weseze’s picture

I used this snippet of code to force the same email address for all headers:

function mymodule_mail_alter(&$message) {
  if (isset($message['headers']['Reply-To'])) {
    $message['headers']['From'] = $message['headers']['Reply-To'];
    $message['headers']['Sender'] = $message['headers']['Reply-To'];
    $message['headers']['Errors-To'] = $message['headers']['Reply-To'];
    $message['headers']['Return-Path'] = $message['headers']['Reply-To'];
  }
}
wesleymusgrove’s picture

@weseze, I just want to share my experience based on your helpful snippet. At least with what I'm seeing in Drupal 8.2.4 and Webform 8.x-5.0-beta4, the $message['headers']['Reply-To'] was actually supposed to have a lowercase "t" like $message['headers']['Reply-to'] . That simple change had me scratching my head for a good day and a half :/.

However I am still unable to hit a breakpoint in my own custom hook_mail_alter() implementation.

In Line 267 of docroot/core/lib/Drupal/Core/Mail/MailManager.php, this alter function says it's going to allow all modules to alter the resulting email.

// Invoke hook_mail_alter() to allow all modules to alter the resulting email.
$this->moduleHandler->alter('mail', $message);

Therefore this correctly falls into webform_mail_alter(&$message) on line 238 of docroot/modules/webform/webform.module, but not in my custom implementation of MYTHEME_mail_alter(&$message), which I have in the active and default theme's docroot/sites/all/themes/MYTHEME/MYTHEME.theme. I have cleared all my caches after defining the new hook.

Is it not hitting my custom implementation of hook_mail_alter() because it's in my theme file instead of in a custom module?

wesleymusgrove’s picture

Following up to say that putting my custom implementation of hook_mail_alter in a custom module instead of in the custom theme file worked.

usmanjutt84’s picture

comment #4 works for me

bikrampal’s picture

Hi, This is coming automatically based on the email ID used in your website. If your sender email is different from the Site information email then you will get this message like site-email@example.com on behalf of sender-email@example.com.

BUT you can alter email through web hook:

function yourmodule_mail_alter(&$message) {
if (isset($message['headers']['Reply-To'])) {
$message['headers']['From'] = $message['headers']['Reply-To'];
$message['headers']['Sender'] = $message['headers']['Reply-To'];
$message['headers']['Errors-To'] = $message['headers']['Reply-To'];
$message['headers']['Return-Path'] = $message['headers']['Reply-To'];
}
}