cancel
Showing results for 
Search instead for 
Did you mean: 

Duplicate template id Error When Sending Email Programmatically in Magento 2 (“dotmailer_current_tem

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Duplicate template id Error When Sending Email Programmatically in Magento 2 (“dotmailer_current_tem

Registry key "dotmailer_current_template_id" already exists in /var/www/html/vendor/magento/framework/Registry.php:60

I am getting this error when I try to programmatically send an email with magento 2.

My code is as a follows. The AmastyConfig object has methods that check if my configurations are valid before sending the email.

public function sendStartSubscriptionEmail( AmastySubscriptionInterface $subscription,$storeId){ $customerEmail= $this->customerRepositoryInterface->getById($subscription->getCustomerId())->getEmail(); if ($this->AmastyConfig->isNotifySubscriptionPurchased($storeId)) { $template = $this->AmastyConfig->getEmailTemplateSubscriptionPurchased($storeId); //$this->Notifier->sendEmail( $subscription, $template, $storeId, $customerEmail); }}

This is the definition of the send email class.

public function sendEmail( AmastySubscriptionInterface $subscription, string $template, int $storeId, string $email, array $templateVariables = [] ) { $templateVariables = array_merge($this->prepareTemplateVariables($subscription), $templateVariables);

    $data = new DataObject(
        [
            'template'           => $template,
            'store_id'           => $storeId,
            'email_recipient'    => $email,
            'email_sender'       => $this->config->getEmailSender($storeId),
            'template_variables' => $templateVariables
        ]
    );

    $this->eventManager->dispatch('amasty_recurring_send_email', ['email_data' => $data]);
}

}

This is the Observer:

class Notification implements ObserverInterface { /** * @var TransportBuilder */ private $transportBuilder;

public function __construct(TransportBuilder $transportBuilder)
{
    $this->transportBuilder = $transportBuilder;
}

/**
 * @param Observer $observer
 *
 * @throws \Exception
 */
public function execute(Observer $observer)
{
    $data = $observer->getData('email_data');

    if (!$data) {
        throw new \Exception('Email data not specified');
    }

    /** @var TransportBuilder $transportBuilder */
    $transportBuilder = clone $this->transportBuilder;

    $transportBuilder->setTemplateIdentifier($data['template'] ?? null)
        ->setTemplateVars($data['template_variables'] ?? [])
        ->setTemplateOptions(
            [
                Area::PARAM_AREA=> Area::AREA_FRONTEND,
                Store::ENTITY => $data['store_id'] ?? null
            ]
        )
        ->setFrom($data['email_sender'] ?? null)
        ->addTo($data['email_recipient'] ?? null);

    /** @var \Magento\Framework\Mail\TransportInterface $transport */
    $transport = $transportBuilder->getTransport();

    $transport->sendMessage();
}

}