I am running a server with Directadmin and Exim installed. When I send email using a PHP script, the envelope sender is being rewritten to the default Directadmin user. Why is this happening and how do I fix it?
It's probably best not to use mail(), but instead add a good PHP mail class such as PHPMailer to your app. However, to answer the question:
There are two main parts you must manage when sending an email:
1.) The message headers/body.
2.) The smtp conversation between the mail servers which establishes the sender and recipient(s), often times referred to as envelope settings.
Your code is setting headers, which address things in item #1 only. Your code's problem is that the "envelope sender", which is part of item #2 is not being explicitly set, so Exim is using the default which is $USER@$HOSTNAME.
In general when using the php mail() command, it is difficult and non-obvious how to get the envelope settings to values you want. Instead, use something more configurable that can make an actual SMTP connection, (instead of calling /usr/sbin/sendmail directly). It's even better when the SMTP server is on localhost, the same principles apply. When you can specify everything in the envelope for an actual SMTP connection, such as the PHPMailer class, this issue will be resolved.