How to send mail in WordPress using SMTP without using plugins

SMTP:

Simple Mail Transfer Protocol (SMTP) is an ASCII-based protocol. It establishes a TCP connection to port number 25 of the destination machine. Here the sending machine is known as the client and the receiving machine is known as the server.

 

WordPress, by default, is configured to use the PHP mail function to handle email services. However, the PHP mail function is not configured correctly, and some service providers disable this mail function to reduce SPAM mail. The issue is that most WordPress companies need to have their servers appropriately configured with PHP mails.

 

Send emails via SMTP in WordPress without using plugins:

Step-1: First, Add this code to the wp-config.php file of your WordPress website.

define( 'SMTP_USER', 'user@example.com' );
define( 'SMTP_PASS', 'smtp password' );
define( 'SMTP_HOST', 'smtp.example.com' );
define( 'SMTP_FROM', 'your@example.com' );
define( 'SMTP_NAME', 'your Name' );
define( 'SMTP_PORT', '25' );
define( 'SMTP_SECURE', 'tls' );
define( 'SMTP_AUTH', true );
define( 'SMTP_DEBUG', 0 );

Step 2: Add the following lines of code to the theme functions file (functions.php).

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->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}

Leave a Reply

Your email address will not be published. Required fields are marked *