WordPress normally uses PHP’s mail() function to send emails. On many hosting servers, this can cause emails to go to spam or fail to arrive altogether.
A more reliable solution is to configure SMTP (Simple Mail Transfer Protocol) directly in WordPress.
The good news is that you don’t need to install a plugin such as WP Mail SMTP. WordPress uses PHPMailer internally, and we can configure PHPMailer using the phpmailer_init action.
This approach works with:
- Contact Form 7
- WooCommerce emails
- WordPress password reset emails
- User registration emails
- Custom
wp_mail()emails - Other plugins that use WordPress’s email system
Why Use SMTP in WordPress?
SMTP allows WordPress to send emails through a dedicated email server.
Compared with the default PHP mail method, SMTP generally provides:
- Better email deliverability
- SMTP authentication
- TLS/SSL encryption
- Better control over the sender address
- More reliable delivery
Step 1: Add SMTP Credentials to wp-config.php
Add the following configuration before the line:
/* That's all, stop editing! Happy publishing. */Example:
define( 'CUSTOM_SMTP_HOST', 'smtp.example.com' );
define( 'CUSTOM_SMTP_PORT', 587 );
define( 'CUSTOM_SMTP_USERNAME', 'your-email@example.com' );
define( 'CUSTOM_SMTP_PASSWORD', 'YOUR_SMTP_PASSWORD' );
define( 'CUSTOM_SMTP_FROM', 'your-email@example.com' );
define( 'CUSTOM_SMTP_FROM_NAME', 'Your Website Name' );Keeping the SMTP password in wp-config.php is preferable to putting it directly inside your plugin code.
Step 2: Configure PHPMailer
Add this code:
/**
* Configure WordPress PHPMailer to use SMTP.
*/
add_action( 'phpmailer_init', function ( $phpmailer ) {
$phpmailer->isSMTP();
// SMTP server.
$phpmailer->Host = CUSTOM_SMTP_HOST;
// Enable SMTP authentication.
$phpmailer->SMTPAuth = true;
// SMTP username and password.
$phpmailer->Username = CUSTOM_SMTP_USERNAME;
$phpmailer->Password = CUSTOM_SMTP_PASSWORD;
// SMTP port.
$phpmailer->Port = CUSTOM_SMTP_PORT;
// Encryption.
$phpmailer->SMTPSecure = 'tls';
// Sender information.
$phpmailer->From = CUSTOM_SMTP_FROM;
$phpmailer->FromName = CUSTOM_SMTP_FROM_NAME;
} );Save the file
Step 3: Test WordPress Email
After configuring SMTP, you can test the configuration using WordPress’s wp_mail() function.
For example:
wp_mail(
'admin@example.com',
'WordPress SMTP Test',
'This is a test email from WordPress SMTP.'
);If the email arrives successfully, your SMTP configuration is working.
For production websites, it is better to create a temporary admin-only test function rather than leaving test code publicly accessible.
Conclusion
Configuring SMTP directly through WordPress’s built-in PHPMailer integration is a simple way to improve email delivery without installing an SMTP plugin.
The key points are:
- Store SMTP credentials securely in
wp-config.php. - Use the
phpmailer_inithook. - Configure PHPMailer to use your SMTP server.
- Use TLS/SSL as required by your provider.
- Configure SPF, DKIM, and DMARC for better deliverability.
- Keep the SMTP configuration in a custom plugin instead of the theme.
This gives you a lightweight and maintainable SMTP setup while avoiding an additional WordPress plugin.

