How to hide server mail on smtp php mail

When sending emails using PHP's mail() function or a library like SwiftMailer, the server's email address is typically included in the From header of the email. This can be a security risk, as it reveals the server's email address to the recipient.

To hide the server's email address, you can use a technique called "spoofing" or "email relaying". Here are a few ways to do it:

Method 1: Using a relay server

You can set up a relay server that acts as a middleman between your PHP script and the final email recipient. The relay server will receive the email from your PHP script and then forward it to the final recipient. This way, the recipient will see the relay server's email address instead of your server's email address.

For example, you can use a service like Mailgun or Sendgrid, which offer relay services that can be used to send emails on behalf of your domain.

Method 2: Using a fake sender email address

You can set the From header of the email to a fake email address that is not associated with your server. This is known as "spoofing" or "email spoofing". However, be aware that this technique is not recommended, as it can be considered a form of email abuse and may be blocked by email providers.

Here's an example of how you can set a fake sender email address using PHP:

$headers = array(
    'From' => '[email protected]',
    'Reply-To' => '[email protected]',
    'Subject' => 'Test email',
    'Content-Type' => 'text/plain'
);

mail('[email protected]', 'Test email', 'Hello, this is a test email.', $headers);

Method 3: Using a PHP library that supports email relaying

Some PHP libraries, such as SwiftMailer, offer built-in support for email relaying. You can configure the library to use a relay server or set a fake sender email address.

For example, with SwiftMailer, you can set the from parameter to a fake email address:

$mailer = Swift_Mailer::newInstance();
$mailer->setFrom('[email protected]');
$mailer->send($message);

Method 4: Using a custom email handler

You can also write a custom email handler that intercepts the email and modifies the From header before sending it. This approach requires more coding effort, but it gives you full control over the email sending process.

For example, you can create a custom email handler that sets the From header to a fake email address:

function custom_email_handler($to, $subject, $message) {
    $headers = array(
        'From' => '[email protected]',
        'Reply-To' => '[email protected]',
        'Subject' => $subject,
        'Content-Type' => 'text/plain'
    );
    mail($to, $subject, $message, $headers);
}

Remember to always follow email best practices and respect email providers' policies when sending emails.