1

I want to send an email to the admin of the website whenever a new user creates an account.

Everything works fine, except that the email sent does not contain the Customer Name and Store Name.

It looks like if the email is sent before the user is created in the database.

Here is my observer

/**
 * @var TransportBuilder
 */
protected $transportBuilder;

/**
 * @var StoreManagerInterface
 */
protected $storeManager;

/**
 * @var LoggerInterface
 */
protected $logger;

/**
 * @param TransportBuilder $transportBuilder
 * @param StoreManagerInterface $storeManager
 * @param LoggerInterface $logger
 */
public function __construct(
    TransportBuilder $transportBuilder,
    StoreManagerInterface $storeManager,
    LoggerInterface $logger
) {
    $this->transportBuilder = $transportBuilder;
    $this->storeManager = $storeManager;
    $this->logger = $logger;
}

/**
 * @param \Magento\Framework\Event\Observer $observer
 * @return $this
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
   $customer = $observer->getEvent()->getCustomer();


    // If customer data is empty then doesn't need to process
    if (!$customer) {
        return $this;
    }

    /* Receiver Detail the person who is going to receive the info that user logged in*/
    $receiverInfo = [
        'name' => 'Admin',
        'email' => 'ovazquez@transom-group.com'
    ];

    $store = $this->storeManager->getStore();

    $templateParams = ['store' => $store, 'customer' => $customer, 'administrator_name' => $receiverInfo['name']];

    $transport = $this->transportBuilder->setTemplateIdentifier(
        'phpcuong_transactional_email_customer_register_success_template'
    )->setTemplateOptions(
        ['area' => 'frontend', 'store' => $store->getId()]
    )->addTo(
        $receiverInfo['email'], $receiverInfo['name']
    )->setTemplateVars(
        $templateParams
    )->setFrom(
        'general'
    )->getTransport();

    try {
        // Send an email
        $transport->sendMessage();
    } catch (\Exception $e) {
        // Write a log message whenever get errors
        $this->logger->critical($e->getMessage());
    }
    return $this;
}

}

The problem is in

 $customer = $observer->getEvent()->getCustomer();

It´s not giving any data about the customer.

This is my events.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

<event name="customer_register_success">
    <observer name="phpcuong_transactional_email_customer_register_succes" instance="PHPCuong\TransactionalEmail\Observer\CustomerLoginSuccess" />
</event>

And finally this is the code for my email template

 {{template config_path="design/email/header_template"}}
<p class="greeting">{{trans "Hello <b>%administrator_name</b>," administrator_name=$administrator_name|row}}</p>
<p>{{trans 'The customer named <b>%customer_name</b> created an account in %store_name:' customer_name=$customer store_name=$store.getFrontendName()|row}}</p>
<p>{{trans "You don't need to do anything, this email helps you to track the activations of the customers."|row}}</p>
{{template config_path="design/email/footer_template"}}

Thank you and regards!

2

Try this

$customer = $observer->getEvent()->getCustomer();
$email = $customer->getEmail();
$first_name = $customer->getFirstname();
$last_name = $customer->getLastname();
  • Hello, I tried it but still not getting data :( – Oscar Vazquez Jun 27 at 14:08
  • Is the event triggered? – Arunprabakaran M Jun 27 at 14:09
  • Check the events.xml file is inside etc/frontend folder or etc folder? – Arunprabakaran M Jun 27 at 14:13
  • Yes, the event is triggered because the email is sent. I tried to put it in frontend and outside of frontend. If I put static information it`s displayed in the email template, so I guess the problem is in $customer = $observer->getEvent()->getCustomer(); method. – Oscar Vazquez Jun 27 at 14:19
  • $email=$customer->getEmail(); – Arunprabakaran M Jun 27 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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