Sometimes, Your client doesn’t want to rely on plugins for security reasons, so at that time he/she might ask you to develop a code to setup SMTP in WordPress. Are you ready to do that? Let’s just do it.
Why not to Use Plugins for SMTP?
We are not completely in favor to avoid the use of any SMTP plugins, but we should avoid plugins as much as we can for security reasons. You should avoid such plugins which you can do with simple code snippets. You can find a very useful code snippet on our blog.
We have found some highly vulnerable plugins, which helps hackers to easily hack your site. Our intentions are not to defame any plugins here, we just want to aware our readers about the actual reality of the plugins. So check some below screenshot of SMTP plugins reviews.
We hope the above reviews help you to understand how security is very important for your site. If your site hacked then it will waste your lots of time & money, even you lost your goodwill. So let’s add SMTP to your site by simple code snippet and make it more secure.
Setup SMTP in WordPress
There are 2 ways to do this, but we will follow a more secure way. Generally, We are not storing any credentials on function.php file. So we will add our SMTP credentials in the wp-config.php file and rest code in function.php. Let’s just add it.
- Add the following code to your wp-config.php file and edit it according to the SMTP credentials of your mail. You can place the following code after the other constants defined like WP_DEBUG.
/**
* 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
On the above code snippet, We have defined constants. Constants are global variables that can be used anywhere across the script (functions, plugins, etc.). One big advantage of a constant is the value cannot be changed during the script execution.
- Add the following code to your function.php file and we are done.
/*
* SMTP Mail sender help you to prevent mail goes to spam folder
*/
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;
On the above code snippet, we have used phpmailer_init
hook which allows us to pass extra parameters to the PHP’s mail function. So we have passed our SMTP parameters and we are done with the setup. Now, let’s send an email for the testing.
That’s all for the day. We hope this article helped you to setup SMTP in WordPress without any plugins.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂 Keep Smiling! Happy Coding!
You said that phpmailer_init hook allows us to pass extra parameters to the PHP’s mail function. In fact we are talking about the wp_mail function, isn’t it? So we can use it with just the mail, subject and content to be sent. Am I correct? Thanks!
Hi Raul, Actually, we are talking about how to set up SMTP to send emails.
wp_mail()
function sends email which will considered as a spam or it won’t send without SMTP so.