Send Notification ( Email ) When Users Land on a Specific Page in a WordPress Site

Someone asked this question the other day. I wrote a short answer with example code and it is working fine.

This article is the longer and more detailed version of my reply.

First, all emails from a WordPress site should be sent through a SMTP email server. Else there is a good chance that your host will flag you for sending spam or the emails will never get sent.

Then use wp_mail() function. It is a wrapper around phpMailer, a robust PHP library for sending emails.

The phpMailer library uses PHP native mail() function by default. But it can be configured to send emails through a SMTP server.

An SMTP server needs the following information –

  • Server Hostname
  • Port to use
  • Username
  • Password
  • From email
  • From name

If you are using a plugin like WP Mail SMTP, then you simply have to enter the values and you are done.

But if you are doing it manually then this is what you will need to do – open the wp-config.php file and put the following code in it –

define( 'SMTP_HOST', 'my.smtpserver.com' );  
define( 'SMTP_AUTH', true );
define( 'SMTP_PORT', '465' );
define( 'SMTP_SECURE', 'TLS' );
define( 'SMTP_USERNAME', 'test@example.com' );  // Username for authentication
define( 'SMTP_PASSWORD', 'xxxxxxxx' );          // Password for authentication
define( 'SMTP_FROM',     'test@example.com' );  // From address - in email
define( 'SMTP_FROMNAME', 'Technical Support' ); // Name of sender

The italicized words are the values that you will replace with your own values.

The SMTP_AUTH is set to true as you will need to use authentication in most cases. The port SMTP_PORT default value is 465 but can be different for your server, so make sure that it is the correct port.

And of course, you will use a secure connection with the SMTP through TLS. This key SMTP_SECURE can take SSL or TLS as values. TLS is a successor to SSL protocol and has better security.

Next, open your functions.php and write the following code –

add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host       = SMTP_HOST;
    $phpmailer->SMTPAuth   = SMTP_AUTH;
    $phpmailer->Port       = SMTP_PORT;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->Username   = SMTP_USERNAME;
    $phpmailer->Password   = SMTP_PASSWORD;
    $phpmailer->From       = SMTP_FROM;
    $phpmailer->FromName   = SMTP_FROMNAME;
}

You do not have to change anything here as this action hook just initiates the phpMailer library to use SMTP ( isSMTP() ) and then retrieves the values for different parameters from the config file.

Finally, use the template_redirect hook to send the notification mail when a user lands on a specific page.

add_action('template_redirect', 'template_redirect_callback');
function template_redirect_callback() {
    global $post;
    if ( $post->ID == 2 ) {
        wp_mail(
           "test@gmail.com", 
           "Test Subject", 
           "Another Test Message"
        ); 
    }
}

The template_redirect hook is used because it fires just before the content is rendered on the screen and is the most appropriate location for sending notifications.

Here I am using post id 2, but you can use any id you want and the condition can also be changed to fit your needs.