Home >Backend Development >PHP Tutorial >How to Modify the Envelope \'MAIL FROM\' Address When Sending Email with PHP\'s mail() Function?
Modifying Envelope From Address in PHP Mail
When utilizing PHP's mail() function to send emails, it can be desirable to modify the envelope "MAIL FROM" address to something other than the default Apache_user@localhostname. This address can be rejected by some remote mail servers due to nonexistent domains.
PHP Mail() Function Arguments
The mail() function accepts five optional arguments, the last of which can be used to pass options directly to sendmail. To modify the envelope address, we can set this fifth argument as follows:
<code class="php">mail('[email protected]', 'subject!', 'body!', 'From: [email protected]', '-f [email protected]');</code>
In this example, the envelope "MAIL FROM" address will be set to "realname@realhost", even though the "From:" header in the message body will still show "[email protected]."
Creating a Custom Email Address
If you want a more PHP-centric approach, consider the following options:
Other Considerations
When setting the envelope address, ensure it is valid and belongs to a properly configured domain. Some anti-spam filters may reject emails with an invalid or unverifiable envelope address. Additionally, if you are using shared hosting, you may need to consult your hosting provider to ensure that you have the necessary permissions to modify envelope addresses.
The above is the detailed content of How to Modify the Envelope \'MAIL FROM\' Address When Sending Email with PHP\'s mail() Function?. For more information, please follow other related articles on the PHP Chinese website!