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:
- Log in to your WordPress site as the administrator.
- In the left-hand pane, click Plugins.
- Click Add New.
- In the Search Plugins text box, type mail smtp, and then press Enter.
- Locate WP Mail SMTP by WPForms, and then click Install Now.
- 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:
- Log in to your WordPress site as the administrator.
- In the left-hand pane, click WP Mail SMTP, and then click Settings. The WP Mail SMTP general settings page appears.
- 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.
- In the From Name text box, type the name you want to associate with the e-mail address you specified in step 3.
- Under Return Path, select the Set the return-path to match the From Email check box.
- Under Mailer, select Other SMTP.
- 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.
- 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.
- Under Authentication, select On.
- In the SMTP Username text box, type the e-mail address you specified in step 3.
- In the SMTP Password text box, type the password for the e-mail account you specified in step 10.
- 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:
- 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', 'user@example.com' ); // Username for SMTP authentication define( 'SMTP_PASSWORD', 'password' ); // Password for SMTP authentication define( 'SMTP_FROM', 'user@example.com' ); // SMTP From address define( 'SMTP_FROMNAME', 'Kelly Koe' ); // SMTP From name
- Replace the values in red with the settings for your own site, and then save your changes to the wp-config.php file.
- 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; }
- To send an e-mail message, call the wp_mail() function. For example:
wp_mail(“recipient@example.com”, “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.
source : https://www.a2hosting.co.id/kb/installable-applications/optimization-and-configuration/wordpress2/sending-e-mail-in-wordpress#:~:text=To%20send%20an%20e%2Dmail,authentication%20settings%20you%20defined%20above.