SMTP configuration in WordPress without using plugins

cấu hình SMTP không dùng plugin

The content discusses the importance of security in WordPress websites and suggests configuring SMTP without using plugins to avoid vulnerabilities. The process involves adding SMTP login information to the wp-config.php file and the remaining code in function.php. Constants are used to define global variables for SMTP configuration. A function in function.php is created to set up SMTP parameters using the phpmailer_init hook. The setup is completed by testing the email function. The article concludes by encouraging feedback and offering to help if any issues arise. It also recommends reading more WordPress Tips articles for similar topics.

Sometimes, you don’t want to install many plugins for security reasons. So you can code your own SMTP configuration in WordPress. Are you ready to do that? Let’s get started!

Why not use plugins for SMTP?

I am not completely in favor of not using any plugins. But as a coder, I think plugins should be avoided as much as possible for security reasons. I found that some plugins are very vulnerable, making it easy for hackers to break into your website. Here are my screenshots of some SMTP plugin reviews for WordPress. I hope the above reviews will help you understand how important security is for your website. If your website is hacked, it will waste a lot of your time and money. So let’s configure SMTP without plugins!

Configure SMTP in WordPress

For security reasons, I think it is not a good idea to store any login information in the file function.php. So I will add SMTP login information to the file wp-config.php and the rest of the code in function.php.
Add the following code to the file wp-config.php and edit it according to your mail SMTP credentials.

/** 
 * SMTP Credentials 
 */ 
define( 'SMTP_USER', 'user@example.com' ); // Username to use for SMTP authentication 
define( 'SMTP_PASS', 'smtp password' ); // Password to use for SMTP authentication 
define( 'SMTP_HOST', 'smtp.example.com' ); // The hostname of the mail server 
define( 'SMTP_FROM', 'website@example.com' ); // SMTP From email address 
define( 'SMTP_NAME', 'e.g Website Name' ); // SMTP From name 
define( 'SMTP_PORT', '25' ); // SMTP port number - likely to be 25, 465 or 587 
define( 'SMTP_SECURE', 'tls' ); // Encryption system to use - ssl or tls 
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false) 
define( 'SMTP_DEBUG', 0 ); // for debugging purposes only set to 1 or 2

In the above code, I have defined constants. Constants are global variables that can be used anywhere in the entire project (theme, plugin,…). A big advantage of constants is that the value cannot be changed during the execution of functions. Next, add the code below to the file function.php.

if ( !function_exists('sc_smtp_mail_sender') ) :

    add_action( 'phpmailer_init', 'sc_smtp_mail_sender' );

    function sc_smtp_mail_sender( $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;  

    } 

endif;

Explain a little

In the above code, I used hook phpmailer_init allows me to pass additional parameters to PHP’s mail function. That’s it, the setup is complete, you can try email to test.

See also  Guide to Creating Shortcodes: What Is an English Shortcode?

Conclusion

That’s all for today. Hope this article helped you set up SMTP in WordPress without any plugin. If I have any mistakes, or my code is not working correctly, please comment and let me know. Also, if you are interested in similar topics, read other WordPress Tips articles and follow Fanpage so you don’t miss new posts from me.

Rate this post

Related posts