Wordpress reset password email - Plain Text to HTML

9.1k Views Asked by At

I need help with the below script

public function replace_retrieve_password_message( $message, $key, $user_login, $user_data ) {
    // Create new message
    $msg  = __( 'Hello!', 'personalize-login' ) . "\r\n\r\n";
    $msg .= sprintf( __( 'You asked us to reset your password for your account using the email address %s.', 'personalize-login' ), $user_login ) . "\r\n\r\n";
    $msg .= __( "If this was a mistake, or you didn't ask for a password reset, just ignore this email and nothing will happen.", 'personalize-login' ) . "\r\n\r\n";
    $msg .= __( 'To reset your password, visit the following address:', 'personalize-login' ) . "\r\n\r\n";
    $msg .= site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' ) . "\r\n\r\n";
    $msg .= __( 'Thanks!', 'personalize-login' ) . "\r\n";

    return $msg;
}

This is replacing the Wordpress reset password email. It functions great and is working a treat. However, it is not very pretty and I want to replace it with an HTML email.

Is there a way that I can add HTML to this and still keep the function of printing the URL and user email address? For example;

<div>
  <h1>Hello</h1>
  <p>You asked us to reset your password for your account using the email address <?php PRINT USER EMAIL ?></p>
  <a href="<?php PRINT URL ?></a>
</div>

I know that the above is very basic and the PHP is not correct but if I wanted to replace the Plain Text email with this, how would I do it? Is it perhaps possible to create a separate PHP file with the HTML content and return the content from this template?

Your time is always greatly appreciated.

2

There are 2 best solutions below

0
Darren On BEST ANSWER

The solution to my question was done in two parts

First was to add to the functions.php

function email_set_content_type(){
return "text/html";
}

add_filter( 'wp_mail_content_type','email_set_content_type' );

Second is added within a custom plugin as shown below

    $msg  = __( "<div><h1>Hello!</h1>", 'personalize-login' ) . "\r\n\r\n";
    $msg  = __( "<p>You asked us to reset your password for your account using the email address", 'personalize-login' ) . "\r\n\r\n";
    $msg .= sprintf( __( "%s.</p>", 'personalize-login' ), $user_login ) . "\r\n\r\n";
    $msg .= __( "<a href='", 'personalize-login' ) . "\r\n\r\n";
    $msg .= site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' ) . "\r\n\r\n";
    $msg .= __( "'></a></div>", 'personalize-login' ) . "\r\n\r\n";

    return $msg;

I have now used this method to replace all core Wordpress Messages with entire email templates, including html body and head tags.

Thank you for those who had an input into my solution.

4
Anand Choudhary On

Yes, Darren Mathers, you can use something like that

function replace_retrieve_password_message($message, $key, $user_login,   $user_data) {
// Create new message

  $msg  = '<div><h1>Hello!, personalize-login</h1>';
  $msg .= '<p>You asked us to reset your password for your account using the email address personalize-login </p>'.$user_login;
  $msg .= "<p>If this was a mistake, or you didn't ask for a password reset, just ignore this email and nothing will happen personalize-login";
  $msg .= '<p> To reset your password, visit the following address: personalize-login';

  $msg .= '<p>'.site_url( "wp-login.php?action=rp&key=$key&login=".rawurlencode($user_login),'login').'</p>' 
  $msg .= '<p>Thanks! personalize-login</p> </div>';

  return $msg;

}