PHPwordpressintermediate
Configure WordPress SMTP Email
Set up SMTP for reliable WordPress email delivery using PHPMailer
Faisal Yaqoob
November 12, 2025
#wordpress#email#smtp#phpmailer#gmail
Code
php
1 // Configure SMTP settings for WordPress 2 function configure_smtp_settings($phpmailer) { 3 $phpmailer->isSMTP(); 4 $phpmailer->Host = 'smtp.gmail.com'; // Your SMTP server 5 $phpmailer->SMTPAuth = true; 6 $phpmailer->Port = 587; // or 465 for SSL 7 $phpmailer->Username = 'your-email@gmail.com'; 8 $phpmailer->Password = 'your-app-password'; // Use app-specific password 9 $phpmailer->SMTPSecure = 'tls'; // 'tls' or 'ssl' 10 $phpmailer->From = 'your-email@gmail.com'; 11 $phpmailer->FromName = 'Your Site Name'; 12 } 13 add_action('phpmailer_init', 'configure_smtp_settings'); 14
15 // Set default from email 16 function custom_wp_mail_from($original_email_address) { 17 return 'your-email@yourdomain.com'; 18 } 19 add_filter('wp_mail_from', 'custom_wp_mail_from'); 20
21 // Set default from name 22 function custom_wp_mail_from_name($original_email_from) { 23 return 'Your Website Name'; 24 } 25 add_filter('wp_mail_from_name', 'custom_wp_mail_from_name');
Configure WordPress SMTP Email
Fix WordPress email delivery issues by configuring SMTP (Simple Mail Transfer Protocol) to send emails reliably through services like Gmail, SendGrid, or Mailgun.
// Configure SMTP settings for WordPress
function configure_smtp_settings($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.gmail.com'; // Your SMTP server
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587; // or 465 for SSL
$phpmailer->Username = 'your-email@gmail.com';
$phpmailer->Password = 'your-app-password'; // Use app-specific password
$phpmailer->SMTPSecure = 'tls'; // 'tls' or 'ssl'
$phpmailer->From = 'your-email@gmail.com';
$phpmailer->FromName = 'Your Site Name';
}
add_action('phpmailer_init', 'configure_smtp_settings');
// Set default from email
function custom_wp_mail_from($original_email_address) {
return 'your-email@yourdomain.com';
}
add_filter('wp_mail_from', 'custom_wp_mail_from');
// Set default from name
function custom_wp_mail_from_name($original_email_from) {
return 'Your Website Name';
}
add_filter('wp_mail_from_name', 'custom_wp_mail_from_name');
Gmail Configuration
// Gmail SMTP configuration
function setup_gmail_smtp($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.gmail.com';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = 'your-email@gmail.com';
$phpmailer->Password = 'your-16-digit-app-password';
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = 'your-email@gmail.com';
$phpmailer->FromName = get_bloginfo('name');
// Enable verbose debug output (disable in production)
// $phpmailer->SMTPDebug = 2;
}
add_action('phpmailer_init', 'setup_gmail_smtp');
SendGrid Configuration
// SendGrid SMTP configuration
function setup_sendgrid_smtp($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.sendgrid.net';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = 'apikey'; // This is always 'apikey'
$phpmailer->Password = 'your-sendgrid-api-key';
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = 'noreply@yourdomain.com';
$phpmailer->FromName = get_bloginfo('name');
}
add_action('phpmailer_init', 'setup_sendgrid_smtp');
Mailgun Configuration
// Mailgun SMTP configuration
function setup_mailgun_smtp($phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.mailgun.org';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = 'postmaster@your-domain.mailgun.org';
$phpmailer->Password = 'your-mailgun-password';
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = 'noreply@yourdomain.com';
$phpmailer->FromName = get_bloginfo('name');
}
add_action('phpmailer_init', 'setup_mailgun_smtp');
Environment Variables (Secure Method)
// Use environment variables for credentials (recommended)
// Add to wp-config.php:
// define('SMTP_HOST', 'smtp.gmail.com');
// define('SMTP_PORT', 587);
// define('SMTP_USER', 'your-email@gmail.com');
// define('SMTP_PASS', 'your-password');
// define('SMTP_FROM', 'noreply@yourdomain.com');
// define('SMTP_NAME', 'Your Site Name');
function setup_smtp_from_env($phpmailer) {
if (defined('SMTP_HOST')) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
}
add_action('phpmailer_init', 'setup_smtp_from_env');
Test Email Function
// Test email sending
function send_test_email() {
$to = 'test@example.com';
$subject = 'WordPress SMTP Test Email';
$message = 'This is a test email sent from WordPress using SMTP.';
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: Your Site <noreply@yourdomain.com>'
);
$sent = wp_mail($to, $subject, $message, $headers);
if ($sent) {
echo 'Test email sent successfully!';
} else {
echo 'Failed to send test email.';
}
}
// Run this function once to test
// send_test_email();
Error Logging
// Log email errors
function log_email_errors($wp_error) {
if (is_wp_error($wp_error)) {
error_log('Email Error: ' . $wp_error->get_error_message());
}
}
add_action('wp_mail_failed', 'log_email_errors');
// Enable SMTP debugging (only for troubleshooting)
function enable_smtp_debugging($phpmailer) {
$phpmailer->SMTPDebug = 2; // Set to 0 in production
$phpmailer->Debugoutput = function($str, $level) {
error_log("SMTP Debug level $level: $str");
};
}
// add_action('phpmailer_init', 'enable_smtp_debugging');
HTML Email Template
// Send HTML email with custom template
function send_custom_html_email($to, $subject, $message) {
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo('name') . ' <noreply@yourdomain.com>',
);
$html_message = '
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: #0073aa; color: white; padding: 20px; text-align: center; }
.content { padding: 20px; background: #f9f9f9; }
.footer { padding: 20px; text-align: center; font-size: 12px; color: #666; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>' . get_bloginfo('name') . '</h1>
</div>
<div class="content">
' . wpautop($message) . '
</div>
<div class="footer">
<p>© ' . date('Y') . ' ' . get_bloginfo('name') . '. All rights reserved.</p>
</div>
</div>
</body>
</html>
';
return wp_mail($to, $subject, $html_message, $headers);
}
Features
- Reliable Delivery: Use professional SMTP services
- Multiple Providers: Supports Gmail, SendGrid, Mailgun, etc.
- Secure: Use environment variables for credentials
- Debugging: Built-in error logging
- HTML Support: Send formatted HTML emails
- Testing: Test function to verify configuration
Gmail App Password Setup
- Enable 2-Factor Authentication on your Google account
- Go to Google Account > Security > App Passwords
- Generate an app-specific password
- Use this 16-digit password in your SMTP configuration
Common SMTP Ports
- 587: TLS/STARTTLS (recommended)
- 465: SSL
- 25: Non-encrypted (not recommended)
Related Snippets
WordPress Custom Breadcrumbs
Create SEO-friendly breadcrumb navigation without plugins
PHPwordpressintermediate
phpPreview
// Display breadcrumbs
function custom_breadcrumbs() {
// Settings
$separator = ' » ';
...#wordpress#breadcrumbs#navigation+2
11/15/2025
View
Create Custom WordPress REST API Endpoint
Add custom REST API endpoints to extend WordPress functionality
PHPwordpressintermediate
phpPreview
// Register custom REST API endpoint
function register_custom_api_endpoint() {
register_rest_route('custom/v1', '/posts', array(
'methods' => 'GET',
...#wordpress#rest-api#api+2
11/14/2025
View
WordPress Custom Menu Walker
Create custom navigation menu HTML structure with a custom walker class
PHPwordpressadvanced
phpPreview
// Custom Walker Class for Bootstrap 5
class Bootstrap_5_Walker_Nav_Menu extends Walker_Nav_Menu {
/**
...#wordpress#menu#navigation+2
11/13/2025
View