How can I change the default from address for the php mail() function. I know I can do it on a per-send basis, but I'd like to change the default setting as well.
3 Answers
You can set that in php.ini with "sendmail_from".
From the php.ini:
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail -t
If you haven't got access to php.ini for some reason, you could set the following in a .htaccess file:
php_value sendmail_from [email protected]
Note: you can't set sendmail_path in this way for security reasons. Source: http://nl.php.net/manual/en/mail.configuration.php
I have done this for our webmasters in the MTA (smtp server). If your MTA is postfix, you could use the sender_canonical_maps lookup table. From man 5 postconf:
sender_canonical_maps (default: empty)
Optional address mapping lookup tables for envelope and header sender
addresses. The table format and lookups are documented in canoni‐
cal(5).
Example: you want to rewrite the SENDER address "[email protected]" to
"[email protected]", while still being able to send mail to the RECIP‐
IENT address "[email protected]".
Note: $sender_canonical_maps is processed before $canonical_maps.
Example:
sender_canonical_maps = hash:/etc/postfix/sender_canonical
You need to define a header where you put "From: Toto toto < [email protected] >" (without spaces, but the interface remove the contents of < >).
To change it in default setting, you should do this not in PHP, but in your MTA. Rewrite the from address to what you want.