Knowledge Base

How to send e-mail in WordPress

This article describes how to configure WordPress to send e-mail messages using SMTP authentication. You can use a third-party WordPress plugin to do this, or you can write your own code in a custom WordPress plugin.

Method #1: Using a third-party plugin

There are many WordPress plugins that provide e-mail functionality. One of the most popular and well-known plugins is WP Mail SMTP, which enables you to configure e-mail settings that are compatible with A2 Hosting servers.

Installing the WP Mail SMTP plugin

To install the WP Mail SMTP plugin, follow these steps:

  1. Log in to your WordPress site as the administrator.
  2. In the left-hand pane, click Plugins.
  3. Click Add New.
  4. In the Search Plugins text box, type mail smtp, and then press Enter.
  5. Locate WP Mail SMTP by WPForms, and then click Install Now.
  6. After WordPress finishes installing the plugin, click Activate.
Configuring the WP Mail SMTP plugin

To configure the WP Mail SMTP plugin to work with your account, follow these steps:

  1. Log in to your WordPress site as the administrator.
  2. In the left-hand pane, click Settings, and then click Email. The Advanced Email Options page appears.
  3. In the From Email text box, type the e-mail address you want to use as the sender.
    This e-mail address must exist on your account.
  4. In the From Name text box, type the name you want to associate with the e-mail address you specified in step 3.
  5. Under Mailer, select Send all WordPress emails via SMTP.
  6. Under Return Path, select the Set the return-path to match the From Email check box.
  7. Under SMTP Options, in the SMTP Host text box, type the full server name for your account.
    For information about how to determine your account's server name, please see this article.
  8. Choose one of the following configurations:
    • If you want to use encryption, in the SMTP Port text box type 465, and then under Encryption, select Use SSL encryption.
    • If you do not want to use encryption, in the SMTP Port text box type 25, and then under Encryption, select No encryption.
    A2 Hosting strongly recommends you use encryption whenever possible.
  9. Under Authentication, select Yes: Use SMTP authentication.
  10. In the Username text box, type the e-mail address you specified in step 3.
  11. In the Password text box, type the password for the e-mail account you specified in step 10.
  12. Click Save Changes.

    You should test the new configuration to make sure that it works. To do this, follow these steps:

    • Under Send a Test Email, in the To text box, type a valid external e-mail address where you can receive a test message.
    • Click Send Test. You should receive a test message at the e-mail address you specified in the previous step. If you do not receive a message, check the settings you provided in steps 3 to 11, and then try again.

Method #2: Using the WordPress API

Instead of using a third-party plugin to handle e-mail for your site, you can use the WordPress API to develop your own custom code.

The following procedure demonstrates how to send e-mail messages using SMTP authentication. The broader topic of how to write a plugin is beyond the scope of this article. For information about how to get started writing a WordPress plugin, please visit https://codex.wordpress.org/Writing_a_Plugin.

To send e-mail messages with SMTP authentication using the WordPress API, follow these steps:

  1. In the wp-config.php file, copy and paste the following code:
    define( 'SMTP_HOST', 'server.a2hosting.com' );  // A2 Hosting server name. For example, "a2ss10.a2hosting.com"
    define( 'SMTP_AUTH', true );
    define( 'SMTP_PORT', '465' );
    define( 'SMTP_SECURE', 'ssl' );
    define( 'SMTP_USERNAME', '[email protected]' );  // Username for SMTP authentication
    define( 'SMTP_PASSWORD', 'password' );          // Password for SMTP authentication
    define( 'SMTP_FROM',     '[email protected]' );  // SMTP From address
    define( 'SMTP_FROMNAME', 'Kelly Koe' );         // SMTP From name
    
  2. Replace the values in red with the settings for your own site, and then save your changes to the wp-config.php file.
  3. In your plugin code file, copy and paste the following code:

    add_action( 'phpmailer_init', 'send_smtp_email' );
    function send_smtp_email( $phpmailer ) {
        $phpmailer->isSMTP();
        $phpmailer->Host       = SMTP_HOST;
        $phpmailer->SMTPAuth   = SMTP_AUTH;
        $phpmailer->Port       = SMTP_PORT;
        $phpmailer->SMTPSecure = SMTP_SECURE;
        $phpmailer->Username   = SMTP_USERNAME;
        $phpmailer->Password   = SMTP_PASSWORD;
        $phpmailer->From       = SMTP_FROM;
        $phpmailer->FromName   = SMTP_FROMNAME;
    }
    
  4. To send an e-mail message, call the wp_mail() function. For example:

    wp_mail("[email protected]", "Subject", "Message");

    WordPress then sends the message using the SMTP authentication settings you defined above.

More Information

For more information about the WP-Mail-SMTP plugin, please visit https://wordpress.org/plugins/wp-mail-smtp.