# Send Email Through SMTP

> Routes all WordPress email through an SMTP server, using credentials stored as constants in wp-config.php rather than in the database.

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

## Code

```php
add_action( 'phpmailer_init', function ( $phpmailer ) {
    if ( ! defined( 'SMTP_HOST' ) || '' === SMTP_HOST ) {
        return;
    }

    $phpmailer->isSMTP();
    $phpmailer->Host       = SMTP_HOST;
    $phpmailer->Port       = defined( 'SMTP_PORT' ) ? (int) SMTP_PORT : 587;
    $phpmailer->SMTPSecure = defined( 'SMTP_SECURE' ) ? SMTP_SECURE : 'tls';

    if ( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) ) {
        $phpmailer->SMTPAuth = true;
        $phpmailer->Username = SMTP_USER;
        $phpmailer->Password = SMTP_PASS;
    }

    if ( defined( 'SMTP_FROM' ) && is_email( SMTP_FROM ) ) {
        $phpmailer->setFrom( SMTP_FROM, wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), false );
    }
} );
```

## Notes

Add to wp-config.php: define( 'SMTP_HOST', 'smtp.example.com' ); define( 'SMTP_PORT', 587 ); define( 'SMTP_SECURE', 'tls' ); define( 'SMTP_USER', 'user' ); define( 'SMTP_PASS', 'password' ); and optionally define( 'SMTP_FROM', 'hello@example.com' ); The snippet does nothing until SMTP_HOST is defined.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send Email Through SMTP" 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/
