ZF-3362: about BCC and TO in Zend_Mail_Transport_Sendmail

Description

 $mail->addTo('me@example.com');
$mail->addBcc('him@example.com');
$mail->addBcc('them@example.com');

The above code snippet adds all three mail addresses to the TO field. $mail is an instance of Zend_Mail. I would expect the latter two to be added to BCC field.

I am using standard/trunk from svn at the time 1.5.2 is released.

NOTE: Applies to using IIS under VISTA with no explicit mail transport set.

Comments

Please evaluate and categorize as necessary.

To fix this problem

File: Zend/Mail.php

500 /** 501 * Return list of recipient email addresses 502 * 503 * @return array (of strings) 504 */ 505 public function getRecipients() 506 { 507 return $this->_to; 508// return array_keys($this->_recipients); 509 }

This returns all recipients by default comment out line 508 and replace with line 507 to only return those added in the two!!!

CC and BCC will still work as they are added to the headers which are still included.

Further Investigation found this is only happening on a windows machine IIS not on apache on linux

I must add that in my case it happens on windows Vista BU using Apache and remote smtp server of ISP.

**** Ignore the last change as it ill muck up the function to find out what recipients are getting mail ****

Yeah you would be right Bart - This would happen on all Windows machines as it has to do with the way the built-in mail(); function works.

On windows it acts as a kind of smtp router - on linux it is a direct hook into the sendmail MTA.

So there is a fundamental difference in the two and hence why the bug only turns up on a windows installation.

The following fix only puts the "To" recipients to the $to; in mail(); and all the Bcc and Cc are still added to the headers to they still get the email!!!!!

Change file....

File: Zend/Mail/Transport/Abstract.php

// Determine recipients, and prepare headers REMOVE THIS LINE // $this->recipients = implode(',', $mail->getRecipients()); /** This is the old code which gets all recipients and adds them to the "to" in mail() - this is not good */

ADD THIS LINE $this->recipients = implode(',', $mail->getTo()); // now you only get the _to recipients to add to the to in mail()

$this->_prepareHeaders($this->_getHeaders($boundary)); //All Bcc and Cc are still added as they are added in the headers where they belong

Add this function to...

File: Zend/Mail.php

/** * Return list of "To" recipient email addresses only, this ignore's the Cc and Bcc * * @return array (of strings) */ public function getTo() { return $this->_to; }

This is a better fix!!!!!!!! - Same basic idea though

Am I right, that this only happens with the sendmail transport? mail() has a different behavior under Windows. Have you tried using Zend_Mail_Transport_Smtp? Does it have similar problems?

I currently don't have a Windows with PHP here, so if anyone is faster than me providing a patch ... But recipients and the to header are two different things. The recipients are used for the envelope and if you replace it with what's in the to header you might break the SMTP transport. There is already special treatment for Windows in Zend_Mail_Transport_Sendmail::_prepareHeaders() and if there's something that should be fixed it's in this method.

I must admit that I did not look into transport at all. I have seen it come by when reading about Zend_Mail, but I thought, well, if it wraps mail(), it will work, since mail() works. This basically means that I did not set the transport explicitly, so I am using the default behavior under windows.

To me personally it doesn't really matter if it gets fixed fast, my production server being linux.

I tried the transport just now, to be sure if it makes a difference, and yes, it does, under windows using new Zend_Mail_Transport_Smtp('smtp.myserver.nl') works as it should!

So I guess it may come down to choosing the right transport for the job. The only downside I can see is that I will now have to configure the smtp server for the live host and the local testserver. But that's no big deal.

Changed priority to minor, because it works under windows if you use Zend_Mail_Transport_Smtp.

Change summary to be more understandable easyly

Changed summary.

I'll close this as documentation related issue, since this is also stated in the PHP manual and stuff.

A passage will be added to the documentation and i'll close, any comments?

Fixed as documentation issue

Well done.