WordPress.org

Ready to get started?Download WordPress

Forums

Send email at failed login using wp_login_failed (6 posts)

  1. Rrrr7
    Member
    Posted 1 year ago #

    Hello everyone, this is my first post here !

    I am trying to make a function that automatically sends an email to a specified address when a failed login attempt occurs using the wp_login_failed hook but some reason I can't make it work and cannot figure out why.

    I tried with this code in my theme's functions.php file

    add_action('wp_login_failed','email_at_login_failed');
    function email_at_login_failed( $username ) {
         wp_mail('me@example.net', 'Failed login attempt', 'Failed login message');
              exit;
         }
    }

    I does not work, I get no email. I read that wp_failed_login only fires up if both a wrong username and password combination is entered.

    Email works ok, I tried adding just wp_mail('me@example.net', 'Failed login attempt', 'Failed login message'); and it works.

    Sorry for my bad english !

  2. curlybracket
    Member
    Posted 1 year ago #

    add_action('wp_login_failed','email_at_login_failed');
    function email_at_login_failed( $username ) {
        wp_mail('myrealmail@myrealdomain.com', 'Failed login attempt by '.$username, 'Failed login message');
    }

    Just tried it on a clean install with Twentytwelve theme. This code works perfectly.. Just needed to remove the last curlybracket. I entered only a wrong password and I received the email.

    You could btw use this variable for the email receiver
    <?php $admin_email = get_settings('admin_email'); ?>

  3. Rrrr7
    Member
    Posted 1 year ago #

    It was that damn curly bracket, I feel stupid now :).

    I do receive emails now, but only if both username and password are filled, interesting that you get emails by only filling the password field.

    So my code is

    add_action('wp_login_failed','email_at_login_failed');
    function email_at_login_failed( $username ) {
        $admin_email = get_settings('admin_email');
        $ip = $_SERVER['REMOTE_ADDR'];
        wp_mail($admin_email, 'Failed login attempt', 'Failed login with username' . ' ' .$username . 'and IP ' .$ip);
    }

    This function sends an email containing the (wrong) username entered and the IP of the person's computer to the admin's email address entered in WP > Settings > General.

    I will work on it adding more useful info and post here.

    Thank you very much curlybracket

  4. Rrrr7
    Member
    Posted 1 year ago #

    Well, it works but today I noticed that I get about 30 - 40 emails at once each having the same timestamp, so something isn't right.

    Could be somehow related to W3 total cache plugin ? Or Cloudflare ?

  5. curlybracket
    Member
    Posted 1 year ago #

    Hello, glad it works!

    I don't know anything about cloudflare.

    And could this be related to cache ?
    Well.. i am not sure. Cache would probably not recall a _POST variable ?

  6. Rrrr7
    Member
    Posted 1 year ago #

    It seem it not related to either W3Total Cache plugin or Cloudflare because I tried the function on installations that have none of the above. It seems to be random, I get bursts of 30 - 90 emails with failed attempts, same IP, same timestamp and message.

    Is there another way to do this ?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.