actionSendEmail.php

Created Diff never expires
<?php
<?php
/**
/**
* Advanced OpenWorkflow, Automating SugarCRM.
* Advanced OpenWorkflow, Automating SugarCRM.
* @package Advanced OpenWorkflow for SugarCRM
* @package Advanced OpenWorkflow for SugarCRM
* @copyright SalesAgility Ltd http://www.salesagility.com
* @copyright SalesAgility Ltd http://www.salesagility.com
*
*
* This program is free software; you can redistribute it and/or modify
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation; either version 3 of the License, or
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with this program; if not, see http://www.gnu.org/licenses
* along with this program; if not, see http://www.gnu.org/licenses
* or write to the Free Software Foundation,Inc., 51 Franklin Street,
* or write to the Free Software Foundation,Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA 02110-1301 USA
* Fifth Floor, Boston, MA 02110-1301 USA
*
*
* @author SalesAgility <info@salesagility.com>
* @author SalesAgility <info@salesagility.com>
*/
*/




require_once __DIR__ . '/../../AOW_Actions/actions/actionBase.php';
require_once __DIR__ . '/../../AOW_Actions/actions/actionBase.php';
require_once __DIR__ . '/../../AOW_WorkFlow/aow_utils.php';
require_once __DIR__ . '/../../AOW_WorkFlow/aow_utils.php';
class actionSendEmail extends actionBase
class actionSendEmail extends actionBase
{
{
private $emailableModules = array();
private $emailableModules = array();


/**
/**
*
*
* @var int
* @var int
*/
*/
protected $lastEmailsFailed;
protected $lastEmailsFailed;


/**
/**
*
*
* @var int
* @var int
*/
*/
protected $lastEmailsSuccess;
protected $lastEmailsSuccess;


public function __construct($id = '')
public function __construct($id = '')
{
{
parent::__construct($id);
parent::__construct($id);
$this->clearLastEmailsStatus();
$this->clearLastEmailsStatus();
}
}


public function loadJS()
public function loadJS()
{
{
return array('modules/AOW_Actions/actions/actionSendEmail.js');
return array('modules/AOW_Actions/actions/actionSendEmail.js');
}
}


public function edit_display($line, SugarBean $bean = null, $params = array())
public function edit_display($line, SugarBean $bean = null, $params = array())
{
{
global $app_list_strings;
global $app_list_strings;
$email_templates_arr = get_bean_select_array(true, 'EmailTemplate', 'name', '', 'name');
$email_templates_arr = get_bean_select_array(true, 'EmailTemplate', 'name', '', 'name');


if (!in_array($bean->module_dir, getEmailableModules())) {
if (!in_array($bean->module_dir, getEmailableModules())) {
unset($app_list_strings['aow_email_type_list']['Record Email']);
unset($app_list_strings['aow_email_type_list']['Record Email']);
}
}
$targetOptions = getRelatedEmailableFields($bean->module_dir);
$targetOptions = getRelatedEmailableFields($bean->module_dir);
if (empty($targetOptions)) {
if (empty($targetOptions)) {
unset($app_list_strings['aow_email_type_list']['Related Field']);
unset($app_list_strings['aow_email_type_list']['Related Field']);
}
}


$html = '<input type="hidden" name="aow_email_type_list" id="aow_email_type_list" value="'.get_select_options_with_id($app_list_strings['aow_email_type_list'], '').'">
$html = '<input type="hidden" name="aow_email_type_list" id="aow_email_type_list" value="'.get_select_options_with_id($app_list_strings['aow_email_type_list'], '').'">
<input type="hidden" name="aow_email_to_list" id="aow_email_to_list" value="'.get_select_options_with_id($app_list_strings['aow_email_to_list'], '').'">';
<input type="hidden" name="aow_email_to_list" id="aow_email_to_list" value="'.get_select_options_with_id($app_list_strings['aow_email_to_list'], '').'">';


$checked = '';
$checked = '';
if (isset($params['individual_email']) && $params['individual_email']) {
if (isset($params['individual_email']) && $params['individual_email']) {
$checked = 'CHECKED';
$checked = 'CHECKED';
}
}


$html .= "<table border='0' cellpadding='0' cellspacing='0' width='100%' data-workflow-action='send-email'>";
$html .= "<table border='0' cellpadding='0' cellspacing='0' width='100%' data-workflow-action='send-email'>";
$html .= "<tr>";
$html .= "<tr>";
$html .= '<td id="relate_label" scope="row" valign="top"><label>' . translate(
$html .= '<td id="relate_label" scope="row" valign="top"><label>' . translate(
"LBL_INDIVIDUAL_EMAILS",
"LBL_INDIVIDUAL_EMAILS",
"AOW_Actions"
"AOW_Actions"
) . ':</label>';
) . ':</label>';
$html .= '</td>';
$html .= '</td>';
$html .= "<td valign='top'>";
$html .= "<td valign='top'>";
$html .= "<input type='hidden' name='aow_actions_param[".$line."][individual_email]' value='0' >";
$html .= "<input type='hidden' name='aow_actions_param[".$line."][individual_email]' value='0' >";
$html .= "<input type='checkbox' id='aow_actions_param[".$line."][individual_email]' name='aow_actions_param[".$line."][individual_email]' value='1' $checked></td>";
$html .= "<input type='checkbox' id='aow_actions_param[".$line."][individual_email]' name='aow_actions_param[".$line."][individual_email]' value='1' $checked></td>";
$html .= '</td>';
$html .= '</td>';


if (!isset($params['email_template'])) {
if (!isset($params['email_template'])) {
$params['email_template'] = '';
$params['email_template'] = '';
}
}
$hidden = "style='visibility: hidden;'";
$hidden = "style='visibility: hidden;'";
if ($params['email_template'] != '') {
if ($params['email_template'] != '') {
$hidden = "";
$hidden = "";
}
}


$html .= '<td id="name_label" scope="row" valign="top"><label>' . translate(
$html .= '<td id="name_label" scope="row" valign="top"><label>' . translate(
"LBL_EMAIL_TEMPLATE",
"LBL_EMAIL_TEMPLATE",
"AOW_Actions"
"AOW_Actions"
) . ':<span class="required">*</span></label></td>';
) . ':<span class="required">*</span></label></td>';
$html .= "<td valign='top'>";
$html .= "<td valign='top'>";
$html .= "<select name='aow_actions_param[".$line."][email_template]' id='aow_actions_param_email_template".$line."' onchange='show_edit_template_link(this,".$line.");' >".get_select_options_with_id($email_templates_arr, $params['email_template'])."</select>";
$html .= "<select name='aow_actions_param[".$line."][email_template]' id='aow_actions_param_email_template".$line."' onchange='show_edit_template_link(this,".$line.");' >".get_select_options_with_id($email_templates_arr, $params['email_template'])."</select>";


$html .= "&nbsp;<a href='javascript:open_email_template_form(".$line.")' >".translate('LBL_CREATE_EMAIL_TEMPLATE', 'AOW_Actions')."</a>";
$html .= "&nbsp;<a href='javascript:open_email_template_form(".$line.")' >".translate('LBL_CREATE_EMAIL_TEMPLATE', 'AOW_Actions')."</a>";
$html .= "&nbsp;<span name='edit_template' id='aow_actions_edit_template_link".$line."' $hidden><a href='javascript:edit_email_template_form(".$line.")' >".translate('LBL_EDIT_EMAIL_TEMPLATE', 'AOW_Actions')."</a></span>";
$html .= "&nbsp;<span name='edit_template' id='aow_actions_edit_template_link".$line."' $hidden><a href='javascript:edit_email_template_form(".$line.")' >".translate('LBL_EDIT_EMAIL_TEMPLATE', 'AOW_Actions')."</a></span>";
$html .= "</td>";
$html .= "</td>";
$html .= "</tr>";
$html .= "</tr>";
$html .= "<tr>";
$html .= "<tr>";
$html .= '<td id="name_label" scope="row" valign="top"><label>' . translate(
$html .= '<td id="name_label" scope="row" valign="top"><label>' . translate(
"LBL_EMAIL",
"LBL_EMAIL",
"AOW_Actions"
"AOW_Actions"
) . ':<span class="required">*</span></label></td>';
) . ':<span class="required">*</span></label></td>';
$html .= '<td valign="top" scope="row">';
$html .= '<td valign="top" scope="row">';


$html .='<button type="button" onclick="add_emailLine('.$line.')"><img src="'.SugarThemeRegistry::current()->getImageURL('id-ff-add.png').'"></button>';
$html .='<button type="button" onclick="add_emailLine('.$line.')"><img src="'.SugarThemeRegistry::current()->getImageURL('id-ff-add.png').'"></button>';
$html .= '<table id="emailLine'.$line.'_table" width="100%" class="email-line"></table>';
$html .= '<table id="emailLine'.$line.'_table" width="100%" class="email-line"></table>';
$html .= '</td>';
$html .= '</td>';
$html .= "</tr>";
$html .= "</tr>";
$html .= "</table>";
$html .= "</table>";


$html .= "<script id ='aow_script".$line."'>";
$html .= "<script id ='aow_script".$line."'>";


//backward compatible
//backward compatible
if (isset($params['email_target_type']) && !is_array($params['email_target_type'])) {
if (isset($params['email_target_type']) && !is_array($params['email_target_type'])) {
$email = '';
$email = '';
switch ($params['email_target_type']) {
switch ($params['email_target_type']) {
case 'Email Address':
case 'Email Address':
$email = $params['email'];
$email = $params['email'];
break;
break;
case 'Specify User':
case 'Specify User':
$email = $params['email_user_id'];
$email = $params['email_user_id'];
break;
break;
case 'Related Field':
case 'Related Field':
$email = $params['email_target'];
$email = $params['email_target'];
break;
break;
}
}
$html .= "load_emailline('".$line."','to','".$params['email_target_type']."','".$email."');";
$html .= "load_emailline('".$line."','to','".$params['email_target_type']."','".$email."');";
}
}
//end backward compatible
//end backward compatible


if (isset($params['email_target_type'])) {
if (isset($params['email_target_type'])) {
foreach ($params['email_target_type'] as $key => $field) {
foreach ($params['email_target_type'] as $key => $field) {
if (is_array($params['email'][$key])) {
if (is_array($params['email'][$key])) {
$params['email'][$key] = json_encode($params['email'][$key]);
$params['email'][$key] = json_encode($params['email'][$key]);
}
}
$html .= "load_emailline('".$line."','".$params['email_to_type'][$key]."','".$params['email_target_type'][$key]."','".$params['email'][$key]."');";
$html .= "load_emailline('".$line."','".$params['email_to_type'][$key]."','".$params['email_target_type'][$key]."','".$params['email'][$key]."');";
}
}
}
}
$html .= "</script>";
$html .= "</script>";


return $html;
return $html;
}
}


private function getEmailsFromParams(SugarBean $bean, $params)
private function getEmailsFromParams(SugarBean $bean, $params)
{
{
$emails = array();
$emails = array();
//backward compatible
//backward compatible
if (isset($params['email_target_type']) && !is_array($params['email_target_type'])) {
if (isset($params['email_target_type']) && !is_array($params['email_target_type'])) {
$email = '';
$email = '';
switch ($params['email_target_type']) {
switch ($params['email_target_type']) {
case 'Email Address':
case 'Email Address':
$params['email'] = array($params['email']);
$params['email'] = array($params['email']);
break;
break;
case 'Specify User':
case 'Specify User':
$params['email'] = array($params['email_user_id']);
$params['email'] = array($params['email_user_id']);
break;
break;
case 'Related Field':
case 'Related Field':
$params['email'] = array($params['email_target']);
$params['email'] = array($params['email_target']);
break;
break;
}
}
$params['email_target_type'] = array($params['email_target_type']);
$params['email_target_type'] = array($params['email_target_type']);
$params['email_to_type'] = array('to');
$params['email_to_type'] = array('to');
}
}
//end backward compatible
//end backward compatible
if (isset($params['email_target_type'])) {
if (isset($params['email_target_type'])) {
foreach ($params['email_target_type'] as $key => $field) {
foreach ($params['email_target_type'] as $key => $field) {
switch ($field) {
switch ($field) {
case 'Email Address':
case 'Email Address':
if (trim($params['email'][$key]) != '') {
if (trim($params['email'][$key]) != '') {
$emails[$params['email_to_type'][$key]][] = $params['email'][$key];
$emails[$params['email_to_type'][$key]][] = $params['email'][$key];
}
}
break;
break;
case 'Specify User':
case 'Specify User':
$user = BeanFactory::newBean('Users');
$user = BeanFactory::newBean('Users');
$user->retrieve($params['email'][$key]);
$user->retrieve($params['email'][$key]);
$user_email = $user->emailAddress->getPrimaryAddress($user);
$user_email = $user->emailAddress->getPrimaryAddress($user);
if (trim($user_email) != '') {
if (trim($user_email) != '') {
$emails[$params['email_to_type'][$key]][] = $user_email;
$emails[$params['email_to_type'][$key]][] = $user_email;
$emails['template_override'][$user_email] = array('Users' => $user->id);
$emails['template_override'][$user_email] = array('Users' => $user->id);
}
}


break;
break;
case 'Users':
case 'Users':
$users = array();
$users = array();
switch ($params['email'][$key][0]) {
switch ($params['email'][$key][0]) {
case 'security_group':
case 'security_group':
if (file_exists('modules/SecurityGroups/SecurityGroup.php')) {
if (file_exists('modules/SecurityGroups/SecurityGroup.php')) {
require_once('modules/SecurityGroups/SecurityGroup.php');
require_once('modules/SecurityGroups/SecurityGroup.php');
$security_group = BeanFactory::newBean('SecurityGroups');
$security_group = BeanFactory::newBean('SecurityGroups');
$security_group->retrieve($params['email'][$key][1]);
$security_group->retrieve($params['email'][$key][1]);
$users = $security_group->get_linked_beans('users', 'User');
$users = $security_group->get_linked_beans('users', 'User');
$r_users = array();
$r_users = array();
if ($params['email'][$key][2] != '') {
if ($params['email'][$key][2] != '') {
require_once('modules/ACLRoles/ACLRole.php');
require_once('modules/ACLRoles/ACLRole.php');
$role = BeanFactory::newBean('ACLRoles');
$role = BeanFactory::newBean('ACLRoles');
$role->retrieve($params['email'][$key][2]);
$role->retrieve($params['email'][$key][2]);
$role_users = $role->get_linked_beans('users', 'User');
$role_users = $role->get_linked_beans('users', 'User');
foreach ($role_users as $role_user) {
foreach ($role_users as $role_user) {
$r_users[$role_user->id] = $role_user->name;
$r_users[$role_user->id] = $role_user->name;
}
}
}
}
foreach ($users as $user_id => $user) {
foreach ($users as $user_id => $user) {
if ($params['email'][$key][2] != '' && !isset($r_users[$user->id])) {
if ($params['email'][$key][2] != '' && !isset($r_users[$user->id])) {
unset($users[$user_id]);
unset($users[$user_id]);
}
}
}
}
break;
break;
}
}
//No Security Group module found - fall through.
//No Security Group module found - fall through.
// no break
// no break
case 'role':
case 'role':
require_once('modules/ACLRoles/ACLRole.php');
require_once('modules/ACLRoles/ACLRole.php');
$role = BeanFactory::newBean('ACLRoles');
$role = BeanFactory::newBean('ACLRoles');
$role->retrieve($params['email'][$key][2]);
$role->retrieve($params['email'][$key][2]);
$users = $role->get_linked_beans('users', 'User');
$users = $role->get_linked_beans('users', 'User');
break;
break;
case 'all':
case 'all':
default:
default:
$db = DBManagerFactory::getInstance();
$db = DBManagerFactory::getInstance();
$sql = "SELECT id from users WHERE status='Active' AND portal_only=0 ";
$sql = "SELECT id from users WHERE status='Active' AND portal_only=0 ";
$result = $db->query($sql);
$result = $db->query($sql);
while ($row = $db->fetchByAssoc($result)) {
while ($row = $db->fetchByAssoc($result)) {
$user = BeanFactory::newBean('Users');
$user = BeanFactory::newBean('Users');
$user->retrieve($row['id']);
$user->retrieve($row['id']);
$users[$user->id] = $user;
$users[$user->id] = $user;
}
}
break;
break;
}
}
foreach ($users as $user) {
foreach ($users as $user) {
$user_email = $user->emailAddress->getPrimaryAddress($user);
$user_email = $user->emailAddress->getPrimaryAddress($user);
if (trim($user_email) != '') {
if (trim($user_email) != '') {
$emails[$params['email_to_type'][$key]][] = $user_email;
$emails[$params['email_to_type'][$key]][] = $user_email;
$emails['template_override'][$user_email] = array('Users' => $user->id);
$emails['template_override'][$user_email] = array('Users' => $user->id);
}
}
}
}
break;
break;
case 'Related Field':
case 'Related Field':
$emailTarget = $params['email'][$key];
$emailTarget = $params['email'][$key];
$relatedFields = array_merge($bean->get_related_fields(), $bean->get_linked_fields());
$relatedFields = array_merge($bean->get_related_fields(), $bean->get_linked_fields());
$field = $relatedFields[$emailTarget];
$field = $relatedFields[$emailTarget];
if ($field['type'] == 'relate') {
if ($field['type'] == 'relate') {
$linkedBeans = array();
$linkedBeans = array();
$idName = $field['id_name'];
$idName = $field['id_name'];
$id = $bean->$idName;
$id = $bean->$idName;
$linkedBeans[] = BeanFactory::getBean($field['module'], $id);
$linkedBeans[] = BeanFactory::getBean($field['module'], $id);
} else {
} else {
if ($field['type'] == 'link') {
if ($field['type'] == 'link') {
$relField = $field['name'];
$relField = $field['name'];
if (isset($field['module']) && $field['module'] != '') {
if (isset($field['module']) && $field['module'] != '') {
$rel_module = $field['module'];
$rel_module = $field['module'];
} else {
} else {
if ($bean->load_relationship($relField)) {
if ($bean->load_relationship($relField)) {
$rel_module = $bean->$relField->getRelatedModuleName();
$rel_module = $bean->$relField->getRelatedModuleName();
}
}
}
}
$linkedBeans = $bean->get_linked_beans($relField, $rel_module);
$linkedBeans = $bean->get_linked_beans($relField, $rel_module);
} else {
} else {
$linkedBeans = $bean->get_linked_beans($field['link'], $field['module']);
$linkedBeans = $bean->get_linked_beans($field['link'], $field['module']);
}
}
}
}
if ($linkedBeans) {
if ($linkedBeans) {
foreach ($linkedBeans as $linkedBean) {
foreach ($linkedBeans as $linkedBean) {
if (!empty($linkedBean)) {
if (!empty($linkedBean)) {
$rel_email = $linkedBean->emailAddress->getPrimaryAddress($linkedBean);
$rel_email = $linkedBean->emailAddress->getPrimaryAddress($linkedBean);
if (trim($rel_email) != '') {
if (trim($rel_email) != '') {
$emails[$params['email_to_type'][$key]][] = $rel_email;
$emails[$params['email_to_type'][$key]][] = $rel_email;
$emails['template_override'][$rel_email] = array($linkedBean->module_dir => $linkedBean->id);
$emails['template_override'][$rel_email] = array($linkedBean->module_dir => $linkedBean->id);
}
}
}
}
}
}
}
}
break;
break;
case 'Record Email':
case 'Record Email':
$recordEmail = $bean->emailAddress->getPrimaryAddress($bean);
$recordEmail = $bean->emailAddress->getPrimaryAddress($bean);
if ($recordEmail == '' && isset($bean->email1)) {
if ($recordEmail == '' && isset($bean->email1)) {
$recordEmail = $bean->email1;
$recordEmail = $bean->email1;
}
}
if (trim($recordEmail) != '') {
if (trim($recordEmail) != '') {
$emails[$params['email_to_type'][$key]][] = $recordEmail;
$emails[$params['email_to_type'][$key]][] = $recordEmail;
}
}
break;
break;
}
}
}
}
}
}
return $emails;
return $emails;
}
}


/**
/**
* Return true on success otherwise false.
* Return true on success otherwise false.
* Use actionSendEmail::getLastEmailsSuccess() and actionSendEmail::getLastEmailsFailed()
* Use actionSendEmail::getLastEmailsSuccess() and actionSendEmail::getLastEmailsFailed()
* methods to get last email sending status
* methods to get last email sending status
*
*
* @param SugarBean $bean
* @param SugarBean $bean
* @param array $params
* @param array $params
* @param bool $in_save
* @param bool $in_save
* @return boolean
* @return boolean
*/
*/
public function run_action(SugarBean $bean, $params = array(), $in_save = false)
public function run_action(SugarBean $bean, $params = array(), $in_save = false)
{
{
include_once __DIR__ . '/../../EmailTemplates/EmailTemplate.php';
include_once __DIR__ . '/../../EmailTemplates/EmailTemplate.php';


$this->clearLastEmailsStatus();
$this->clearLastEmailsStatus();


$emailTemp = BeanFactory::newBean('EmailTemplates');
$emailTemp = BeanFactory::newBean('EmailTemplates');
$emailTemp->retrieve($params['email_template']);
$emailTemp->retrieve($params['email_template']);


if ($emailTemp->id == '') {
if ($emailTemp->id == '') {
return false;
return false;
}
}


$emails = $this->getEmailsFromParams($bean, $params);
$emails = $this->getEmailsFromParams($bean, $params);


if (!isset($emails['to']) || empty($emails['to'])) {
if (!isset($emails['to']) || empty($emails['to'])) {
return false;
return false;
}
}


$attachments = $this->getAttachments($emailTemp);
$attachments = $this->getAttachments($emailTemp);


$ret = true;
$ret = true;
if (isset($params['individual_email']) && $params['individual_email']) {
if (isset($params['individual_email']) && $params['individual_email']) {
foreach ($emails['to'] as $email_to) {
foreach ($emails['to'] as $email_to) {
$emailTemp = BeanFactory::newBean('EmailTemplates');
$emailTemp = BeanFactory::newBean('EmailTemplates');
$emailTemp->retrieve($params['email_template']);
$emailTemp->retrieve($params['email_template']);
$template_override = isset($emails['template_override'][$email_to]) ? $emails['template_override'][$email_to] : array();
$template_override = isset($emails['template_override'][$email_to]) ? $emails['template_override'][$email_to] : array();
$this->parse_template($bean, $emailTemp, $template_override);
$this->parse_template($bean, $emailTemp, $template_override);
if (!$this->sendEmail(array($email_to), $emailTemp->subject, $emailTemp->body_html, $emailTemp->body, $bean, $emails['cc'], $emails['bcc'], $attachments)) {
if (!$this->sendEmail(array($email_to), $emailTemp->subject, $emailTemp->body_html, $emailTemp->body, $bean, $emails['cc'], $emails['bcc'], $attachments)) {
$ret = false;
$ret = false;
$this->lastEmailsFailed++;
$this->lastEmailsFailed++;
} else {
} else {
$this->lastEmailsSuccess++;
$this->lastEmailsSuccess++;
}
}
}
}
} else {
} else {
$this->parse_template($bean, $emailTemp);
$this->parse_template($bean, $emailTemp);
if ($emailTemp->text_only == '1') {
if ($emailTemp->text_only == '1') {
$email_body_html = $emailTemp->body;
$email_body = $emailTemp->body;
$email_body_alt = null;
} else {
} else {
$email_body_html = $emailTemp->body_html;
$email_body = $emailTemp->body_html;
$email_body_alt = $emailTemp->body;
}
}


if (!$this->sendEmail($emails['to'], $emailTemp->subject, $email_body_html, $emailTemp->body, $bean, $emails['cc'], $emails['bcc'], $attachments)) {
if (!$this->sendEmail($emails['to'], $emailTemp->subject, $email_body, $email_body_alt, $bean, $emails['cc'], $emails['bcc'], $attachments)) {
$ret = false;
$ret = false;
$this->lastEmailsFailed++;
$this->lastEmailsFailed++;
} else {
} else {
$this->lastEmailsSuccess++;
$this->lastEmailsSuccess++;
}
}
}
}
return $ret;
return $ret;
}
}


/**
/**
* clear last email sending status
* clear last email sending status
*/
*/
protected function clearLastEmailsStatus()
protected function clearLastEmailsStatus()
{
{
$this->lastEmailsFailed = 0;
$this->lastEmailsFailed = 0;
$this->lastEmailsSuccess = 0;
$this->lastEmailsSuccess = 0;
}
}


/**
/**
* failed emails count at last run_action
* failed emails count at last run_action
* @return int
* @return int
*/
*/
public function getLastEmailsFailed()
public function getLastEmailsFailed()
{
{
return $this->lastEmailsFailed;
return $this->lastEmailsFailed;
}
}


/**
/**
* successfully sent emails count at last run_action
* successfully sent emails count at last run_action
* @return type
* @return type
*/
*/
public function getLastEmailsSuccess()
public function getLastEmailsSuccess()
{
{
return $this->lastEmailsSuccess;
return $this->lastEmailsSuccess;
}
}


public function parse_template(SugarBean $bean, &$template, $object_override = array())
public function parse_template(SugarBean $bean, &$template, $object_override = array())
{
{
global $sugar_config;
global $sugar_config;


require_once __DIR__ . '/templateParser.php';
require_once __DIR__ . '/templateParser.php';


$object_arr[$bean->module_dir] = $bean->id;
$object_arr[$bean->module_dir] = $bean->id;


foreach ($bean->field_defs as $bean_arr) {
foreach ($bean->field_defs as $bean_arr) {
if ($bean_arr['type'] == 'relate') {
if ($bean_arr['type'] == 'relate') {
if (isset($bean_arr['module']) && $bean_arr['module'] != '' && isset($bean_arr['id_name']) && $bean_arr['id_name'] != '' && $bean_arr['module'] != 'EmailAddress') {
if (isset($bean_arr['module']) && $bean_arr['module'] != '' && isset($bean_arr['id_name']) && $bean_arr['id_name'] != '' && $bean_arr['module'] != 'EmailAddress') {
$idName = $bean_arr['id_name'];
$idName = $bean_arr['id_name'];
if (isset($bean->field_defs[$idName]) && $bean->field_defs[$idName]['source'] != 'non-db') {
if (isset($bean->field_defs[$idName]) && $bean->field_defs[$idName]['source'] != 'non-db') {
if (!isset($object_arr[$bean_arr['module']])) {
if (!isset($object_arr[$bean_arr['module']])) {
$object_arr[$bean_arr['module']] = $bean->$idName;
$object_arr[$bean_arr['module']] = $bean->$idName;
}
}
}
}
}
}
} else {
} else {
if ($bean_arr['type'] == 'link') {
if ($bean_arr['type'] == 'link') {
if (!isset($bean_arr['module']) || $bean_arr['module'] == '') {
if (!isset($bean_arr['module']) || $bean_arr['module'] == '') {
$bean_arr['module'] = getRelatedModule($bean->module_dir, $bean_arr['name']);
$bean_arr['module'] = getRelatedModule($bean->module_dir, $bean_arr['name']);
}
}
if (isset($bean_arr['module']) && $bean_arr['module'] != ''&& !isset($object_arr[$bean_arr['module']])&& $bean_arr['module'] != 'EmailAddress') {
if (isset($bean_arr['module']) && $bean_arr['module'] != ''&& !isset($object_arr[$bean_arr['module']])&& $bean_arr['module'] != 'EmailAddress') {
$linkedBeans = $bean->get_linked_beans($bean_arr['name'], $bean_arr['module'], array(), 0, 1);
$linkedBeans = $bean->get_linked_beans($bean_arr['name'], $bean_arr['module'], array(), 0, 1);
if ($linkedBeans) {
if ($linkedBeans) {
$linkedBean = $linkedBeans[0];
$linkedBean = $linkedBeans[0];
if (!isset($object_arr[$linkedBean->module_dir])) {
if (!isset($object_arr[$linkedBean->module_dir])) {
$object_arr[$linkedBean->module_dir] = $linkedBean->id;
$object_arr[$linkedBean->module_dir] = $linkedBean->id;
}
}
}
}
}
}
}
}
}
}
}
}


$object_arr['Users'] = is_a($bean, 'User') ? $bean->id : $bean->assigned_user_id;
$object_arr['Users'] = is_a($bean, 'User') ? $bean->id : $bean->assigned_user_id;


$object_arr = array_merge($object_arr, $object_override);
$object_arr = array_merge($object_arr, $object_override);


$parsedSiteUrl = parse_url($sugar_config['site_url']);
$parsedSiteUrl = parse_url($sugar_config['site_url']);
$host = $parsedSiteUrl['host'];
$host = $parsedSiteUrl['host'];
if (!isset($parsedSiteUrl['port'])) {
if (!isset($parsedSiteUrl['port'])) {
$parsedSiteUrl['port'] = 80;
$parsedSiteUrl['port'] = 80;
}
}


$port = ($parsedSiteUrl['port'] != 80) ? ":".$parsedSiteUrl['port'] : '';
$port = ($parsedSiteUrl['port'] != 80) ? ":".$parsedSiteUrl['port'] : '';
$path = !empty($parsedSiteUrl['path']) ? $parsedSiteUrl['path'] : "";
$path = !empty($parsedSiteUrl['path']) ? $parsedSiteUrl['path'] : "";
$cleanUrl = "{$parsedSiteUrl['scheme']}://{$host}{$port}{$path}";
$cleanUrl = "{$parsedSiteUrl['scheme']}://{$host}{$port}{$path}";


$url = $cleanUrl."/index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}";
$url = $cleanUrl."/index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}";


$template->subject = str_replace("\$contact_user", "\$user", $template->subject);
$template->subject = str_replace("\$contact_user", "\$user", $template->subject);
$template->body_html = str_replace("\$contact_user", "\$user", $template->body_html);
$template->body_html = str_replace("\$contact_user", "\$user", $template->body_html);
$template->body = str_replace("\$contact_user", "\$user", $template->body);
$template->body = str_replace("\$contact_user", "\$user", $template->body);
$template->subject = aowTemplateParser::parse_template($template->subject, $object_arr);
$template->subject = aowTemplateParser::parse_template($template->subject, $object_arr);
$template->body_html = aowTemplateParser::parse_template($template->body_html, $object_arr);
$template->body_html = aowTemplateParser::parse_template($template->body_html, $object_arr);
$template->body_html = str_replace("\$url", $url, $template->body_html);
$template->body_html = str_replace("\$url", $url, $template->body_html);
$template->body_html = str_replace('$sugarurl', $sugar_config['site_url'], $template->body_html);
$template->body_html = str_replace('$sugarurl', $sugar_config['site_url'], $template->body_html);
$template->body = aowTemplateParser::parse_template($template->body, $object_arr);
$template->body = aowTemplateParser::parse_template($template->body, $object_arr);
$template->body = str_replace("\$url", $url, $template->body);
$template->body = str_replace("\$url", $url, $template->body);
$template->body = str_replace('$sugarurl', $sugar_config['site_url'], $template->body);
$template->body = str_replace('$sugarurl', $sugar_config['site_url'], $template->body);
}
}


public function getAttachments(EmailTemplate $template)
public function getAttachments(EmailTemplate $template)
{
{
$attachments = array();
$attachments = array();
if ($template->id != '') {
if ($template->id != '') {
$note_bean = BeanFactory::newBean('Notes');
$note_bean = BeanFactory::newBean('Notes');
$notes = $note_bean->get_full_list('', "parent_type = 'Emails' AND parent_id = '".$template->id."'");
$notes = $note_bean->get_full_list('', "parent_type = 'Emails' AND parent_id = '".$template->id."'");


if ($notes != null) {
if ($notes != null) {
foreach ($notes as $note) {
foreach ($notes as $note) {
$attachments[] = $note;
$attachments[] = $note;
}
}
}
}
}
}
return $attachments;
return $attachments;
}
}


public function sendEmail($emailTo, $emailSubject, $emailBody, $altemailBody, SugarBean $relatedBean = null, $emailCc = array(), $emailBcc = array(), $attachments = array())
public function sendEmail($emailTo, $emailSubject, $emailBody, $altemailBody, SugarBean $relatedBean = null, $emailCc = array(), $emailBcc = array(), $attachments = array())
{
{
require_once('modules/Emails/Email.php');
require_once('modules/Emails/Email.php');
require_once('include/SugarPHPMailer.php');
require_once('include/SugarPHPMailer.php');


$emailObj = BeanFactory::newBean('Emails');
$emailObj = BeanFactory::newBean('Emails');
$defaults = $emailObj->getSystemDefaultEmail();
$defaults = $emailObj->getSystemDefaultEmail();
$mail = new SugarPHPMailer();
$mail = new SugarPHPMailer();
$mail->setMailerForSystem();
$mail->setMailerForSystem();
$mail->From = $defaults['email'];
$mail->From = $defaults['email'];
isValidEmailAddress($mail->From);
isValidEmailAddress($mail->From);
$mail->FromName = $defaults['name'];
$mail->FromName = $defaults['name'];
$mail->ClearAllRecipients();
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->ClearReplyTos();
$mail->Subject=from_html($emailSubject);
$mail->Subject=from_html($emailSubject);
$mail->Body=$emailBody;
$mail->Body=$emailBody;
$mail->AltBody = $altemailBody;
if($altemailBody){
$mail->AltBody = $altemailBody;
}
$mail->handleAttachments($attachments);
$mail->handleAttachments($attachments);
$mail->prepForOutbound();
$mail->prepForOutbound();


if (empty($emailTo)) {
if (empty($emailTo)) {
return false;
return false;
}
}
foreach ($emailTo as $to) {
foreach ($emailTo as $to) {
$mail->AddAddress($to);
$mail->AddAddress($to);
}
}
if (!empty($emailCc)) {
if (!empty($emailCc)) {
foreach ($emailCc as $email) {
foreach ($emailCc as $email) {
$mail->AddCC($email);
$mail->AddCC($email);
}
}
}
}
if (!empty($emailBcc)) {
if (!empty($emailBcc)) {
foreach ($emailBcc as $email) {
foreach ($emailBcc as $email) {
$mail->AddBCC($email);
$mail->AddBCC($email);
}
}
}
}


//now create email
//now create email
if ($mail->Send()) {
if ($mail->Send()) {
$emailObj->to_addrs= implode(',', $emailTo);
$emailObj->to_addrs= implode(',', $emailTo);
$emailObj->cc_addrs= implode(',', $emailCc);
$emailObj->cc_addrs= implode(',', $emailCc);
$emailObj->bcc_addrs= implode(',', $emailBcc);
$emailObj->bcc_addrs= implode(',', $emailBcc);
$emailObj->type= 'out';
$emailObj->type= 'out';
$emailObj->deleted = '0';
$emailObj->deleted = '0';
$emailObj->name = $mail->Subject;
$emailObj->name = $mail->Subject;
$emailObj->description = $mail->AltBody;
$emailObj->description = $mail->AltBody;
$emailObj->description_html = $mail->Body;
$emailObj->description_html = $mail->Body;
$emailObj->from_addr = $mail->From;
$emailObj->from_addr = $mail->From;
isValidEmailAddress($emailObj->from_addr);
isValidEmailAddress($emailObj->from_addr);
if ($relatedBean instanceof SugarBean && !empty($relatedBean->id)) {
if ($relatedBean instanceof SugarBean && !empty($relatedBean->id)) {
$emailObj->parent_type = $relatedBean->module_dir;
$emailObj->parent_type = $relatedBean->module_dir;
$emailObj->parent_id = $relatedBean->id;
$emailObj->parent_id = $relatedBean->id;
}
}
$emailObj->date_sent_received = TimeDate::getInstance()->nowDb();
$emailObj->date_sent_received = TimeDate::getInstance()->nowDb();
$emailObj->modified_user_id = '1';
$emailObj->modified_user_id = '1';
$emailObj->created_by = '1';
$emailObj->created_by = '1';
$emailObj->status = 'sent';
$emailObj->status = 'sent';
$emailObj->save();
$emailObj->save();


// Fix for issue 1561 - Email Attachments Sent By Workflow Do Not Show In Related Activity.
// Fix for issue 1561 - Email Attachments Sent By Workflow Do Not Show In Related Activity.
foreach ($attachments as $attachment) {
foreach ($attachments as $attachment) {
$note = BeanFactory::newBean('Notes');
$note = BeanFactory::newBean('Notes');
$note->id = create_guid();
$note->id = create_guid();
$note->date_entered = $attachment->date_entered;
$note->date_entered = $attachment->date_entered;
$note->date_modified = $attachment->date_modified;
$note->date_modified = $attachment->date_modified;
$note->modified_user_id = $attachment->modified_user_id;
$note->modified_user_id = $attachment->modified_user_id;
$note->assigned_user_id = $attachment->assigned_user_id;
$note->assigned_user_id = $attachment->assigned_user_id;
$note->new_with_id = true;
$note->new_with_id = true;
$note->parent_id = $emailObj->id;
$note->parent_id = $emailObj->id;
$note->parent_type = $attachment->parent_type;
$note->parent_type = $attachment->parent_type;
$note->name = $attachment->name;
$note->name = $attachment->name;
;
;
$note->filename = $attachment->filename;
$note->filename = $attachment->filename;
$note->file_mime_type = $attachment->file_mime_type;
$note->file_mime_type = $attachment->file_mime_type;
$fileLocation = "upload://{$attachment->id}";
$fileLocation = "upload://{$attachment->id}";
$dest = "upload://{$note->id}";
$dest = "upload://{$note->id}";
if (!copy($fileLocation, $dest)) {
if (!copy($fileLocation, $dest)) {
$GLOBALS['log']->debug("EMAIL 2.0: could not copy attachment file to $fileLocation => $dest");
$GLOBA
}