1

How to send confirmation email at the time of customer creation programatically ?

Customer created,but I cannot send confirmation email? Any help?

 public function createCustomerSocial($data, $store)
{
    /** @var CustomerInterface $customer */
    $customer = $this->customerDataFactory->create();
    $customer->setFirstname($data['firstname'])
        ->setLastname($data['lastname'])
        ->setEmail($data['email'])
        ->setStoreId($store->getId())
        ->setWebsiteId($store->getWebsiteId())
        ->setCreatedIn($store->getName());

    try {
        // If customer exists existing hash will be used by Repository
        $customer = $this->customerRepository->save($customer);

        $objectManager     = \Magento\Framework\App\ObjectManager::getInstance();
        $mathRandom        = $objectManager->get('Magento\Framework\Math\Random');
        $newPasswordToken  = $mathRandom->getUniqueHash();
        $accountManagement = $objectManager->get('Magento\Customer\Api\AccountManagementInterface');
        $accountManagement->changeResetPasswordLinkToken($customer, $newPasswordToken);
        $subscriber= $objectManager->create('Magento\Newsletter\Model\SubscriberFactory');
        $subscriber->create()->subscribe($data['email']);

//            if ($this->apiHelper->canSendPassword($store)) {
                $this->getEmailNotification()
                    ->newAccount($customer, EmailNotificationInterface::NEW_ACCOUNT_EMAIL_CONFIRMATION);
            $this->getEmailNotification()
                ->newAccount($customer, EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED);
//            }

            $this->setAuthorCustomer($data['identifier'], $customer->getId(), $data['type']);
        } catch (AlreadyExistsException $e) {
            throw new InputMismatchException(
                __('A customer with the same email already exists in an associated website.')
            );
        } catch (\Exception $e) {
            if ($customer->getId()) {
                $this->_registry->register('isSecureArea', true, true);
                $this->customerRepository->deleteById($customer->getId());
            }
            throw $e;
        }

        /** @var Customer $customer */
        $customer = $this->customerFactory->create()->load($customer->getId());

        return $customer;
    }
3

Try to add this code when you creating new customer.

$customer->sendNewAccountEmail();
  • $this->getEmailNotification() ->newAccount($customer, EmailNotificationInterface::NEW_ACCOUNT_EMAIL_CONFIRMATION); this is the same , not working – alexey boltynov Feb 11 at 14:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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