Software Engineering in WordPress and Musings on the Deep Life

How to Set an SMTP Server in WordPress

When it comes to sending emails in WordPress, the wp_mail function and its related filters such as wp_mail_content_type, wp_mail_from, and wp_mail_from_name are usually enough to accomplish the majority of what we need.

But there are times where it’s not enough. Specifically, there are times where we may need to define the details for using a custom SMTP server in WordPress.

Fortunately, WordPress provides a hook that makes this really easy to do.

Setting an SMTP Server in WordPress

So let’s say that you’re working on a web site or web application and you want to relay the email through a custom SMTP server rather than, say, whatever is installed on the server on which the site is running.

To do this, you need at least six pieces of information:

  • The SMTP host address
  • Whether or not you want to use SMTP authentication
  • The SMTP port
  • The username for the SMTP server
  • The password for the SMTP server
  • And whether or not you want to use a secure connection to the SMTP server

You can also specify the from and the fromName properties using the method I’m about to show; however, I prefer to use the provided action hooks for doing this.

Hook It Up in the Constructor

So first, let’s say that we have a constructor that’s responsible for setting up various hooks related to the email as well as that of the SMTP server.

The initial class may look something like this:

If you’re familiar with the wp_mail function, then most of these hooks are likely familiar; however, the phpmailer_init may be one that you haven’t seen before.

This is where the SMTP settings are defined.

Define The Hooks

Along with defining functions for the rest of the hooks, this is where we can set the SMTP server credentials to be used:

The code should be relatively easy to follow (and I’ve covered this in more detail in a previous post); however, the most important thing to note in this particular block of code is the values of the properties that are set on the instance of $phpmailer.

This is what allows us to use a different SMTP server.

The Working Class

Finally, the full version of the code looks something like this.

Obviously, this is meant to be invoked by a third-party object such that you’ll need to pass the $message of the email to the send() function, but the rest of the code shows how to specify to whom the email is being sent, the subject line of the email, and what SMTP server we’re using to relay the messages.


  1. Matt

    I like to use the MandrillApp plugin by Mailchimp. Great for a site or app with transactional emails like subscription and purchase receipts.

    You get 12k free a month and you can rest easier that they won’t end up on the spam folder.

    • Tom

      Yes! Mandrill is awesome – I’ve used it for some larger apps and for some other projects, but for my own personal email, Pobox fit the bill just fine.

  2. Morgan Estes

    I’ve used plugins that let me use other SMTP servers but never looked into how they actually work. Thanks for the explanation.

    I’m curious about your use of create_function() instead of using an anonymous function or another named function. Is it just for backwards compatibility for PHP 5.2, or is there another reason?

    • Tom

      I tend to favor using things that are best supported by older versions of PHP since so much of the WordPress-powered web is relying on that.

      That’s really my only reason — I’d love to use the newer features, but I have to temper my desire in order to provide as much compatibility as possible :).

  3. Lexie

    Hi Tom, this is great information. I’m hoping you can help me, because I’ve been struggling for days to set up the smtp mail function. I’ve tried every possibility, called my domain host, and email host, and no one has been able to figure this out. Our only possible option is that there is something within WordPress or my theme’s code that is blocking me from using smtp instead of the php mail function. If you could let me know whether that’s possible and what I should be looking for, that would be so much help!

    • Tom

      Hey Lexie,

      Unfortunately, this sounds like it may be due to a number of different issues ranging anywhere from your hosting environment to your mail configuration. Without more information, it’s hard to track down the problem.

      Wish I could be more help with this!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2023 Tom McFarlin

Theme by Anders NorenUp ↑