public function execute()
{
$returnToEdit = false;
$originalRequestData = $this->getRequest()->getPostValue();
$customerId = $this->getCurrentCustomerId();
if ($originalRequestData) {
try {
// optional fields might be set in request for future processing by observers in other modules
$customerData = $this->_extractCustomerData();
$addressesData = $this->_extractCustomerAddressData($customerData);
// Récupération du helper
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helper = $objectManager->create('Mymodule\Customer\Helper\Data');
if ($customerId) {
$currentCustomer = $this->_customerRepository->getById($customerId);
// Vérifie en cas de modification si mail du request est vide, et mail du client est généré.
if ($originalRequestData['customer']['email'] == '' && $helper->isGeneratedEmail($currentCustomer->getEmail())) {
// Récupère le mail bidon du client.
$customerData['email'] = $currentCustomer->getEmail();
$originalRequestData['customer']['email'] = $currentCustomer->getEmail();
}
$customerData = array_merge(
$this->customerMapper->toFlatArray($currentCustomer),
$customerData
);
$customerData['id'] = $customerId;
}
// Vérifie l'existence du numéro de téléphone
$phoneExist = $helper->doesPhoneExist($customerData['telephone'], $customerId);
if ($phoneExist) {
// Message
$this->messageManager->addError(
__(
'Il existe déjà un compte avec ce numéro de téléphone. Veuillez créer ce compte avec un numéro de téléphone différent ou appeler le Service Client (0805 691 213).'
)
);
$this->_getSession()->setCustomerFormData($originalRequestData);
$resultRedirect = $this->resultRedirectFactory->create();
if ($customerId) {
// Vérifie l'existence du numéro de téléphone
if (!$phoneExist){
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
}else{
// Ajout d'attribut error dans l URL, pour rester sur le même formulaire
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true, 'error' => true]
);
}
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
return $resultRedirect;
}
/** @var CustomerInterface $customer */
$customer = $this->customerDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$customer,
$customerData,
\Magento\Customer\Api\Data\CustomerInterface::class
);
$addresses = [];
foreach ($addressesData as $addressData) {
$region = isset($addressData['region']) ? $addressData['region'] : null;
$regionId = isset($addressData['region_id']) ? $addressData['region_id'] : null;
$addressData['region'] = [
'region' => $region,
'region_id' => $regionId,
];
$addressDataObject = $this->addressDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$addressDataObject,
$addressData,
\Magento\Customer\Api\Data\AddressInterface::class
);
$addresses[] = $addressDataObject;
}
$this->_eventManager->dispatch(
'adminhtml_customer_prepare_save',
['customer' => $customer, 'request' => $this->getRequest()]
);
$customer->setAddresses($addresses);
if (isset($customerData['sendemail_store_id'])) {
$customer->setStoreId($customerData['sendemail_store_id']);
}
if (!$customer->getEmail()) {
$customer->setEmail($helper->_getNewCustomerEmail());
}
if (isset($customerData['chanel'])) {
$customer->setData('chanel', $customerData['chanel']);
}
// Save customer
if ($customerId) {
$this->_customerRepository->save($customer);
$this->getEmailNotification()->credentialsChanged($customer, $currentCustomer->getEmail());
} else {
$customer = $this->customerAccountManagement->createAccount($customer);
$customerId = $customer->getId();
}
$isSubscribed = null;
if ($this->_authorization->isAllowed(null)) {
$isSubscribed = $this->getRequest()->getPost('subscription');
}
if ($isSubscribed !== null) {
if ($isSubscribed !== '0') {
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
} else {
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
}
}
// After save
$this->_eventManager->dispatch(
'adminhtml_customer_save_after',
['customer' => $customer, 'request' => $this->getRequest()]
);
$this->_getSession()->unsCustomerFormData();
// Done Saving customer, finish save action
$this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
$this->messageManager->addSuccess(__('You saved the customer.'));
$returnToEdit = (bool)$this->getRequest()->getParam('back', false);
} catch (\Magento\Framework\Validator\Exception $exception) {
$messages = $exception->getMessages();
if (empty($messages)) {
$messages = $exception->getMessage();
}
$this->_addSessionErrorMessages($messages);
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (LocalizedException $exception) {
$this->_addSessionErrorMessages($exception->getMessage());
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (\Exception $exception) {
$this->messageManager->addException($exception, __('Something went wrong while saving the customer.'));
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
}
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($returnToEdit) {
if ($customerId) {
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
} else {
$resultRedirect->setPath('customer/index');
}
return $resultRedirect;
}
public function execute()
{
$returnToEdit = false;
$originalRequestData = $this->getRequest()->getPostValue();
$customerId = $this->getCurrentCustomerId();
if ($originalRequestData) {
try {
// optional fields might be set in request for future processing by observers in other modules
$customerData = $this->_extractCustomerData();
$addressesData = $this->_extractCustomerAddressData($customerData);
// Récupération du helper
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helper = $objectManager->create('Mymodule\Customer\Helper\Data');
if ($customerId) {
$currentCustomer = $this->_customerRepository->getById($customerId);
// Vérifie en cas de modification si mail du request est vide, et mail du client est généré.
if ($originalRequestData['customer']['email'] == '' && $helper->isGeneratedEmail($currentCustomer->getEmail())) {
// Récupère le mail bidon du client.
$customerData['email'] = $currentCustomer->getEmail();
$originalRequestData['customer']['email'] = $currentCustomer->getEmail();
}
$customerData = array_merge(
$this->customerMapper->toFlatArray($currentCustomer),
$customerData
);
$customerData['id'] = $customerId;
}
// Vérifie l'existence du numéro de téléphone
$phoneExist = $helper->doesPhoneExist($customerData['telephone'], $customerId);
if ($phoneExist) {
// Message
$this->messageManager->addError(
__(
'Il existe déjà un compte avec ce numéro de téléphone. Veuillez créer ce compte avec un numéro de téléphone différent ou appeler le Service Client (0805 691 213).'
)
);
$this->_getSession()->setCustomerFormData($originalRequestData);
$resultRedirect = $this->resultRedirectFactory->create();
if ($customerId) {
// Vérifie l'existence du numéro de téléphone
if (!$phoneExist){
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
}else{
// Ajout d'attribut error dans l URL, pour rester sur le même formulaire
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true, 'error' => true]
);
}
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
return $resultRedirect;
}
/** @var CustomerInterface $customer */
$customer = $this->customerDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$customer,
$customerData,
\Magento\Customer\Api\Data\CustomerInterface::class
);
$addresses = [];
foreach ($addressesData as $addressData) {
$region = isset($addressData['region']) ? $addressData['region'] : null;
$regionId = isset($addressData['region_id']) ? $addressData['region_id'] : null;
$addressData['region'] = [
'region' => $region,
'region_id' => $regionId,
];
$addressDataObject = $this->addressDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$addressDataObject,
$addressData,
\Magento\Customer\Api\Data\AddressInterface::class
);
$addresses[] = $addressDataObject;
}
$this->_eventManager->dispatch(
'adminhtml_customer_prepare_save',
['customer' => $customer, 'request' => $this->getRequest()]
);
$customer->setAddresses($addresses);
if (isset($customerData['sendemail_store_id'])) {
$customer->setStoreId($customerData['sendemail_store_id']);
}
if (!$customer->getEmail()) {
$customer->setEmail($helper->_getNewCustomerEmail());
}
if (isset($customerData['chanel'])) {
$customer->setData('chanel', $customerData['chanel']);
}
// Save customer
if ($customerId) {
$this->_customerRepository->save($customer);
$this->getEmailNotification()->credentialsChanged($customer, $currentCustomer->getEmail());
} else {
$customer = $this->customerAccountManagement->createAccount($customer);
$customerId = $customer->getId();
}
$isSubscribed = null;
if ($this->_authorization->isAllowed(null)) {
$isSubscribed = $this->getRequest()->getPost('subscription');
}
if ($isSubscribed !== null) {
if ($isSubscribed !== '0') {
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
} else {
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
}
}
// After save
$this->_eventManager->dispatch(
'adminhtml_customer_save_after',
['customer' => $customer, 'request' => $this->getRequest()]
);
$this->_getSession()->unsCustomerFormData();
// Done Saving customer, finish save action
$this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
$this->messageManager->addSuccess(__('You saved the customer.'));
$returnToEdit = (bool)$this->getRequest()->getParam('back', false);
} catch (\Magento\Framework\Validator\Exception $exception) {
$messages = $exception->getMessages();
if (empty($messages)) {
$messages = $exception->getMessage();
}
$this->_addSessionErrorMessages($messages);
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (LocalizedException $exception) {
$this->_addSessionErrorMessages($exception->getMessage());
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (\Exception $exception) {
$this->messageManager->addException($exception, __('Something went wrong while saving the customer.'));
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
}
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($returnToEdit) {
if ($customerId) {
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
} else {
$resultRedirect->setPath('customer/index');
}
return $resultRedirect;
}
Unable to send mail: Unknown error magento 2.3
for me when i want to save the customer's information I got this error 'Unable to send mail: Unknown error' , this is the method execute
PS : when i change 1 or 2 fields all work fine, but when i make change for 3 fields or more then the error is occurred
public function execute()
{
$returnToEdit = false;
$originalRequestData = $this->getRequest()->getPostValue();
$customerId = $this->getCurrentCustomerId();
if ($originalRequestData) {
try {
// optional fields might be set in request for future processing by observers in other modules
$customerData = $this->_extractCustomerData();
$addressesData = $this->_extractCustomerAddressData($customerData);
// Récupération du helper
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helper = $objectManager->create('Mymodule\Customer\Helper\Data');
if ($customerId) {
$currentCustomer = $this->_customerRepository->getById($customerId);
// Vérifie en cas de modification si mail du request est vide, et mail du client est généré.
if ($originalRequestData['customer']['email'] == '' && $helper->isGeneratedEmail($currentCustomer->getEmail())) {
// Récupère le mail bidon du client.
$customerData['email'] = $currentCustomer->getEmail();
$originalRequestData['customer']['email'] = $currentCustomer->getEmail();
}
$customerData = array_merge(
$this->customerMapper->toFlatArray($currentCustomer),
$customerData
);
$customerData['id'] = $customerId;
}
// Vérifie l'existence du numéro de téléphone
$phoneExist = $helper->doesPhoneExist($customerData['telephone'], $customerId);
if ($phoneExist) {
// Message
$this->messageManager->addError(
__(
'Il existe déjà un compte avec ce numéro de téléphone. Veuillez créer ce compte avec un numéro de téléphone différent ou appeler le Service Client (0805 691 213).'
)
);
$this->_getSession()->setCustomerFormData($originalRequestData);
$resultRedirect = $this->resultRedirectFactory->create();
if ($customerId) {
// Vérifie l'existence du numéro de téléphone
if (!$phoneExist){
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
}else{
// Ajout d'attribut error dans l URL, pour rester sur le même formulaire
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true, 'error' => true]
);
}
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
return $resultRedirect;
}
/** @var CustomerInterface $customer */
$customer = $this->customerDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$customer,
$customerData,
\Magento\Customer\Api\Data\CustomerInterface::class
);
$addresses = [];
foreach ($addressesData as $addressData) {
$region = isset($addressData['region']) ? $addressData['region'] : null;
$regionId = isset($addressData['region_id']) ? $addressData['region_id'] : null;
$addressData['region'] = [
'region' => $region,
'region_id' => $regionId,
];
$addressDataObject = $this->addressDataFactory->create();
$this->dataObjectHelper->populateWithArray(
$addressDataObject,
$addressData,
\Magento\Customer\Api\Data\AddressInterface::class
);
$addresses[] = $addressDataObject;
}
$this->_eventManager->dispatch(
'adminhtml_customer_prepare_save',
['customer' => $customer, 'request' => $this->getRequest()]
);
$customer->setAddresses($addresses);
if (isset($customerData['sendemail_store_id'])) {
$customer->setStoreId($customerData['sendemail_store_id']);
}
if (!$customer->getEmail()) {
$customer->setEmail($helper->_getNewCustomerEmail());
}
if (isset($customerData['chanel'])) {
$customer->setData('chanel', $customerData['chanel']);
}
// Save customer
if ($customerId) {
$this->_customerRepository->save($customer);
$this->getEmailNotification()->credentialsChanged($customer, $currentCustomer->getEmail());
} else {
$customer = $this->customerAccountManagement->createAccount($customer);
$customerId = $customer->getId();
}
$isSubscribed = null;
if ($this->_authorization->isAllowed(null)) {
$isSubscribed = $this->getRequest()->getPost('subscription');
}
if ($isSubscribed !== null) {
if ($isSubscribed !== '0') {
$this->_subscriberFactory->create()->subscribeCustomerById($customerId);
} else {
$this->_subscriberFactory->create()->unsubscribeCustomerById($customerId);
}
}
// After save
$this->_eventManager->dispatch(
'adminhtml_customer_save_after',
['customer' => $customer, 'request' => $this->getRequest()]
);
$this->_getSession()->unsCustomerFormData();
// Done Saving customer, finish save action
$this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
$this->messageManager->addSuccess(__('You saved the customer.'));
$returnToEdit = (bool)$this->getRequest()->getParam('back', false);
} catch (\Magento\Framework\Validator\Exception $exception) {
$messages = $exception->getMessages();
if (empty($messages)) {
$messages = $exception->getMessage();
}
$this->_addSessionErrorMessages($messages);
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (LocalizedException $exception) {
$this->_addSessionErrorMessages($exception->getMessage());
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
} catch (\Exception $exception) {
$this->messageManager->addException($exception, __('Something went wrong while saving the customer.'));
$this->_getSession()->setCustomerFormData($originalRequestData);
$returnToEdit = true;
}
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($returnToEdit) {
if ($customerId) {
$resultRedirect->setPath(
'customer/*/edit',
['id' => $customerId, '_current' => true]
);
} else {
$resultRedirect->setPath(
'customer/*/new',
['_current' => true]
);
}
} else {
$resultRedirect->setPath('customer/index');
}
return $resultRedirect;
}
default