Configure SMTP in WordPress without using a plugin

Tutorials 0 lượt xem

Sometimes, you don’t want to install multiple plugins for security reasons. So you can code your own SMTP configuration in WordPress. If you are ready to do it? Let’s start with me!

configure SMTP without plugin

Why not use a plugin for SMTP?

I don’t fully support not using any plugin. But as a coder, I think to avoid installing plugins as much as possible for security reasons.

I found that some plugins are very vulnerable, making it easy for hackers to break into your site. Below is my screenshot of the review of some SMTP plugins on WordPress.

SMTP plugin reviewSMTP plugin reviewSMTP plugin reviewSMTP plugin review

I hope the above reviews will help you understand how important security is to your website. If your website gets hacked it will waste a lot of your time and money. So let’s configure SMTP without a plugin!

Configure SMTP in WordPress

For security reasons, I don’t think any login information should be stored in the function.php file . So, I will add the SMTP credentials to the wp-config.php file and the rest of the code in function.php .

Add the following code to the wp-config.php file and edit it according to the SMTP credentials in your mail.

/**
 * 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
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span>
</div>

On the above code, I have defined constants. Constants are global variables that can be used anywhere across the entire project (theme, plugins, …). A big advantage of constants is that the value cannot be changed during the execution of the functions.

Next, add the following code to the function.php . file

if ( !function_exists('sc_smtp_mail_sender') ) :
 
    add_action( 'phpmailer_init', 'sc_smtp_mail_sender' );
 
    function sc_smtp_mail_sender( $phpmailer ) {
 
            $phpmailer-&gt;isSMTP();
        $phpmailer-&gt;Host       = SMTP_HOST;
        $phpmailer-&gt;SMTPAuth   = SMTP_AUTH;
        $phpmailer-&gt;Port       = SMTP_PORT;
        $phpmailer-&gt;Username   = SMTP_USER;
        $phpmailer-&gt;Password   = SMTP_PASS;
        $phpmailer-&gt;SMTPSecure = SMTP_SECURE;
        $phpmailer-&gt;From       = SMTP_FROM;
        $phpmailer-&gt;FromName   = SMTP_NAME;  
 
    } 
 
endif;
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span>
</div>

A little explanation

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

Epilogue

That’s all for today. Hope this article helped you set up SMTP in WordPress without any plugins.

If I have an error, or my code works incorrectly, please comment to 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.

Bài viết liên quan