Home > Article > Backend Development > How to Customize the Envelope From Address in PHP\'s mail() Function?
How to Manipulate Envelope From Address Using PHP's Mail Function
In PHP, the mail() function is commonly used to send emails. However, by default, the envelope from address, which appears in the MAIL FROM field of the email protocol, often includes the Apache user and localhost name. This can pose problems when remote mail servers reject emails due to non-existent domains.
To address this issue, you can specify an additional parameter in the mail() function, which allows you to pass options directly to sendmail. This approach is preferred over spawning sendmail manually and piping the email contents.
To change the envelope from address using the mail() function, add the fifth parameter as follows:
<code class="php">mail('[email protected]','subject!','body!','From: [email protected]','-f [email protected]');</code>
In this example, the -f [email protected] option specifies the desired envelope from address. The -f flag tells sendmail to use the specified address as the sender during the MAIL FROM step of the email protocol.
By using this approach, you can effectively control the envelope from address and ensure that emails are delivered without rejection issues due to incorrect envelope addresses.
The above is the detailed content of How to Customize the Envelope From Address in PHP\'s mail() Function?. For more information, please follow other related articles on the PHP Chinese website!