You can try below code for sending forgot/reset password emails.
public function __construct(
Magento\Framework\App\Action\Context $context,
Magento\Customer\Model\Session $customerSession,
Magento\Customer\Api\AccountManagementInterface $customerAccountManagement,
) {
$this->session = $customerSession;
$this->customerAccountManagement = $customerAccountManagement;
parent::__construct($context);
}
public function sendCustomMail()
{
$email = 'test@example.com'; // loop your email.
if (!\Zend_Validate::is($email, 'EmailAddress')) {
$this->session->setForgottenEmail($email);
$this->messageManager->addErrorMessage(__('Please correct the email address.'));
}
try {
$this->customerAccountManagement->initiatePasswordReset(
$email,
AccountManagement::EMAIL_RESET
);
} catch (NoSuchEntityException $e) {
// Do nothing, we don't want anyone to use this action to determine which email accounts are registered.
} catch (\Exception $exception) {
$this->messageManager->addExceptionMessage(
$exception,
__('We\'re unable to send the password reset email.')
);
}
$this->messageManager->addSuccessMessage($this->getSuccessMessage($email));
}
Make sure it should extend \Magento\Customer\Controller\AbstractAccount
You can take the reference of /vendor/magento/module-customer/Controller/Account/ForgotPasswordPost.php
.
Prepare the code accordingly.
Hope it helps.