DKJ1337
DKJ1337

Reputation: 23

Wordpress How to send an "Set Password" E-Mail to users that got imported by script

I try to send an "Set your Password" mail, which I can customise, for a Site with a custom user role "Händler", in WordPress. These users get imported via script and XML-File.

So I want, when the script adds these ≈1200 users and they aren´t already present, to automatically or on button click receive an email with a link, where they can set a password for that user. So it needs to work like a "Verification-process", because only when these users have set their password and accept that way, that they show up on the website.

I tried some Plugins already, but none of them fits my needs completely.

Plugins I already tried:

Can anyone point me in the right direction here, cause I googled for some time now, but nothing helped me yet.

Thanks for your help, appreciate it.

Upvotes: 0

Views: 895

Answers (1)

DKJ1337
DKJ1337

Reputation: 23

I found a solution for this particular problem. I loop through the users with the custom user role and send them via wp_mail() the "Reset Password" Mail with custom content.

function mm_send_password_reset_mail(){
    $users = get_users( array( 'role' => array( 'haendler' ) ) );
    $output = '';
    foreach ( $users as $useri ) {
        $user = get_user_by('id', $useri->ID);
        $firstname = $user->first_name;
        $email = $user->user_email;
        $adt_rp_key = get_password_reset_key( $user );
        $user_login = $user->user_login;
        $rp_link = '<a href="' . network_site_url("wp-login.php?action=rp&key=$adt_rp_key&login=" . rawurlencode($user_login), 'login') . '">Passwort vergeben</a>';
        
        $output .= $user->user_email;
    
        if ($firstname == "") $firstname = "Händler";
        $message = "Hallo ".$firstname.",<br>";
        $message .= "Sie wurden auf der Seite ".get_bloginfo( 'name' )." als Händler angelegt mit der E-Mail-Adresse ".$email."<br>";
        $message .= "Klicken Sie hier, wenn Sie damit einverstanden sind, das Sie auf der Webseite als Händler öffentlich aufgelistet werden: <br>";
        $message .= $rp_link.'<br>';
    
       $subject = __("Vergeben Sie ein Passwort für ".get_bloginfo( 'name'));
       $headers = array();
    
       add_filter( 'wp_mail_content_type', function( $content_type ) {return 'text/html';});
       wp_mail( $email, $subject, $message, $headers);
    
       // Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
       remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
   }
   echo json_encode(array('user' => $output));
    die();
}

I added a custom link in the admin toolbar and call the above function via ajax. That works like a charm.

Thanks anyway.

Upvotes: 0

Related Questions