3

We have migrated 60,000 Customers from Old System to Magento.

Now we have to send them Email for Reset Password.

https://stackoverflow.com/questions/27920153/create-and-send-email-to-customer-reset-password-link-programmatically-in-magento

https://mage2.pro/t/topic/1595/2

I have gone through above links, but it will be for Magento 1.

How to send Reset Password Email Programmatically. Need to use MailChimp for this Mass Email Sending or by GMAIL SMTP?

Need to create Cron Job? Bulk Mail Send Goes to Spam?

This Script is going to run One time only.

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Nov 2, 2016 at 3:53

2 Answers 2

7

From Outside Magento.

If you want to send All Customers Reset Password Email, then below is the code.

//place this before any script you want to calculate time
$time_start = microtime(true);
// MAGENTO START
include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\AccountManagement;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$_customers = $objectManager->create('Magento\Customer\Api\AccountManagementInterface');
$customerCollection = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
$customerCollection->load();
$i = 0;
foreach ($customerCollection as $customers) {
 $email = $customers->getData('email');
 try {
 $_customers->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) {
 echo __('We\'re unable to send the password reset email.');
 }
 echo "<pre>";
 echo $i . " Email :-" . $email . " Mail Sent";
 $i++;
}
echo "<pre>";
echo "Total Customers: " . $i;
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes other wise seconds
$execution_time = ($time_end - $time_start) / 60;
//execution time of the script
echo "<pre>";
echo 'Total Execution Time:</b> ' . $execution_time . ' Mins';

Hope it helps Magento Community :)

answered Nov 16, 2016 at 3:27
1
  • I have tried this code in Cron it gives me this error [2019年06月19日 11:29:46] main.ERROR: Cron Job gme_alerttest_cron has an error: No such entity with email = [email protected], websiteId = 0. Statistics: {"sum":0,"count":1,"realmem":0,"emalloc":0,"realmem_start":81788928,"emalloc_start":77602808} [] [] Commented Jun 19, 2019 at 11:33
2

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 = '[email protected]'; // 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.

answered Nov 2, 2016 at 4:46
2
  • 1
    Base I Know. Need to Create Cron Job? Bulk Mail goes to Spam? Commented Nov 2, 2016 at 4:54
  • Get the customer collection and make it as a loop and send it accordingly. Commented Nov 2, 2016 at 6:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.