Sending Password Reset Link in Magento Auto-Generated Email Templates
When managing customer accounts in Magento from the admin interface, you have the ability to reset passwords for your customers directly. By default, this process sends an email containing the new password and a link to log into their account. However, you may wish to include a password reset link in this same email template for added convenience.
Let’s explore how to achieve this while avoiding the error message “Your password reset link has expired.”
Understanding the Default Email Template
The default Magento email template used for sending new passwords is located at:
app/locale/en_US/template/email/password_new.html
This file contains the HTML structure of the email, including placeholders for dynamic content like the customer’s name and the new password.
Modifying the Email Template
To include a password reset link in this email, you’ll need to modify the template. You’re on the right track by attempting to insert the following line:
{{store url="customer/account/resetpassword/" _query_id=$customer.id _query_token=$customer.rp_token}}
However, it’s important to understand that Magento’s core functionality doesn’t support sending reset links this way from the admin interface. The rp_token field is only generated and used when a customer requests a password reset directly.
A Workaround Approach
While Magento doesn’t provide an out-of-the-box solution for this, you can achieve it with a bit of customization:
- Override the Password Reset Controller
Create an override for the Magento_Customer module’s resetpasswordpost action located in vendor/magento/module-customer/Controller/Account/Resetpasswordpost.php.
- Generate a New Token and Save It to the Customer Record
In your override, after processing the password reset request, generate a new random token (using Magento’s built-in Zend_Crypt) and save it as a custom attribute on the customer record.
$customer->setData('my_password_reset_token', $newToken);
$customer->save();
- Insert the Reset Link in the Email Template
Update your password_new.html template to include the reset link using the new token:
<a href="{{store url="customer/account/resetpassword/" _query_id=$customer.id _query_token=$my_password_reset_token}}">Reset Your Password</a>
- Pass the Token to the Template
In your override, set a variable for the $my_password_reset_token in the email context data:
$transport->setTemplateVars([
'my_password_reset_token' => $newToken,
]);
Conclusion
While Magento’s built-in password reset functionality from the admin interface doesn’t support including a reset link in the auto-generated email, you can achieve this with some custom development.
By overriding the core controller and generating a new token for each admin-initiated password reset, you can then include this token in your email template. This provides customers with a convenient way to request a password reset if needed, directly from the email containing their new password sent by the admin.
Remember, always backup your codebase before making changes, and thoroughly test any overrides in a development environment before deploying them to production.
For more advanced Magento customization techniques like this, consider exploring our Magento Development Services at Laravel Company. Our team of experienced Magento developers can help you implement complex solutions tailored to your business needs.