Hi cjmNY,
I did this using Logic Hooks. Use the After_Save event from the logic hooks and write the code to send emails. I am giving you an example of the code below. My code was more complex as it involved getting information from associated leads, etc… I have tried to make it simple. So there might be some errors. Please debug if it gives errors.
Open custom/modules/Opportunities/logic_hooks.php and insert this line:
$hook_array['after_save'][] = Array(76, 'sendAutomatedEmail', 'custom/modules/Opportunities/OpportunitiesAutomatedEmail.php','OpportunitiesAutomatedEmail', 'sendAutomatedEmail');
Create custom/modules/Opportunities/OpportunitiesAutomatedEmail.php and enter the following code:
<?php
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
class OpportunitiesAutomatedEmail
{
function sendAutomatedEmail(&$bean, $event, $arguments)
{
//get the related acccount record
$accounts = $bean->get_linked_beans('accounts','Account');
$opportunityid = $bean->id;
//get the assigned user record
$user = BeanFactory::getBean("Users", $bean->assigned_user_id);
//get the assigned user's email id and name
$fromid = $user->email1;
$fromname = $user->name;
foreach($accounts as $account)
{
//get the email id to whom the email should be sent
$accountemailid = $account->email1;
$accountname = $account->name;
//send the email
$this->sendEmailAccount('49faf8b7-6497-0e07-0c65-57d91a9fcd4c', $accountname, $accountemailid, $fromid, $fromname, $opportunityid);
}
}
private function sendEmailAccount($templateid, $accountname, $toemailid, $fromemailid, $fromemailname, $opportunityid)
{
require_once("include/SugarPHPMailer.php");
//create new email object
$mailer=new SugarPHPMailer();
//create new emailtemplate object
$email_template = new EmailTemplate();
$email_template->disable_row_level_security = true;
//retrieve the email template
$email_template->retrieve($templateid);
//replace the entity merge codes with proper values in the email template
$email_template->body_html = str_replace('$contact_first_name', $accountname, $email_template->body_html);
$mailer->Body = wordwrap($email_template->body_html, 900);
//set the from id, from name, subject and to
$mailer->From = $fromemailid;
$mailer->FromName = $fromemailname;
$mailer->Subject = $email_template->subject;
$mailer->AddAddress($toemailid, $accountname);
$mailer->IsHTML(true);
$mailer->prepForOutbound();
$mailer->setMailerForSystem();
//send the email
if (!$mailer->Send())
{
$GLOBALS['log']->fatal("OpportunityUpdatesHook: Could not send email: " . $mailer->ErrorInfo);
return false;
}
else
{
//if the send email succeeds then log the email object with the opportunity record
$this->logEmail($toemailid, $mailer, $opportunityid, $bean->assigned_user_id);
return true;
}
}
private function logEmail($email, $mailer, $entityid = null, $ownerid)
{
require_once('modules/Emails/Email.php');
$emailObj = new Email();
$emailObj->to_addrs = $email;
$emailObj->type= 'out';
$emailObj->deleted = '0';
$emailObj->name = $mailer->Subject;
$emailObj->description = $mailer->AltBody;
$emailObj->description_html = $mailer->Body;
$emailObj->from_addr = $mailer->From;
if ($entityid)
{
$emailObj->parent_type = "Opportunities";
$emailObj->parent_id = $entityid;
}
$emailObj->date_sent = TimeDate::getInstance()->nowDb();
$emailObj->modified_user_id = $ownerid;
$emailObj->created_by = $ownerid;
$emailObj->assigned_user_id = $ownerid;
$emailObj->status = 'sent';
$emailObj->save();
}
}
?>