How to setup SMTP mail in WordPress themes [without plugin]

SMTP mail setup is one of the important things to do in WordPress. There are few plugins available for this function but I don’t recommend plugins for every single thing in WordPress. Thus we can avoid extra third party codes. Here I will be discussing the good way to do it manually.

SMTP stands for “simple mail transfer protocol”. This is important if you are using shared hosting or dedicated hosting with service provider like GoDaddy or BlueHost or similar. This is useful if you are using a free theme or a premium theme. I recommend you to use a child theme to add your own code. In my next post, I will discuss how you can make this code dynamic with WordPress admin theme options.

First, you need to open your theme’s “functions.php” file. Here you can just check if there is any SMTP function exists or not. If there is no SMTP function exists, just add the following code to it –

Writing a function and passing an argument as “$phpmailer”. To know more about passing arguments in a function, please refer php manual.

function ta_phpmailer ( $phpmailer ) {
//code goes here

Now, inside the function, you need to add some code for the SMTP mail mainly for the “from” mail authentication. This is where SMTP is required if you are using emails like Gmail or AOL etc. This is another topic and I will light on this topic in my upcoming posts.

Add this code as displayed –

function ta_phpmailer( $phpmailer ) {

// First defining all the variables that is required
$host_name = ‘Your Host Name’;
$smtp_port = 25; //This can differ with different hosting. Check yours
$username_email_id = ‘email id’; //username and email id is same
$email_password = ‘your password’;
$enable_debug_mode = 1; //use 1 or 0 to enable or disable debug mode.

// This is SMTP code
$phpmailer->isSMTP(); //switching on the SMTP
$phpmailer->Host = $host_name; // Your host/outgoing server name for SMTP
$phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
$phpmailer->Port = $smtp_port; // Check your port. Standard should be 25, 80, 3535 and (465 for ssl)
$phpmailer->Username = $username_email_id ; // Your full email address
$phpmailer->Password = $email_password ; // Password for the email
$phpmailer->SMTPDebug = $enable_debug_mode; // Enable or disable Debug Mode



So you need to place your email id, password etc as shown above. To know exact port and host name, please contact your hosting provider and ask them to provide full SMTP settings for you.

Lastly, you need to call this function with “add_action” method as per WordPress standards.

Add this line of code before or after the above function –

//adding the ‘ta_phpmailer’ function to WordPress

add_action( ‘phpmailer_init’, ‘ta_phpmailer’ );

To know more about “add_action”, please consider the WordPress codex. Thanks! See you soon.


Leave a Reply

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