Hi,
I am using the email2send function in a logic hook (Sugar 6.3 CE). It sucessfully sends out a template based email to a lead if a given field equals a certain value. After sending of the email, it is successfully attached to the lead's history.
My problem is that all mails that were sent through the logic hook take forever (20-30 seconds) until they open up when they are selected in the lead's history.
Interesting is that if I send an email directly through the email client within the CRM and have it attached to the lead's history, it opens quickly without any delay.
could this be some kind of errorneous implementation of the mail2send function that causes Sugar to run into an infinite loop?
This is my code:
PHP Code:
<?php
class hookMailer {
function dispatch(&$bean, $event, $arguments)
{
require_once('modules/EmailTemplates/EmailTemplate.php');
require_once('include/SugarPHPMailer.php');
require_once('modules/Emails/Email.php');
/////
//RECIPE DEFINITION
/////
$recipes = array(
"recipe_1" => array(
"triggerOnField" => "lead_source",
"triggerOnFieldValue" => "Website_General_Inquiry_Form",
"emailTemplateID" => "7543cc31-47a8-3a47-7579-4ee38176f57e",
"monitoredModuleName" => 'Leads'
),
"recipe_2" => array(
"triggerOnField" => "lead_source",
"triggerOnFieldValue" => "Website_PPStudy_Inquiry_Form",
"emailTemplateID" => "efa203e5-21ae-6d74-b60b-4ee4b53b2474",
"monitoredModuleName" => 'Leads'
),
);
/////
//DEBUG
/////
//IF SET TO TRUE, A MESSAGE WILL BE SENT REGARDLESS OF RECIPE FILTERS
$debug = false;
/////
//Better don't change anything below this line unless you know what your are doing
/////
//walk through every recipe and test it!
foreach($recipes as $recipe){
$triggerOnField=$recipe['triggerOnField'];
$triggerOnFieldValue=$recipe['triggerOnFieldValue'];
$emailTemplateID=$recipe['emailTemplateID'];
$monitoredModuleName=$recipe['monitoredModuleName'];
//Initialize some variables
$beanID = $bean->id;
$beanStatus = $bean->$triggerOnField;
//Get the TO name and e-mail address for the message
$rcpt_name = $bean->first_name . ' ' . $bean->last_name;
$rcpt_email = $bean->email1;
//Check if we have a Field that matches a value as defined in our recipe list
if ($beanStatus == $triggerOnFieldValue || $debug == true)
{
$emailTemp = new EmailTemplate();
$emailTemp->disable_row_level_security = true;
//Fetch template by TemplateID
$emailTemp->retrieve($emailTemplateID);
$mail = new Email();
$defaults = $mail->getSystemDefaultEmail();
$email_template_body=$emailTemp->body_html;
$email_template_subject=$emailTemp->subject;
$object_arr_leads = array();
$object_arr_leads[$monitoredModuleName] = $beanID;
$email_template_body = $emailTemp->parse_template($email_template_body,$object_arr_leads);
$email_template_subject = $emailTemp->parse_template($email_template_subject,$object_arr_leads);
//$mail->From = $defaults['email'];
//$mail->FromName = $defaults['name'];
//$mail->ClearAllRecipients();
//$mail->ClearReplyTos();
//$mail->AddAddress($rcpt_email, $rcpt_name);
//$mail->Subject = from_html($email_template_subject);
//$mail->Body_html = from_html($email_template_body);
//$mail->Body = wordwrap($email_template_body, 900);
$mail->IsHTML = true; //Omit or comment out this line if plain text
//$mail->prepForOutbound();
//$mail->setMailerForSystem();
$new_email->parent_type = "Leads";
$new_email->parent_id = $beanID;
$new_email->name = $email_template_subject;
$new_email->type = 'out';
$new_email->to_addrs = $rcpt_email;
$request = array();
$request['sendSubject'] = from_html($email_template_subject);
$request['sendDescription'] = from_html($email_template_body);
$request['sendTo'] = $rcpt_email;
//Account information is from inbound_email table
$request['fromAccount'] = $defaults['name'];
$request['addressFrom1'] = $defaults['email'];
$request['parent_id'] = $beanID;
$request['parent_type'] = "Leads";
$request['saveToSugar'] = "1";
$request['addressTo1'] = $rcpt_email;
//$request['sendCc'] = '';
//$request['sendBcc'] = '';
//$request['sendCharset'] = '';
$_REQUEST['sendSubject'] = $email_template_subject;
//This is the body of the email
$_REQUEST['sendDescription'] = from_html($email_template_body);
$_REQUEST['sendTo'] = $rcpt_email;
$_REQUEST['setEditor'] = "1";
$_REQUEST['sendCharset'] = "ISO-8859-1";
$_REQUEST['addressTo2'] = $rcpt_email;
$_REQUEST['emailUIAction'] = "sendEmail";
$_REQUEST['addressFrom1'] = $defaults['email'];
$_REQUEST['fromAccount'] = $defaults['name'];
$_REQUEST['saveToSugar'] = "1";
$_REQUEST['addressTo1'] = $rcpt_email;
$_REQUEST['parent_id'] = $beanID;
$_REQUEST['parent_type'] = "Leads";
//$_REQUEST['sendCc'] = '';
//$_REQUEST['sendBcc'] = '';
//$_REQUEST['sendCharset'] = '';
$query = "Select * from notes where parent_id = '$emailTemplateID' and deleted = 0";
$resultAttachments = $bean->db->query($query, true);
$num_row = $bean->db->getRowCount($resultAttachments);
$counter = 1;
if ($num_row > 0) {
while($rowAttachment = $bean->db->fetchByAssoc($resultAttachments))
{
//get each row attachment id's and seperate with ::
//$rowAttachment = $this->db->fetchByAssoc($resultAttachment);
$attachmentId = $rowAttachment['id'];
if ($num_row == 1 ) {
$finalAttachmentId = $attachmentId;
}
elseif ($counter < $num_row) {
$finalAttachmentId = $finalAttachmentId .$attachmentId ."::";
$counter = $counter + 1;
}
else{
$finalAttachmentId = $finalAttachmentId .$attachmentId;
}
}
$request['templateAttachments'] = $finalAttachmentId;
}
$mail->email2init();
$result = $mail->email2Send($request);
//unset($mail);
//Send the message, log if error occurs
if (!$result)
{
$GLOBALS['log']->fatal('ERROR: Message Send in hookMailer failed');
}
}
}
}
}
?>


LinkBack URL
About LinkBacks



Reply With Quote
Bookmarks