Magento 2 – Send Reset Password Mass Email to Customers Programmatically

customeremailmagento2password

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.

Best Answer

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 :)

Related Topic