How to Setup WordPress SMTP Without a Plugin

WordPress sends email for password resets, new user notifications, contact form messages, and WooCommerce order updates. When that email never arrives, the problem is rarely your contact form or your theme. The problem is how WordPress sends mail by default, and the fix is SMTP. This guide shows you how to setup WordPress SMTP without a plugin, using a small code snippet and settings in wp-config.php.
Why Does WordPress Email Fail by Default?
WordPress uses a function called wp_mail() to send email. By default, wp_mail() hands the message to the PHP mail() function on your web server, which sends it without logging in to any mail server. The message goes out with no authentication, no encryption, and often from an address that does not match your domain, such as wordpress@yourdomain.com or an address built from your server hostname.
Receiving mail servers treat unauthenticated mail with suspicion, and for good reason. Most spam on the internet is sent exactly this way, from servers that never prove who they are. Gmail, Outlook, and Yahoo check whether the sending server is allowed to send mail for your domain. When the answer is unclear, the message lands in the spam folder or gets rejected outright, and neither you nor your visitor ever sees an error.
What Is SMTP and Why Does It Fix Deliverability?
SMTP stands for Simple Mail Transfer Protocol, and it is the standard way email moves across the internet. When you configure SMTP in WordPress, every email your site sends goes through a real mail server that you log in to with a username and password, usually over an encrypted connection. The receiving server can verify that the message came from an account that is allowed to send mail for your domain, which is exactly the trust signal that PHP mail lacks.
SMTP also gives you a paper trail. Providers like Gmail, SendGrid, and Postmark keep logs of sent messages, bounces, and delivery failures, so you can see whether an email was accepted, rejected, or marked as spam. With plain PHP mail, a failed message simply vanishes, and you have no way to know what happened. That visibility alone makes SMTP worth setting up, even before you count the deliverability improvement.
What Do You Need Before You Start?
Before you write any code, gather four things. First, pick your SMTP provider. Gmail works well for small sites, while SendGrid or Postmark suit sites that send order emails, membership notifications, or newsletters. Second, get the SMTP host, port, username, and password from your provider. Third, make sure you can edit wp-config.php and add a code snippet, either through the Code Snippets plugin or your child theme functions.php file. Fourth, set aside ten minutes to test, because an untested SMTP setup is barely better than no setup at all.
- SMTP host: the mail server address, such as smtp.gmail.com for Gmail or smtp.sendgrid.net for SendGrid.
- SMTP port: usually 587 with TLS encryption, or 465 with SSL encryption. Port 587 is the safer default.
- Username: usually your full email address, such as you@yourdomain.com.
- Password: your mail account password, or an app password where the provider requires one. Gmail requires an app password, covered below.
- From address: the address your site mail appears to come from. Use a real address on your own domain, not a free Gmail address.
How to Setup WordPress SMTP Without a Plugin
The manual setup has two parts. First, define your SMTP credentials as constants in wp-config.php. Second, add a small function that hooks into phpmailer_init and tells PHPMailer to use those constants. Splitting the setup this way keeps passwords out of your theme files, so they survive theme updates, and it keeps the logic in one tidy function you can remove at any time.
Add the snippet with the Code Snippets plugin or in your child theme functions.php file. Never edit the parent theme directly, because a theme update will wipe your changes. The official phpmailer_init hook documentation on developer.wordpress.org lists every property you can set on the mailer object.
function vutg_configure_smtp( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = get_bloginfo( 'name' );
}
add_action( 'phpmailer_init', 'vutg_configure_smtp' );
Each line has a clear job. The isSMTP() call switches PHPMailer from the default PHP mail method to SMTP. The Host line points it at your provider server. SMTPAuth set to true tells it to log in, and Port, Username, and Password supply the login details. SMTPSecure sets the encryption method, usually tls on port 587. The From and FromName lines set the sender address and the display name your readers see, using your site name by default.
How to Add SMTP Settings to wp-config.php
Open wp-config.php in your hosting file manager or over SFTP and add the constants just above the line that says “That is all, stop editing!” Fill in your own values for each one. Use tls for the secure setting when your port is 587, and ssl when your port is 465.
define( 'SMTP_HOST', 'smtp.gmail.com' );
define( 'SMTP_PORT', 587 );
define( 'SMTP_USER', 'you@yourdomain.com' );
define( 'SMTP_PASS', 'your-app-password-here' );
define( 'SMTP_SECURE', 'tls' );
define( 'SMTP_FROM', 'you@yourdomain.com' );
If your host blocks outgoing SMTP connections on shared plans, the test email will time out instead of sending. Some budget hosts block port 587 and only allow their own mail servers, so check your host documentation or ask support to open SMTP access. Our web hosting guide covers how to judge a host mail policy before you sign up, because a host that blocks SMTP will cause email trouble for years.
How to Use Gmail SMTP With WordPress
Gmail is the easiest free SMTP option for a small site, but it has one rule that catches almost everyone. Google no longer accepts your regular Gmail password for SMTP. You must turn on 2-Step Verification for your Google account, then create an app password and use that as your SMTP password instead. The official Google guide to app passwords on support.google.com walks through the exact steps.
- Host: smtp.gmail.com
- Port: 587 with tls encryption
- Username: your full Gmail address
- Password: the 16 character app password, entered without spaces
- From address: your Gmail address, or a domain address verified in Gmail settings
To create the app password, open your Google account security settings, confirm 2-Step Verification is on, then open the App passwords page and generate one for Mail. Google shows the password only once, so copy it straight into wp-config.php. If you paste it with spaces, remove them, because the password works without spaces.
How to Use SendGrid or Postmark SMTP With WordPress
Transactional providers exist for one job, which is delivering application email reliably at scale. They offer dashboards that show delivery, opens, bounces, and spam complaints, plus free tiers that cover most small sites comfortably. The WordPress setup is identical to Gmail, because only the constants change.
- SendGrid host: smtp.sendgrid.net, port 587 with tls, username apikey, password your SendGrid API key.
- Postmark host: smtp.postmarkapp.com, port 587 with tls, username your Postmark server token, password the same server token again.
- From address: a domain address you have verified with the provider, because unverified sender addresses get rejected.
How to Send a Test Email After Setup
Testing is not optional. Add this temporary snippet, visit any page on your site once, then check your inbox. The snippet sends one test message to the address you choose and then you should delete it, because leaving test code live is sloppy and it will send a message on every page load if you forget it.
function vutg_send_smtp_test() {
$sent = wp_mail( 'you@yourdomain.com', 'SMTP test from WordPress', 'If you can read this, SMTP is working.' );
error_log( 'SMTP test sent: ' . ( $sent ? 'yes' : 'no' ) );
}
add_action( 'init', 'vutg_send_smtp_test' );
Check three things. First, did the message arrive, and did it arrive in the inbox rather than spam. Second, check the sender details in your mail client, which should show your domain and a passing authentication result. Third, check your error log for the line the snippet writes, which confirms wp_mail() returned success. If the message never arrives, read the next section before changing random settings, because the cause is usually DNS, not the snippet.
Why SPF, DKIM, and DMARC Matter as Much as SMTP
SMTP proves you logged in to send the message, but SPF, DKIM, and DMARC prove your domain allows that server to send on its behalf. Without them, a receiving server sees a properly authenticated SMTP login from a server your domain never authorized, and it still distrusts the message. Think of SMTP as your passport and these three records as the visa stamp. You need both to cross the border smoothly.
- SPF: a DNS record that lists which servers may send mail for your domain. Add your provider include, such as include:_spf.google.com for Gmail, to your existing SPF record. Never create two SPF records, because that breaks validation.
- DKIM: a digital signature added to each outgoing message. Your provider gives you a public key to publish in DNS, and receiving servers use it to confirm the message was not altered in transit.
- DMARC: a DNS record that tells receiving servers what to do when SPF or DKIM checks fail. Start with a monitoring policy, such as p=none, watch the reports for a week, then tighten it to quarantine or reject.
What to Do When Email Still Fails After SMTP Setup
Check your provider dashboard too. SendGrid and Postmark log every rejection with a reason, such as an unverified sender address or a suppressed recipient, and that reason is often the whole answer. If your contact form still loses messages after SMTP works, the form itself may be at fault, so our guide to adding a contact form in Elementor covers form setup and notification settings. For a full troubleshooting walkthrough, read our companion guide to fixing WordPress not sending emails, which covers every other cause from plugin conflicts to host mail limits.
Frequently Asked Questions
Do I need a plugin to setup WordPress SMTP?
No. WordPress includes the PHPMailer library, and the phpmailer_init hook lets you configure SMTP with a short snippet plus constants in wp-config.php. A plugin only adds a settings screen, so the manual method gives you the same result with one less plugin to maintain.
Can I use Gmail SMTP for WordPress for free?
Yes, for low volume sites. Turn on 2-Step Verification, create an app password, and use smtp.gmail.com on port 587. Gmail enforces daily sending limits, so switch to SendGrid or Postmark if your site sends a large number of emails every day.
Why do my WordPress emails still go to spam after SMTP setup?
SMTP alone does not prove your domain authorized the sending server. Add SPF, DKIM, and DMARC records for your domain, use a from address on your own domain, and test with a mail tester tool to confirm all three checks pass.
Is it safe to store my SMTP password in wp-config.php?
Yes, wp-config.php already holds your database password, and it sits outside the public web root on most hosts. It is far safer than hardcoding credentials in a theme file, and using an app password or API key means you can revoke it at any time without changing your main account password.
Last updated: November 2026.



