title: How can I force the From: header (envelope sender) in PHP without putting it in mail()?
body: <p>I have a development web server (CentOS LAMP stack) that uses SMTP relays setup in postfix to send email. We use <a href="http://documentation.mailgun.com/" rel="nofollow">mailgun</a> with multiple users, a setup similar to <a href="https://community.rackspace.com/products/f/28/t/3897" rel="nofollow">this</a>, but with specific users instead of just wildcard emails:</p>
  
  <p><strong>/etc/postfix/main.cf</strong></p>
  
  <pre><code>smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
  smtp_sasl_auth_enable = yes
  sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map
  smtp_sender_dependent_authentication = yes
  smtp_sasl_security_options = noanonymous
  relayhost = [smtp.mailgun.org]:587
  </code></pre>
  
  <p><strong>/etc/postfix/sasl_passwd</strong></p>
  
  <pre><code># our domains
  no-reply@domain.com  postmaster@domain.com:password1
  info@domain2.com     postmaster@domain2.com:password2
  support@domain3.com  postmaster@domain3.com:password3
  
  # in-case we don't have it setup, use a default
  #[smtp.mailgun.org]:587      postmaster@domain2.com:password2
  </code></pre>
  
  <p><strong>/etc/postfix/relayhost_map</strong></p>
  
  <pre><code>no-reply@domain.com  [smtp.mailgun.org]:587
  info@domain2.com     [smtp.mailgun.org]:587
  support@domain3.com  [smtp.mailgun.org]:587
  </code></pre>
  
  <p>To setup email logging , I am authenticating each developer on the machine with their own SMTP credentials. I want to set it up so that developers don't need to add the <code>additional_headers</code> or <code>additional_parameters</code> to get the correct smtp relay match in postfix - and indeed it would take a lot of work to setup different mail headers in code for different developers, especially with versioned code. I digress. This was working fine from postfix's side of things when I use the following:</p>
  
  <pre><code>mail('email@address.tld', 'subject', 'message here...', 'From: noreply@domain.com', '-fnoreply@domain.com');
  </code></pre>
  
  <p>So I then added the following to the vhost configs:</p>
  
  <pre><code>php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -fnoreply@domain.com"
  </code></pre>
  
  <p>which successfully allowed me to get rid of the <code>-f</code> <code>additional_parameter</code> and still send properly. Then I added the following:</p>
  
  <pre><code>php_value sendmail_from "noreply@domain.com"
  </code></pre>
  
  <p>In a <code>phpinfo()</code> dump I see the local value for <code>sendmail_from</code> is set correctly, however now when I send the email it comes up as:</p>
  
  <blockquote>
    <p>None@domain.com on behalf of Apache </p>
  </blockquote>
  
  <p>It seems as if the correct sender (MIME, not envelope, as postfix recognises the authentication and gives 250 Great success). With postfix logging on verbose, I see only references to the correct email from the sender input attribute.</p>
  
  <p>In mailgun, I see the following information from the log, however, for the email when the <code>From: noreply@domain.com</code> <strong>isn't</strong> used:</p>
  
  <pre><code>...
  "envelope": {
      "transport": "smtp", 
      "sender": "noreply@domain.com", 
      "sending-ip": "x.x.x.x", 
      "targets": "email@address.tld"
  }, 
  "message": {
      "headers": {
          "to": "email@address.tld", 
          "message-id": "2014061111016.ABC1D23456E@domain.com", 
          "from": "noreply@domain.com (Apache)", 
          "subject": "Debug Test"
      }, 
      "attachments": [], 
      "recipients": [
          "email@address.tld"
      ], 
      "size": 654
  },
  ...
  </code></pre>
  
  <p>What's interesting is the same log for when <code>From: noreply@domain.com</code> <strong>is</strong> present, the <code>message-&gt;headers-&gt;from</code> is set correcetly to <code>noreply@domain.com</code> without the <code>(Apache)</code> addition. Surely this means that it's PHP's fault and that PHP is not utilising the <code>sendmail_from</code> value properly? </p>
  
  <p>So with all this in mind, my resulting question is <strong>how can I set the default MIME Sender (From header) in PHP, apart from in the <code>mail()</code> function?</strong> Have I missed something with my method/config above, or is it just plain impossible? I'm happy to think outside the box a little, being that this will help save time for the reason we want  this feature.</p>