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.

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

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 = emailaddress@gmail.com, websiteId = 0. Statistics: {"sum":0,"count":1,"realmem":0,"emalloc":0,"realmem_start":81788928,"emalloc_start":77602808} [] []
    – Ghulam.M
    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 = '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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.