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 WP Mail SMTP, and then click Settings. The WP Mail SMTP general settings page appears.
  3. Under Mail, 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 Return Path, select the Set the return-path to match the From Email check box.
  6. Under Mailer, select Other SMTP.
  7. Under Other SMTP, 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 SSL.
    • If you do not want to use encryption, in the SMTP Port text box type 25, and then under Encryption, select None.
    A2 Hosting strongly recommends you use encryption whenever possible.
  9. Under Authentication, select On.
  10. In the SMTP Username text box, type the e-mail address you specified in step 3.
  11. In the SMTP Password text box, type the password for the e-mail account you specified in step 10.
  12. Click Save Settings.

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

    • Click the Email Test tab, and then in the Send To text box, type a valid external e-mail address where you can receive a test message.
    • Click Send Email. 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 develop your own custom code and use the wp_mail() function in the WordPress API.

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://developer.wordpress.org/plugins.

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.

Get WordPress Hosting

Article Details

Other Articles in This Category

Show More

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

 

 

Loading