# Keep the From Address on Your Own Domain

> When a plugin sends email "from" a visitor's or a free webmail address, switches the From to your own domain and keeps the original address as Reply-To, so emails pass SPF and DMARC checks.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, deliverability, dmarc
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-align-from-domain/

## Code

```php
$wpide_aligned_from = ''; // Address on your domain; empty = wordpress@yourdomain.

add_action( 'phpmailer_init', function ( $phpmailer ) use ( $wpide_aligned_from ) {
    $site   = preg_replace( '/^www\./', '', (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
    $domain = strtolower( (string) substr( strrchr( (string) $phpmailer->From, '@' ), 1 ) );

    if ( '' === $site || $domain === $site || ( strlen( $domain ) > strlen( $site ) && substr( $domain, -strlen( '.' . $site ) ) === '.' . $site ) ) {
        return;
    }

    $original = $phpmailer->From;
    $name     = $phpmailer->FromName;
    $from     = is_email( $wpide_aligned_from ) ? $wpide_aligned_from : 'wordpress@' . $site;

    if ( ! $phpmailer->getReplyToAddresses() && is_email( $original ) ) {
        $phpmailer->addReplyTo( $original, $name );
    }

    $phpmailer->setFrom( $from, $name, false );
}, 999 );
```

## Notes

Set the address to use at the top, or leave it empty to use wordpress@ your domain.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Keep the From Address on Your Own Domain" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
