WordPress SMTP settings via functions.php

WordPress SMTP settings via functions.php

Like many other PHP scripts, WordPress also uses PHPmailer to send transactional emails. If no email is set for sending emails, WordPress will use the default cPanel email account (username@domain.com) which isn’t pretty.

To specify which email account WordPress should use for sending all emails you need to add the login credentials for an email account either in code (your theme’s functions.php file or create a new plugin) or use an existing plugin such as WP Mail SMTP by WPForms.

To set SMTP from email in WordPress you need the following information:

  • Server address (e.g. gmail.com)
  • Username – email address
  • Password
  • Port (usually 25, 587 or 465 – ask your hosting provider)
  • SMTP Connection type: TLS or SSL

To use Secure SSL/TLS Settings in your WordPress site you can add the following code to your active theme functions.php file:

add_action( 'phpmailer_init', 'setup_phpmailer_init' );
function setup_phpmailer_init( $phpmailer ) {
    $phpmailer->Host = 'smtp.example.com';
    $phpmailer->Port = 587; // set the appropriate port: 465, 2525, etc.
    $phpmailer->Username = 'you@yourdomail.com';
    $phpmailer->Password = 'yourpassword';
    $phpmailer->SMTPAuth = true;
    $phpmailer->SMTPSecure = 'tls'; // preferable but optional
    $phpmailer->From = "you@yourdomail.com";
    $phpmailer->FromName = "Your Name";
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.