Amit

Configure SMTP on WordPress without using a plugin

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:

WordPress configuration File (wp-config.php): You can locate this file under the root directory of the WordPress website. Edit this using file manager or FTP server.


define( 'SMTP_HOST', 'mail.server.com' );  // Hosting email server name. For example, "mail.server.com"
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false)
define( 'SMTP_PORT', '465' ); // Server Port Number
define( 'SMTP_SECURE', 'ssl' ); // Encryption - ssl or tls
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', 'Amit Bera' );         // SMTP From name
define( 'SMTP_DEBUG',   0 );  // for debugging purposes only

In the above files, you’ve to change the dummy values with your values. If you don’t know where you can find all these SMTP server settings, read this article till the end.You’ve to copy and paste the following code to the theme functions file:

  • You’ve to copy and paste the following code to the theme functions file:

Theme Function File (function.php): You can locate this file in the Theme of the WordPress website. You can edit this file from the WordPress dashboard through Appearance > Theme Editor.

add_action( 'phpmailer_init', 'ab_send_smtp_email' );
function ab_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;
}

You don’t need to make any changes in the above file, copy the code, and paste it into the function.php file. Save the settings. We advise you to use a child theme; else, these changes will be removed on the theme update.

After making the above changes, you can try sending an email using the contact us form.

Find SMTP servers for Emails using cPanel

You can use this service if your hosting provider provides cPanel to manage the WordPress website. For this, you’ve to log in to the hosting provider and visit the cPanel section. And create a Business email address to send emails of WordPress.

Last Update on:March 5th, 2023 at 5:19 pm


Display the Sale price before Regular price in WooCommerce
Previous post