0

Good Day Everyone,

I am trying my New User Registration form Plugin on a live site, but the emails are not being sent.

I save the new user using:

    if (empty($errors)) {
        $myplugin_newuser = wp_insert_user(array(
            'user_login' => $user_login,
            'user_email' => $user_email,
            'nickname' => $user_nickname,
            'user_pass' => $user_pass,
            'first_name' => $user_firstname,
            'last_name' => $user_lastname,
            'description' => $user_bio,
            'user_url' => $user_url,
            'user_registered' => date('Y-m-d H:i:s'),
            'role' => 'subscriber'
            ));

After that I follow the official way of WordPress to change the New User Notification found here:

// Redefine user notification function
if ( !function_exists('wp_new_user_notification') ) {

    function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {

        $user = new WP_User( $user_id );

        $user_login = stripslashes( $user->user_login );
        $user_email = stripslashes( $user->user_email );

        $message  = sprintf( __('New user registration on %s:'), get_option('blogname') ) . "\r\n\r\n";
        $message .= sprintf( __('Username: %s'), $user_login ) . "\r\n\r\n";
        $message .= sprintf( __('E-mail: %s'), $user_email ) . "\r\n";

        @wp_mail(
            get_option('admin_email'),
            sprintf(__('[%s] New User Registration'), get_option('blogname') ),
            $message
        );

        if ( empty( $plaintext_pass ) )
            return;

        $message  = __('Hi there,') . "\r\n\r\n";
        $message .= sprintf( __("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "\r\n\r\n";
        $message .= wp_login_url() . "\r\n";
        $message .= sprintf( __('Username: %s'), $user_login ) . "\r\n";
        $message .= sprintf( __('Password: %s'), $plaintext_pass ) . "\r\n\r\n";
        $message .= sprintf( __('If you have any problems, please contact me at %s.'), get_option('admin_email') ) . "\r\n\r\n";
        $message .= __('Adios!');

        wp_mail(
            $user_email,
            sprintf( __('[%s] Your username and password'), get_option('blogname') ),
            $message
        );
    }
}

I know it's said that if the password information is not passed the email will not be sent to the user, but I don't seem to be receiving the email to my admin email either.

But from my understanding of the code if '' is given that would mean it's empty but the information is passed so the email should be sent.

Is it the way I Insert the new user that is not correct? Or is it something else that am missing.

The tests were made, normal emails are being sent properly with my WP Mail Bank plugin.

Just to make sure I also tried to call the function itself, as per the line above I am passing the user_login and user_pass information:

wp_new_user_notification( $user_login, $user_pass);

EDIT Now that I use the function (which fixed a quarter of the issue), the admin receives the email, but without any details of the user_login or $user_pass (which are variables, as you can see above, I use to send the information WordPress to fill in the information inside the DB.

-- Original Message --

I was trying my new registration form plugin on a website live (Thanks @Pieter Goosen), but there is no email sent after the user has clicked register.

The question is, is it because I use a different form that there is no email sent?

I was thinking that the default email would be sent automatically?

Do I absolutely have to setup an email inside my Plugin?

Or should I expect WordPress to do it?

-----

As Pieter mentioned in the comments, yes the emails are going through as I am using a plugin to use the SMTP and not the PHP mailer by default (Plugin named: Wp Mail Bank)

The test was made BY the Plugin (to make sure there was not configuration changes on my servers) and I also disabled my plugin and try the original email from the normal Registration form of WordPress and I got both emails without any problems.

6
  • Make sure that you can actually sent emails from your host. Most hosts disable this function, or the spam filters is set too sensitive which means your mail is blocked as spam even though it is legit. I know when my wife had her buddypress site, this was one of my main issues Jun 4, 2015 at 11:46
  • Thanks for your reply once again Pieter :-) I will add details ;-) As this is tested already... Give me 2 minutes
    – pSyToR
    Jun 4, 2015 at 11:47
  • Do you call the wordpress built in signup function? Jun 4, 2015 at 12:25
  • Sorry... I'm not a professional of WordPress... Could you help me with which function you are referring to?
    – pSyToR
    Jun 4, 2015 at 12:27
  • The only one I can find that could look like that ... I use wp_insert_user
    – pSyToR
    Jun 4, 2015 at 12:29

2 Answers 2

0

I might be wrong but I think wp_insert_user function doesn't call wp_new_user_notification function. As your edit you need to call this function manually.

As you are testing your plugin in a live site I presume the error reporting is turned off and hence you are not getting any error.

if (empty($errors)) {
        $myplugin_newuser = wp_insert_user(array(
            'user_login' => $user_login,
            'user_email' => $user_email,
            'nickname' => $user_nickname,
            'user_pass' => $user_pass,
            'first_name' => $user_firstname,
            'last_name' => $user_lastname,
            'description' => $user_bio,
            'user_url' => $user_url,
            'user_registered' => date('Y-m-d H:i:s'),
            'role' => 'subscriber'
        ));
}

if( !is_wp_error( $myplugin_newuser ) ){
    wp_new_user_notification( $user_login, $user_pass);
}
else{
    // this holds the errors that might be holding you back 
    // $myplugin_newuser->get_error_messages()
}

I think there might be some problem inserting user, or else your code should work fine. Just check if the returned value is a WP_error.

3
  • As per my last edit yesterday, you are right that the function need to be called. But now that I am calling wp_new_user_notification on Admin receives the email but it's blank... I think I might just be better to create my own email WP_Mail.... What do you think?
    – pSyToR
    Jun 7, 2015 at 9:26
  • Admin receives blank email may be because user is not created at all. And as the user isn't receiving email may be because the email is not valid. Can you check what value is hold by $myplugin_newuser?
    – maksbd19
    Jun 7, 2015 at 17:01
  • The User is created and the email is good... Thanks for your input @maksbd19
    – pSyToR
    Jun 8, 2015 at 21:01
0

The problem was not related at all with my plugin or with any kind of coding mistakes.

When using W3 Total Cache the Database Cache seems to remove some data sent to functions, so the information about the username and password were not sent to the function causing the email to the user not to be sent (because there was no password given to the function) and the email sent to the admin was without any information for the same reason.

By disabling the application or only the "Database Cache" the information are sent properly and both email are sent as they were supposed to be.

Thanks everyone that tried to help me. I will post more details when I have information from W3 Total Cache team if there is a solution or it's not supposed to do like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.