I have done a search here. I know this question has been asked multiple times. But my situation is a little different, and I can't seem to get this to work.
I have written a CLI script which reads from a mail queue in a database, and sends the mails. The database contains the data for $to
, $subject
, $body
, and $headers
, so they can just be plugged right into the mail()
function. Sounds simple enough.
EXCEPT.
The database also contains $from
, which is the username of the user on my system who enqueued the mail. It is not intended to replace the From header in $headers
. Users may specify any address they like in the From header, as is the norm.
Because the script is running as root (for reasons I won't get into), calling the mail()
function results in the Return-Path header being set to [email protected]
. It also results in "root" showing up in the SPF mail headers that get added along the way.
Ideally, I would like the user that enqueued the mail, in $from
, to appear in the Return-Path.
Things I've tried:
mail( $to, $subject, $body, array_merge( $headers, [ 'Return-Path' => "[email protected]" ] );
mail( $to, $subject, $body, $headers, "-f [email protected]" );
posix_seteuid( posix_getpwnam( $from )['uid'] ); mail( $to, $subject, $body, $headers );
if( pcntl_fork() === 0 ) { posix_setuid( posix_getpwnam( $from )['uid'] ); mail( $to, $subject, $body, $headers ); die; }
All of the above things DO in fact send the mail, but ALL of the have headers peppered with that pesky "root." The last one particularly surprises me...
Any thoughts on why this might be happening, or how I can go about changing that Return-Path?
Okay, so, the problem was with
posix_setuid()
not working. I appendedposix_getuid()
andposix_geteuid()
to the body of the outgoing mail and noticed that the uid had not changed. After resolving this issue (my own bug, nothing crazy), the mail is now being sent with the appropriate return path.Thanks to any one who read and thought about this!