I’m doing a plugin that accesses remote data to change a specific token at the email sending . The function is onEmailGenerate , but in this role I noticed that the mautic calls again for a different lead without even finished making the sending of the first email . Aparentente is a competition problem someone went through it ?
Here is my source code:
use MauticCoreBundleEventListenerCommonSubscriber;
use MauticEmailBundleEmailEvents;
use MauticEmailBundleEventEmailBuilderEvent;
use MauticEmailBundleEventEmailSendEvent;
/**
Class EmailSubscriber
*/
class EmailSubscriber extends CommonSubscriber
{
/**
@return array / static public function getSubscribedEvents() { return array( EmailEvents::EMAIL_ON_BUILD => array(‘onEmailBuild’, 0), EmailEvents::EMAIL_ON_SEND => array(‘onEmailGenerate’, 0), EmailEvents::EMAIL_ON_DISPLAY => array(‘onEmailGenerate’, 0) ); }
/*
@param EmailBuilderEvent $event / public function onEmailBuild(EmailBuilderEvent $event) { $content = $this->templating->render(‘VestconBundle:SubscribedEventsEmailToken:token.html.php’); $event->addTokenSection(‘vestcon.token’, ‘plugin.vestcon.builder.header’, $content); }
/*
@param EmailSendEvent $event
*/
public function onEmailGenerate(EmailSendEvent $event)
{
$stringHtmlEmail = $this->getEmailDynamic($event);
// Set updated content
$event->setContent($stringHtmlEmail);
}
private function getEmailDynamic($event)
{
$html = $content;
try {
$content = $event->getContent();
if (strpos($content, '{cart.model1}') == true) {
$html = $this->getHtmlCart($event, '{cart.model1}');
}
else if(strpos($content, '{cart.model2}') == true) {
$html = $this->getHtmlCart($event, '{cart.model2}');
}
}
catch (Exception $ex ){ }
return $html;
}
private function getHtmlCart($event, $token)
{
// Get content
$content = $event->getContent();
$lead = $event->getLead();
$cartId = $lead[‘cart_id’];
$urlServico = ‘http://xxxxxx’;
$json_object = $this->getJson($urlServico, $cartId);
ob_start();
include (dirname(DIR).’\Views\TemplateEmails\Cart’.str_replace(array("{", “}”), “”, $token).’.html.php’);
$stingEmail = ob_get_clean();
return str_replace($token, $stingEmail, $content);
}
private function getJson($url, $id)
{
$username = ‘xxxxxx’;
$password = ‘xxxxxx’;
$postdata = http_build_query( array(‘cestaid’ => $id ) );
$opts = array(‘http’ =>
array(
‘method’ => ‘POST’,
‘header’ => “Content-Type: application/x-www-form-urlencodedrn”.
“Authorization: Basic “.base64_encode(”$username:$password”).“rn”,
‘content’ => $postdata,
‘timeout’ => 60
)
);
$context = stream_context_create($opts);
$unparsed_json = file_get_contents($url, false, $context);
$unparsed_json = mb_convert_encoding($unparsed_json, ‘HTML-ENTITIES’, “UTF-8”);
$json_object = json_decode($unparsed_json);
return $json_object;
}
}