/** * @group 49109 */ public function testRelations() { global $beanFiles; $bean = new Email(); $bean->load_relationship('users', $bean); $relation = $bean->users->getRelationshipObject(); $this->assertNotEmpty($relation->lhsLink, 'lhsLink is undefined'); $this->assertNotEmpty($relation->rhsLink, 'rhsLink is undefined'); }
public function testEmailImport() { $this->_login(); $nv = array('from_addr' => '*****@*****.**', 'parent_type' => 'Accounts', 'parent_id' => $this->acc->id, 'description' => 'test', 'name' => 'Test Subject'); $result = $this->_soapClient->call('set_entry', array('session' => $this->_sessionId, "module_name" => 'Emails', 'name_value_list' => $nv)); $this->email_id = $result['id']; $email = new Email(); $email->retrieve($this->email_id); $email->load_relationship('accounts'); $acc = $email->accounts->get(); $this->assertEquals($this->acc->id, $acc[0]); }
/** * Link the Lead to the Email from which the lead was created * Also set the assigned user to current user and mark email as read. * TODO: This logic is brought over from LeadFormBase->handleSave() - need refactoring to use Link2? * * @param $emailId * @param $leadId */ protected function linkLeadToEmail($emailId, $leadId) { global $current_user; $email = new Email(); $email->retrieve($emailId); $email->parent_type = 'Leads'; $email->parent_id = $leadId; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('leads'); $email->leads->add($leadId); }
function handleSave($prefix, $redirect = true, $useRequired = false) { global $theme, $current_user; require_once 'include/formbase.php'; global $timedate; $focus = new Contact(); if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { return null; } if (!empty($_POST[$prefix . 'new_reports_to_id'])) { $focus->retrieve($_POST[$prefix . 'new_reports_to_id']); $focus->reports_to_id = $_POST[$prefix . 'record']; } else { $focus = populateFromPost($prefix, $focus); if (!empty($focus->portal_password) && $focus->portal_password != $_POST[$prefix . 'old_portal_password']) { $focus->portal_password = md5($focus->portal_password); } if (!isset($_POST[$prefix . 'email_opt_out'])) { $focus->email_opt_out = 0; } if (!isset($_POST[$prefix . 'do_not_call'])) { $focus->do_not_call = 0; } } if (!$focus->ACLAccess('Save')) { ACLController::displayNoAccess(true); sugar_cleanup(true); } if ($_REQUEST['action'] != 'BusinessCard' && $_REQUEST['action'] != 'ConvertLead' && $_REQUEST['action'] != 'ConvertProspect') { if (!empty($_POST[$prefix . 'sync_contact'])) { $focus->contacts_users_id = $current_user->id; } else { if (!isset($focus->users)) { $focus->load_relationship('user_sync'); } $focus->contacts_users_id = null; $focus->user_sync->delete($focus->id, $current_user->id); } } if (isset($GLOBALS['check_notify'])) { $check_notify = $GLOBALS['check_notify']; } else { $check_notify = FALSE; } if (empty($_POST['dup_checked'])) { $duplicateContacts = $this->checkForDuplicates($prefix); if (isset($duplicateContacts)) { $location = 'module=Contacts&action=ShowDuplicates'; $get = ''; if (isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) { $get .= '&inbound_email_id=' . $_POST['inbound_email_id']; } // Bug 25311 - Add special handling for when the form specifies many-to-many relationships if (isset($_POST['relate_to']) && !empty($_POST['relate_to'])) { $get .= '&Contactsrelate_to=' . $_POST['relate_to']; } if (isset($_POST['relate_id']) && !empty($_POST['relate_id'])) { $get .= '&Contactsrelate_id=' . $_POST['relate_id']; } //add all of the post fields to redirect get string foreach ($focus->column_fields as $field) { if (!empty($focus->{$field}) && !is_object($focus->{$field})) { $get .= "&Contacts{$field}=" . urlencode($focus->{$field}); } } foreach ($focus->additional_column_fields as $field) { if (!empty($focus->{$field})) { $get .= "&Contacts{$field}=" . urlencode($focus->{$field}); } } if ($focus->hasCustomFields()) { foreach ($focus->field_defs as $name => $field) { if (!empty($field['source']) && $field['source'] == 'custom_fields') { $get .= "&Contacts{$name}=" . urlencode($focus->{$name}); } } } $emailAddress = new SugarEmailAddress(); $get .= $emailAddress->getFormBaseURL($focus); //create list of suspected duplicate contact id's in redirect get string $i = 0; foreach ($duplicateContacts as $contact) { $get .= "&duplicate[{$i}]=" . $contact['id']; $i++; } //add return_module, return_action, and return_id to redirect get string $get .= "&return_module="; if (!empty($_POST['return_module'])) { $get .= $_POST['return_module']; } else { $get .= "Contacts"; } $get .= "&return_action="; if (!empty($_POST['return_action'])) { $get .= $_POST['return_action']; } //else $get .= "DetailView"; if (!empty($_POST['return_id'])) { $get .= "&return_id=" . $_POST['return_id']; } if (!empty($_POST['popup'])) { $get .= '&popup=' . $_POST['popup']; } if (!empty($_POST['create'])) { $get .= '&create=' . $_POST['create']; } // for InboundEmail flow if (!empty($_POST['start'])) { $get .= '&start=' . $_POST['start']; } $_SESSION['SHOW_DUPLICATES'] = $get; //now redirect the post to modules/Contacts/ShowDuplicates.php if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') { ob_clean(); $json = getJSONobj(); echo $json->encode(array('status' => 'dupe', 'get' => $location)); } else { if (!empty($_REQUEST['ajax_load'])) { echo "<script>SUGAR.ajaxUI.loadContent('index.php?{$location}');</script>"; } else { if (!empty($_POST['to_pdf'])) { $location .= '&to_pdf=' . $_POST['to_pdf']; } header("Location: index.php?{$location}"); } } return null; } } global $current_user; if (is_admin($current_user)) { if (!isset($_POST[$prefix . 'portal_active'])) { $focus->portal_active = '0'; } //if no password is set set account to inactive for portal if (empty($_POST[$prefix . 'portal_name'])) { $focus->portal_active = '0'; } } /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { // fake this case like it's already saved. $focus->save($check_notify); $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Contacts'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('contacts'); $email->contacts->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start'] . '&assigned_user_id=' . $current_user->id); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// $focus->save($check_notify); $return_id = $focus->id; $GLOBALS['log']->debug("Saved record with id of " . $return_id); if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') { $json = getJSONobj(); echo $json->encode(array('status' => 'success', 'get' => '')); $trackerManager = TrackerManager::getInstance(); $timeStamp = TimeDate::getInstance()->nowDb(); if ($monitor = $trackerManager->getMonitor('tracker')) { $monitor->setValue('action', 'detailview'); $monitor->setValue('user_id', $GLOBALS['current_user']->id); $monitor->setValue('module_name', 'Contacts'); $monitor->setValue('date_modified', $timeStamp); $monitor->setValue('visible', 1); if (!empty($this->bean->id)) { $monitor->setValue('item_id', $return_id); $monitor->setValue('item_summary', $focus->get_summary_text()); } $trackerManager->saveMonitor($monitor, true, true); } return null; } if (isset($_POST['popup']) && $_POST['popup'] == 'true') { $get = '&module='; if (!empty($_POST['return_module'])) { $get .= $_POST['return_module']; } else { $get .= 'Contacts'; } $get .= '&action='; if (!empty($_POST['return_action'])) { $get .= $_POST['return_action']; } else { $get .= 'Popup'; } if (!empty($_POST['return_id'])) { $get .= '&return_id=' . $_POST['return_id']; } if (!empty($_POST['popup'])) { $get .= '&popup=' . $_POST['popup']; } if (!empty($_POST['create'])) { $get .= '&create=' . $_POST['create']; } if (!empty($_POST['to_pdf'])) { $get .= '&to_pdf=' . $_POST['to_pdf']; } $get .= '&first_name=' . urlencode($focus->first_name); $get .= '&last_name=' . urlencode($focus->last_name); $get .= '&query=true'; header("Location: index.php?{$get}"); return; } if ($redirect) { $this->handleRedirect($return_id); } else { return $focus; } }
function handleSave($prefix, $redirect = true, $useRequired = false, $do_save = true, $exist_lead = null) { require_once 'modules/Campaigns/utils.php'; require_once 'include/formbase.php'; if (empty($exist_lead)) { $focus = new Lead(); } else { $focus = $exist_lead; } if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { return null; } $focus = populateFromPost($prefix, $focus); if (!$focus->ACLAccess('Save')) { ACLController::displayNoAccess(true); sugar_cleanup(true); } if (!isset($_POST[$prefix . 'email_opt_out'])) { $focus->email_opt_out = 0; } if (!isset($_POST[$prefix . 'do_not_call'])) { $focus->do_not_call = 0; } if ($do_save) { if (!empty($GLOBALS['check_notify'])) { $focus->save($GLOBALS['check_notify']); } else { $focus->save(FALSE); } } $return_id = $focus->id; if (isset($_POST[$prefix . 'prospect_id']) && !empty($_POST[$prefix . 'prospect_id'])) { $prospect = new Prospect(); $prospect->retrieve($_POST[$prefix . 'prospect_id']); $prospect->lead_id = $focus->id; $prospect->save(); //if prospect id exists, make sure we are coming from prospect detail if (strtolower($_POST['return_module']) == 'prospects' && strtolower($_POST['return_action']) == 'detailview') { //create campaing_log entry if (isset($focus->campaign_id) && $focus->campaign_id != null) { campaign_log_lead_entry($focus->campaign_id, $prospect, $focus, 'lead'); } } } /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { if (!isset($current_user)) { global $current_user; } // fake this case like it's already saved. $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Leads'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('leads'); $email->leads->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start']); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// $GLOBALS['log']->debug("Saved record with id of " . $return_id); if ($redirect) { handleRedirect($return_id, 'Leads'); } else { return $focus; } }
$email->duration_hours = array_rand($possible_duration_hours_arr); $email->duration_minutes = array_rand($possible_duration_minutes_arr); $email->assigned_user_id = $assigned_user_id; $email->assigned_user_id = $contacts_account->assigned_user_id; $email->assigned_user_name = $contacts_account->assigned_user_name; $email->description = 'Discuss project plan and hash out the details of implementation'; $email->status = 'sent'; $email->parent_id = $account_id; $email->parent_type = 'Accounts'; $email->to_addrs = $contact->emailAddress->getPrimaryAddress($contact); $email->from_addr = $assignedUser->emailAddress->getPrimaryAddress($assignedUser); $email->from_addr_name = $email->from_addr; $email->to_addrs_names = $email->to_addrs; $email->type = 'out'; $email->save(); $email->load_relationship('contacts'); $email->contacts->add($contact->id); $email->load_relationship('accounts'); $email->contacts->add($account_id); } for ($i = 0; $i < $number_leads; $i++) { $lead = new Lead(); $lead->account_name = $company_name_array[mt_rand(0, $company_name_count - 1)] . ' ' . mt_rand(1, 1000000); $lead->first_name = $first_name_array[mt_rand(0, $first_name_max)]; $lead->last_name = $last_name_array[mt_rand(0, $last_name_max)]; $lead->primary_address_street = $street_address_array[mt_rand(0, $street_address_max)]; $lead->primary_address_city = $city_array[mt_rand(0, $city_array_max)]; $lead->lead_source = $app_list_strings['lead_source_dom'][array_rand($app_list_strings['lead_source_dom'])]; $lead->title = $titles[mt_rand(0, $title_max)]; $lead->phone_work = create_phone_number(); $lead->phone_home = create_phone_number();
} } break; case "relateEmails": if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id']) && isset($_REQUEST['parent_type']) && !empty($_REQUEST['parent_type'])) { $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $_REQUEST['uid']); $mod = strtolower($_REQUEST['parent_type']); $modId = $_REQUEST['parent_id']; foreach ($uids as $id) { $email = new Email(); $email->retrieve($id); $email->parent_id = $modId; $email->parent_type = $_REQUEST['parent_type']; $email->status = 'read'; $email->save(); $email->load_relationship($mod); $email->{$mod}->add($modId); } } break; case "getAssignmentDialogContent": $out = $email->distributionForm(""); $out = trim($json->encode($out, false)); echo $out; break; case "doAssignmentAssign": $out = $email->et->doAssignment($_REQUEST['distribute_method'], $_REQUEST['ieId'], $_REQUEST['folder'], $_REQUEST['uids'], $_REQUEST['users']); echo $out; break; case "doAssignmentDelete": $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: doAssignmentDelete");
$focus->to_addrs = $_REQUEST['to_addrs']; $focus->cc_addrs = $_REQUEST['cc_addrs']; $focus->bcc_addrs = $_REQUEST['bcc_addrs']; $focus->from_addr = $_REQUEST['from_addr']; // delete the existing relationship of all the email addresses with this email $query = "update emails_email_addr_rel set deleted = 1 WHERE email_id = '{$focus->id}'"; $focus->db->query($query); // delete al the relationship of this email with all the beans //$query = "update emails_beans set deleted = 1, bean_id = '', bean_module = '' WHERE email_id = '{$focus->id}'"; //$focus->db->query($query); if (isset($_REQUEST['object_type']) && !empty($_REQUEST['object_type']) && isset($_REQUEST['object_id']) && !empty($_REQUEST['object_id'])) { //run linking code only if the object_id has not been linked as part of the contacts above and it is an OOB relationship $GLOBALS['log']->debug("CESELY" . $_REQUEST['object_type']); if (!in_array($_REQUEST['object_id'], $exContactIds)) { $rel = strtolower($_REQUEST['object_type']); if ($focus->load_relationship($rel)) { $focus->{$rel}->add($_REQUEST['object_id']); $GLOBALS['log']->debug("CESELY LOADED" . $_REQUEST['object_type']); } } } //// END RELATIONSHIP LINKING /////////////////////////////////////////////////////////////////////////////// // If came from email archiving edit view, this would have been set from form input. if (!isset($focus->date_start)) { $timedate = TimeDate::getInstance(); list($focus->date_start, $focus->time_start) = $timedate->split_date_time($timedate->now()); } $focus->date_sent = ""; require_once 'include/formbase.php'; $focus = populateFromPost('', $focus);
$focus->{$field} = $value; } } foreach ($focus->additional_column_fields as $field) { if (isset($_POST[$field])) { $value = $_POST[$field]; $focus->{$field} = $value; } } $focus->save($check_notify); $return_id = $focus->id; /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { require_once 'modules/Emails/Email.php'; $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Bugs'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('bugs'); $email->bugs->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start'] . '&assigned_user_id=' . $current_user->id); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// handleRedirect($return_id, 'Bugs');
function sendEmail($mail, $testmode = false) { $this->test = $testmode; global $beanList, $beanFiles, $sugar_config; global $mod_strings; $mod_strings = return_module_language($sugar_config['default_language'], 'EmailMan'); //get tracking entities locations. if (!isset($this->tracking_url)) { if (!class_exists('Administration')) { require_once 'modules/Administration/Administration.php'; } $admin = new Administration(); $admin->retrieveSettings('massemailer'); //retrieve all admin settings. if (isset($admin->settings['massemailer_tracking_entities_location_type']) and $admin->settings['massemailer_tracking_entities_location_type'] == '2' and isset($admin->settings['massemailer_tracking_entities_location'])) { $this->tracking_url = $admin->settings['massemailer_tracking_entities_location']; } else { $this->tracking_url = $sugar_config['site_url']; } } if (!isset($beanList[$this->related_type])) { return false; } $class = $beanList[$this->related_type]; if (!class_exists($class)) { require_once $beanFiles[$class]; } if (!class_exists('Email')) { require_once 'modules/Emails/Email.php'; } $module = new $class(); $module->retrieve($this->related_id); if ((!isset($module->email_opt_out) || $module->email_opt_out != 'on') && (!isset($module->invalid_email) || $module->invalid_email != 1)) { $lower_email_address = strtolower($module->email1); //test against indivdual address. if (isset($this->restricted_addresses) and isset($this->restricted_addresses[$lower_email_address])) { $this->set_as_sent($lower_email_address, true, true, null, null, null, true); return true; } //test against restricted domains $at_pos = strrpos($lower_email_address, '@'); if ($at_pos !== false) { foreach ($this->restricted_domains as $domain => $value) { $pos = strrpos($lower_email_address, $domain); if ($pos !== false && $pos > $at_pos) { //found $this->set_as_sent($lower_email_address, true, true, null, null, null, true); return true; } } } //test for duplicate email address. if (!empty($module->email1) and !empty($this->campaign_id)) { $dup_query = "select id from campaign_log where more_information='" . $module->email1 . "' and campaign_id='" . $this->campaign_id . "'"; $dup = $this->db->query($dup_query); $dup_row = $this->db->fetchByAssoc($dup); if (!empty($dup_row)) { //email address was processed //silent delete this entry. $this->set_as_sent($module->email1, true, true, null, null, null, true); return true; } } $start = microtime(); $this->target_tracker_key = create_guid(); //fetch email marketing. if (empty($this->current_emailmarketing) or !isset($this->current_emailmarketing)) { if (!class_exists('EmailMarketing')) { require_once 'modules/EmailMarketing/EmailMarketing.php'; } $this->current_emailmarketing = new EmailMarketing(); } if (empty($this->current_emailmarketing->id) or $this->current_emailmarketing->id !== $this->marketing_id) { $this->current_emailmarketing->retrieve($this->marketing_id); } //fetch email template associate with the marketing message. if (empty($this->current_emailtemplate) or $this->current_emailtemplate->id !== $this->current_emailmarketing->template_id) { if (!class_exists('EmailTemplate')) { require_once 'modules/EmailTemplates/EmailTemplate.php'; } $this->current_emailtemplate = new EmailTemplate(); $this->current_emailtemplate->retrieve($this->current_emailmarketing->template_id); //escape email template contents. $this->current_emailtemplate->subject = from_html($this->current_emailtemplate->subject); $this->current_emailtemplate->body_html = from_html($this->current_emailtemplate->body_html); $this->current_emailtemplate->body = from_html($this->current_emailtemplate->body); $q = "SELECT * FROM notes WHERE parent_id = '" . $this->current_emailtemplate->id . "' AND deleted = 0"; $r = $this->db->query($q); // cn: bug 4684 - initialize the notes array, else old data is still around for the next round $this->notes_array = array(); while ($a = $this->db->fetchByAssoc($r)) { $noteTemplate = new Note(); $noteTemplate->retrieve($a['id']); $this->notes_array[] = $noteTemplate; } } //fetch mailbox details.. if (empty($this->current_mailbox)) { if (!class_exists('InboundEmail')) { require_once 'modules/InboundEmail/InboundEmail.php'; } $this->current_mailbox = new InboundEmail(); } if (empty($this->current_mailbox->id) or $this->current_mailbox->id !== $this->current_emailmarketing->inbound_email_id) { $this->current_mailbox->retrieve($this->current_emailmarketing->inbound_email_id); //extract the email address. $this->mailbox_from_addr = $this->current_mailbox->get_stored_options('from_addr', '*****@*****.**', null); } //fetch campaign details.. if (empty($this->current_campaign)) { if (!class_exists('Campaign')) { require_once 'modules/Campaigns/Campaign.php'; } $this->current_campaign = new Campaign(); } if (empty($this->current_campaign->id) or $this->current_campaign->id !== $this->current_emailmarketing->campaign_id) { $this->current_campaign->retrieve($this->current_emailmarketing->campaign_id); //load defined tracked_urls $this->current_campaign->load_relationship('tracked_urls'); $query_array = $this->current_campaign->tracked_urls->getQuery(true); $query_array['select'] = "SELECT tracker_name, tracker_key, id, is_optout "; $result = $this->current_campaign->db->query(implode(' ', $query_array)); $this->has_optout_links = false; while (($row = $this->current_campaign->db->fetchByAssoc($result)) != null) { $this->tracker_urls['{' . $row['tracker_name'] . '}'] = $row; //has the user defined opt-out links for the campaign. if ($row['is_optout'] == 1) { $this->has_optout_links = true; } } } //BEGIN:this code will trigger for only campaigns pending before upgrade to 4.2.0. //will be removed for the next release. $btracker = true; $tracker_url = $this->tracking_url . '/campaign_tracker.php?track=' . $this->current_campaign->tracker_key . '&identifier=' . $this->target_tracker_key; $tracker_text = $this->current_campaign->tracker_text; if (empty($tracker_text)) { $btracker = false; } //END $mail->ClearAllRecipients(); $mail->ClearReplyTos(); $mail->Sender = $this->mailbox_from_addr; $mail->From = $this->mailbox_from_addr; $mail->FromName = $this->current_emailmarketing->from_name; $mail->AddReplyTo($this->mailbox_from_addr, $this->current_emailmarketing->from_name); //parse and replace bean variables. $template_data = $this->current_emailtemplate->parse_email_template(array('subject' => $this->current_emailtemplate->subject, 'body_html' => $this->current_emailtemplate->body_html, 'body' => $this->current_emailtemplate->body), 'Contacts', $module); //parse and replace urls. //this is new style of adding tracked urls to a campaign. $tracker_url_template = $this->tracking_url . '/campaign_trackerv2.php?track=%s' . '&identifier=' . $this->target_tracker_key; $removeme_url_template = $this->tracking_url . '/removeme.php?identifier=' . $this->target_tracker_key; $template_data = $this->current_emailtemplate->parse_tracker_urls($template_data, $tracker_url_template, $this->tracker_urls, $removeme_url_template); $mail->AddAddress($module->email1, $module->first_name . ' ' . $module->last_name); $mail->Subject = $template_data['subject']; $mail->Body = wordwrap($template_data['body_html'], 900); //BEGIN:this code will trigger for only campaigns pending before upgrade to 4.2.0. //will be removed for the next release. if ($btracker) { $mail->Body .= "<br><br><a href='" . $tracker_url . "'>" . $tracker_text . "</a><br><br>"; } else { if (!empty($tracker_url)) { $mail->Body = str_replace('TRACKER_URL_START', "<a href='" . $tracker_url . "'>", $mail->Body); $mail->Body = str_replace('TRACKER_URL_END', "</a>", $mail->Body); $mail->AltBody = str_replace('TRACKER_URL_START', "<a href='" . $tracker_url . "'>", $mail->AltBody); $mail->AltBody = str_replace('TRACKER_URL_END', "</a>", $mail->AltBody); } } //END //do not add the default remove me link if the campaign has a trackerurl of the opotout link if ($this->has_optout_links == false) { $mail->Body .= "<br><font size='2'>{$mod_strings['TXT_REMOVE_ME']}<a href='" . $this->tracking_url . "/removeme.php?identifier={$this->target_tracker_key}'>{$mod_strings['TXT_REMOVE_ME_CLICK']}</a></font>"; } //add image reference to track opening of html emails. $mail->Body .= "<br><IMG HEIGHT=1 WIDTH=1 src={$this->tracking_url}/image.php?identifier={$this->target_tracker_key}>"; $mail->AltBody = $template_data['body']; if ($btracker) { $mail->AltBody .= "\n" . $tracker_url; } if ($this->has_optout_links == false) { $mail->AltBody .= "\n\n\n{$mod_strings['TXT_REMOVE_ME_ALT']} " . $this->tracking_url . "/removeme.php?identifier={$this->target_tracker_key}"; } // cn: bug 4684, handle attachments in email templates. $mail->handleAttachments($this->notes_array); $success = $mail->send(); if ($success) { $email = new Email(); $email->to_addrs = $module->first_name . ' ' . $module->last_name . '<' . $module->email1 . '>'; $email->to_addrs_ids = $module->id . ';'; $email->to_addrs_names = $module->first_name . ' ' . $module->last_name . ';'; $email->to_addrs_emails = $module->email1 . ';'; $email->type = 'archived'; $email->deleted = '0'; $email->name = $this->current_campaign->name . ': ' . $mail->Subject; $email->description_html = $mail->AltBody; $email->description = $mail->AltBody; $email->from_addr = $mail->From; $email->assigned_user_id = $this->user_id; $email->parent_type = $this->related_type; $email->parent_id = $this->related_id; $email->date_start = date('Y-m-d'); $email->time_start = date('H:i:s'); $email->status = 'sent'; $retId = $email->save(); foreach ($this->notes_array as $note) { if (!class_exists('Note')) { require_once 'modules/Notes/Note.php'; } // create "audit" email without duping off the file to save on disk space $noteAudit = new Note(); $noteAudit->parent_id = $retId; $noteAudit->parent_type = $email->module_dir; $noteAudit->description = "[" . $note->filename . "] " . $mod_strings['LBL_ATTACHMENT_AUDIT']; $noteAudit->save(); } if (!empty($this->related_id) && !empty($this->related_type)) { //save relationships. switch ($this->related_type) { case 'Users': $rel_name = "users"; break; case 'Prospects': $rel_name = "prospects"; break; case 'Contacts': $rel_name = "contacts"; break; case 'Leads': $rel_name = "leads"; break; } if (!empty($rel_name)) { $email->load_relationship($rel_name); $email->{$rel_name}->add($this->related_id); } } } if ($success) { $this->set_as_sent($module->email1, $success, $success, $email->id, 'Emails', 'targeted'); } else { //log send error. $this->set_as_sent($module->email1, $success, $success); } } else { $mail->ErrorInfo .= "\nRecipient Email Opt Out"; $success = false; if (isset($module->email_opt_out) && $module->email_opt_out == 'on') { $this->set_as_sent($module->email1, $success, true, null, null, 'removed'); } else { if (isset($module->invalid_email) && $module->invalid_email == 1) { $this->set_as_sent($module->email1, $success, true, null, null, 'invalid email'); } else { $this->set_as_sent($module->email1, $success, true); } } } return $success; }
function handleSave($prefix, $redirect = true, $useRequired = false) { global $theme, $current_user; $theme_path = "themes/" . $theme . "/"; require_once 'modules/Contacts/Contact.php'; require_once $theme_path . 'layout_utils.php'; require_once 'include/utils.php'; require_once 'include/formbase.php'; require_once 'XTemplate/xtpl.php'; global $timedate; $focus = new Contact(); if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { return null; } if (!empty($_POST[$prefix . 'new_reports_to_id'])) { $focus->retrieve($_POST[$prefix . 'new_reports_to_id']); $focus->reports_to_id = $_POST[$prefix . 'record']; } else { $focus = populateFromPost($prefix, $focus); if (isset($focus->portal_password) && $focus->portal_password != $_POST[$prefix . 'old_portal_password']) { $focus->portal_password = md5($focus->portal_password); } if (!isset($_POST[$prefix . 'email_opt_out'])) { $focus->email_opt_out = 'off'; } if (!isset($_POST[$prefix . 'do_not_call'])) { $focus->do_not_call = 'off'; } } if (!$focus->ACLAccess('Save')) { ACLController::displayNoAccess(true); sugar_cleanup(true); } if ($_REQUEST['action'] != 'BusinessCard' && $_REQUEST['action'] != 'ConvertLead' && $_REQUEST['action'] != 'ConvertProspect') { if (isset($_POST[$prefix . 'sync_contact'])) { $focus->contacts_users_id = $current_user->id; } else { if (!isset($focus->users)) { $focus->load_relationship('user_sync'); } $focus->contacts_users_id = null; $focus->user_sync->delete($focus->id, $current_user->id); } } if (isset($GLOBALS['check_notify'])) { $check_notify = $GLOBALS['check_notify']; } else { $check_notify = FALSE; } if (empty($_POST['record']) && empty($_POST['dup_checked'])) { $duplicateContacts = $this->checkForDuplicates($prefix); if (isset($duplicateContacts)) { $get = 'module=Contacts&action=ShowDuplicates'; if (isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) { $get .= '&inbound_email_id=' . $_POST['inbound_email_id']; } //add all of the post fields to redirect get string foreach ($focus->column_fields as $field) { if (!empty($focus->{$field})) { $get .= "&Contacts{$field}=" . urlencode($focus->{$field}); } } foreach ($focus->additional_column_fields as $field) { if (!empty($focus->{$field})) { $get .= "&Contacts{$field}=" . urlencode($focus->{$field}); } } //create list of suspected duplicate contact id's in redirect get string $i = 0; foreach ($duplicateContacts as $contact) { $get .= "&duplicate[{$i}]=" . $contact['id']; $i++; } //add return_module, return_action, and return_id to redirect get string $get .= "&return_module="; if (!empty($_POST['return_module'])) { $get .= $_POST['return_module']; } else { $get .= "Contacts"; } $get .= "&return_action="; if (!empty($_POST['return_action'])) { $get .= $_POST['return_action']; } else { $get .= "DetailView"; } if (!empty($_POST['return_id'])) { $get .= "&return_id=" . $_POST['return_id']; } if (!empty($_POST['popup'])) { $get .= '&popup=' . $_POST['popup']; } if (!empty($_POST['create'])) { $get .= '&create=' . $_POST['create']; } // for InboundEmail flow if (!empty($_POST['start'])) { $get .= '&start=' . $_POST['start']; } //now redirect the post to modules/Contacts/ShowDuplicates.php if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') { $json = getJSONobj(); echo $json->encode(array('status' => 'dupe', 'get' => $get)); } else { if (!empty($_POST['to_pdf'])) { $get .= '&to_pdf=' . $_POST['to_pdf']; } header("Location: index.php?{$get}"); } return null; } } global $current_user; if (is_admin($current_user)) { if (!isset($_POST[$prefix . 'portal_active'])) { $focus->portal_active = '0'; } //if no password is set set account to inactive for portal if (empty($_POST[$prefix . 'portal_name'])) { $focus->portal_active = '0'; } } /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { // fake this case like it's already saved. $focus->save($check_notify); require_once 'modules/Emails/Email.php'; $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Contacts'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('contacts'); $email->contacts->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start'] . '&assigned_user_id=' . $current_user->id); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// $focus->save($check_notify); $return_id = $focus->id; $GLOBALS['log']->debug("Saved record with id of " . $return_id); if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') { $json = getJSONobj(); echo $json->encode(array('status' => 'success', 'get' => '')); return null; } if (isset($_POST['popup']) && $_POST['popup'] == 'true') { $get = '&module='; if (!empty($_POST['return_module'])) { $get .= $_POST['return_module']; } else { $get .= 'Contacts'; } $get .= '&action='; if (!empty($_POST['return_action'])) { $get .= $_POST['return_action']; } else { $get .= 'Popup'; } if (!empty($_POST['return_id'])) { $get .= '&return_id=' . $_POST['return_id']; } if (!empty($_POST['popup'])) { $get .= '&popup=' . $_POST['popup']; } if (!empty($_POST['create'])) { $get .= '&create=' . $_POST['create']; } if (!empty($_POST['to_pdf'])) { $get .= '&to_pdf=' . $_POST['to_pdf']; } $get .= '&first_name=' . $focus->first_name; $get .= '&last_name=' . $focus->last_name; $get .= '&query=true'; header("Location: index.php?{$get}"); return; } if ($redirect) { $this->handleRedirect($return_id); } else { return $focus; } }
$focus->save($GLOBALS['check_notify']); $return_id = $focus->id; /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { // fake this machine like it's already saved. require_once 'modules/Emails/Email.php'; $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Machines'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('machines'); $email->machines->add($focus->id); if (!empty($email->reply_to_addr)) { $contactAddr = $email->reply_to_addr; } else { $contactAddr = $email->from_addr; } if ($contactIds = $email->getRelatedId($contactAddr, 'contacts')) { $focus->load_relationship('contacts'); $focus->contacts->add($contactIds); } header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start'] . '&assigned_user_id=' . $current_user->id); break; } //// END INBOUND EMAIL HANDLING ///////////////////////////////////////////////////////////////////////////////
if ($focus->send()) { $focus->status = 'sent'; $today = gmdate('Y-m-d H:i:s'); $focus->date_start = $timedate->to_display_date($today); $focus->time_start = $timedate->to_display_time($today, true); } else { $focus->status = 'send_error'; } } $focus->to_addrs = $_REQUEST['to_addrs']; $focus->save(FALSE); //// END EMAIL SAVE/SEND SETUP /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// //// RELATIONSHIP LINKING $focus->load_relationship('users'); $focus->users->add($current_user->id); if (!empty($_REQUEST['to_addrs_ids'])) { $focus->load_relationship('contacts'); $exContactIds = explode(';', $_REQUEST['to_addrs_ids']); foreach ($exContactIds as $contactId) { $contactId = trim($contactId); $focus->contacts->add($contactId); } } if (isset($_REQUEST['object_type']) && !empty($_REQUEST['object_type']) && isset($_REQUEST['object_id']) && !empty($_REQUEST['object_id'])) { //run linking code only if the object_id has not been linked as part of the contacts above if (!in_array($_REQUEST['object_id'], $exContactIds)) { $rel = strtolower($_REQUEST['object_type']); $focus->load_relationship($rel); $focus->{$rel}->add($_REQUEST['object_id']);
function handleSave($prefix, $redirect = true, $useRequired = false) { require_once 'modules/Leads/Lead.php'; require_once 'include/formbase.php'; $focus = new Lead(); if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { return null; } $focus = populateFromPost($prefix, $focus); if (!$focus->ACLAccess('Save')) { ACLController::displayNoAccess(true); sugar_cleanup(true); } if (!isset($_POST[$prefix . 'email_opt_out'])) { $focus->email_opt_out = 'off'; } if (!isset($_POST[$prefix . 'do_not_call'])) { $focus->do_not_call = 'off'; } if (!empty($GLOBALS['check_notify'])) { $focus->save($GLOBALS['check_notify']); } else { $focus->save(FALSE); } $return_id = $focus->id; if (isset($_POST[$prefix . 'prospect_id']) && !empty($_POST[$prefix . 'prospect_id'])) { if (!class_exists('Prospect')) { require_once 'modules/Prospects/Prospect.php'; } $prospect = new Prospect(); $prospect->retrieve($_POST[$prefix . 'prospect_id']); $prospect->lead_id = $focus->id; $prospect->save(); $linked_beans = $prospect->get_linked_beans('campaigns', 'CampaignLog'); if (empty($linked_beans)) { $linked_beans = array(); } foreach ($linked_beans as $thebean) { $thebean->id = null; $thebean->target_id = $focus->id; $thebean->target_type = 'Leads'; $thebean->archived = 1; $thebean->save(); } } /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { if (!isset($current_user)) { global $current_user; } // fake this case like it's already saved. require_once 'modules/Emails/Email.php'; $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Leads'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('leads'); $email->leads->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start']); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// $GLOBALS['log']->debug("Saved record with id of " . $return_id); if ($redirect) { handleRedirect($return_id, 'Leads'); } else { return $focus; } }
function handleSave($prefix, $redirect = true, $useRequired = false, $do_save = true, $exist_lead = null) { require_once 'modules/Campaigns/utils.php'; require_once 'include/formbase.php'; if (empty($exist_lead)) { $focus = new Lead(); } else { $focus = $exist_lead; } if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { return null; } $focus = populateFromPost($prefix, $focus); if (!$focus->ACLAccess('Save')) { ACLController::displayNoAccess(true); sugar_cleanup(true); } //Check for duplicate Leads if (empty($_POST['record']) && empty($_POST['dup_checked'])) { $duplicateLeads = $this->checkForDuplicates($prefix); if (isset($duplicateLeads)) { //Set the redirect location to call the ShowDuplicates action. This will map to view.showduplicates.php $location = 'module=Leads&action=ShowDuplicates'; $get = ''; if (isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) { $get .= '&inbound_email_id=' . $_POST['inbound_email_id']; } if (isset($_POST['relate_to']) && !empty($_POST['relate_to'])) { $get .= '&Leadsrelate_to=' . $_POST['relate_to']; } if (isset($_POST['relate_id']) && !empty($_POST['relate_id'])) { $get .= '&Leadsrelate_id=' . $_POST['relate_id']; } //add all of the post fields to redirect get string foreach ($focus->column_fields as $field) { if (!empty($focus->{$field}) && !is_object($focus->{$field})) { $get .= "&Leads{$field}=" . urlencode($focus->{$field}); } } foreach ($focus->additional_column_fields as $field) { if (!empty($focus->{$field})) { $get .= "&Leads{$field}=" . urlencode($focus->{$field}); } } if ($focus->hasCustomFields()) { foreach ($focus->field_defs as $name => $field) { if (!empty($field['source']) && $field['source'] == 'custom_fields') { $get .= "&Leads{$name}=" . urlencode($focus->{$name}); } } } $emailAddress = new SugarEmailAddress(); $get .= $emailAddress->getFormBaseURL($focus); //create list of suspected duplicate lead ids in redirect get string $i = 0; foreach ($duplicateLeads as $lead) { $get .= "&duplicate[{$i}]=" . $lead['id']; $i++; } //add return_module, return_action, and return_id to redirect get string $get .= "&return_module="; if (!empty($_POST['return_module'])) { $get .= $_POST['return_module']; } else { $get .= "Leads"; } $get .= "&return_action="; if (!empty($_POST['return_action'])) { $get .= $_POST['return_action']; } if (!empty($_POST['return_id'])) { $get .= "&return_id=" . $_POST['return_id']; } if (!empty($_POST['popup'])) { $get .= '&popup=' . $_POST['popup']; } if (!empty($_POST['create'])) { $get .= '&create=' . $_POST['create']; } // for InboundEmail flow if (!empty($_POST['start'])) { $get .= '&start=' . $_POST['start']; } $_SESSION['SHOW_DUPLICATES'] = $get; if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') { ob_clean(); $json = getJSONobj(); echo $json->encode(array('status' => 'dupe', 'get' => $location)); } else { if (!empty($_REQUEST['ajax_load'])) { echo "<script>SUGAR.ajaxUI.loadContent('index.php?{$location}');</script>"; } else { if (!empty($_POST['to_pdf'])) { $location .= '&to_pdf=' . $_POST['to_pdf']; } header("Location: index.php?{$location}"); } } return null; } } if (!isset($_POST[$prefix . 'email_opt_out'])) { $focus->email_opt_out = 0; } if (!isset($_POST[$prefix . 'do_not_call'])) { $focus->do_not_call = 0; } if ($do_save) { if (!empty($GLOBALS['check_notify'])) { $focus->save($GLOBALS['check_notify']); } else { $focus->save(FALSE); } } $return_id = $focus->id; if (isset($_POST[$prefix . 'prospect_id']) && !empty($_POST[$prefix . 'prospect_id'])) { $prospect = new Prospect(); $prospect->retrieve($_POST[$prefix . 'prospect_id']); $prospect->lead_id = $focus->id; // Set to keep email in target $prospect->in_workflow = true; $prospect->save(); //if prospect id exists, make sure we are coming from prospect detail if (strtolower($_POST['return_module']) == 'prospects' && strtolower($_POST['return_action']) == 'detailview') { //create campaing_log entry if (isset($focus->campaign_id) && $focus->campaign_id != null) { campaign_log_lead_entry($focus->campaign_id, $prospect, $focus, 'lead'); } } } /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { if (!isset($current_user)) { global $current_user; } // fake this case like it's already saved. $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Leads'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('leads'); $email->leads->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start']); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// $GLOBALS['log']->debug("Saved record with id of " . $return_id); if ($redirect) { handleRedirect($return_id, 'Leads'); } else { return $focus; } }
/** * The function creates a copy of email send to each target. */ function create_indiv_email($module, $mail) { global $locale, $timedate; $email = new Email(); $email->to_addrs = $module->name . '<' . $module->email1 . '>'; $email->to_addrs_ids = $module->id . ';'; $email->to_addrs_names = $module->name . ';'; $email->to_addrs_emails = $module->email1 . ';'; $email->type = 'archived'; $email->deleted = '0'; $email->name = $this->current_campaign->name . ': ' . $mail->Subject; if ($mail->ContentType == "text/plain") { $email->description = $mail->Body; $email->description_html = null; } else { $email->description_html = $mail->Body; $email->description = $mail->AltBody; } $email->from_addr = $mail->From; $email->assigned_user_id = $this->user_id; $email->parent_type = $this->related_type; $email->parent_id = $this->related_id; $email->date_start = $timedate->nowDbDate(); $email->time_start = $timedate->asDbTime($timedate->getNow()); $email->status = 'sent'; $retId = $email->save(); foreach ($this->notes_array as $note) { if (!class_exists('Note')) { } // create "audit" email without duping off the file to save on disk space $noteAudit = new Note(); $noteAudit->parent_id = $retId; $noteAudit->parent_type = $email->module_dir; $noteAudit->description = "[" . $note->filename . "] " . $mod_strings['LBL_ATTACHMENT_AUDIT']; $noteAudit->save(); } if (!empty($this->related_id) && !empty($this->related_type)) { //save relationships. switch ($this->related_type) { case 'Users': $rel_name = "users"; break; case 'Prospects': $rel_name = "prospects"; break; case 'Contacts': $rel_name = "contacts"; break; case 'Leads': $rel_name = "leads"; break; case 'Accounts': $rel_name = "accounts"; break; } if (!empty($rel_name)) { $email->load_relationship($rel_name); $email->{$rel_name}->add($this->related_id); } } return $email->id; }
/** * Create objects from createdef definitions * Example definition: * <code> * $createdef['*****@*****.**']['Contacts'] = array( * 'fields' => array( * 'email1' => '{from_addr}', * 'last_name' => '{from_name}', * 'description' => 'created from {subject}', * 'lead_source' => 'Email', * ), * ); * </code> * Supported variables: * - from * - from_addr * - from_name * - subject * - date * - description * - description_html * - message_id * - email_id * @param Email $email */ protected function createObject($email) { if (!SugarAutoLoader::existing('custom/modules/SNIP/createdefs.php')) { return false; } $createdef = array(); include 'custom/modules/SNIP/createdefs.php'; $emaildata = array(); foreach (array("subject", "description", "description_html", "message_id", "from_addr", "from_name") as $prop) { $emaildata["{" . $prop . "}"] = $email->{$prop}; } $emaildata["{from}"] = to_html($email->from_addr_name); $emaildata["{date}"] = $email->date_sent; $emaildata["{email_id}"] = $email->id; foreach ($email->all_addrs as $cleanaddr) { if (!isset($createdef[$cleanaddr])) { continue; } foreach ($createdef[$cleanaddr] as $module => $data) { // $obj = BeanFactory::getBean($module); if (!$obj) { $GLOBALS['log']->error("Unable to create bean for module {$module}"); continue; } // instantiate the data foreach ($data["fields"] as $key => $value) { $obj->{$key} = str_replace(array_keys($emaildata), array_values($emaildata), $value); } // special case for Opportunity if ($obj instanceof Opportunity && empty($obj->date_closed)) { $obj->date_closed = TimeDate::getInstance()->getNow()->asDbDate(); } // save $obj->save(); // associate email to new object if (empty($obj->id)) { continue; } // save failed $mod = strtolower($module); $rel = array_key_exists($mod, $email->field_defs) ? $mod : $mod . "_activities_emails"; //Custom modules rel name if ($email->load_relationship($rel)) { $email->{$rel}->add($obj->id); } } } return true; }
$focus->date_start = ''; } /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { // fake this case like it's already saved. $focus->save(); $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Tasks'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('tasks'); $email->tasks->add($focus->id); header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start'] . '&assigned_user_id=' . $current_user->id); exit; } //// END INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// // CCL - Bugs 41103 and 43751. 41103 address the issue where the parent_id is set, but // the relate_id field overrides the relationship. 43751 fixes the problem where the relate_id and // parent_id are the same value (in which case it should just use relate_id) by adding the != check if (!empty($_REQUEST['relate_id']) && !empty($_REQUEST['parent_id']) && $_REQUEST['relate_id'] != $_REQUEST['parent_id']) { $_REQUEST['relate_id'] = false; } // avoid undefined index if (!isset($GLOBALS['check_notify'])) { $GLOBALS['check_notify'] = false;
$focus->save($GLOBALS['check_notify']); $return_id = $focus->id; /////////////////////////////////////////////////////////////////////////////// //// INBOUND EMAIL HANDLING /////////////////////////////////////////////////////////////////////////////// if (isset($_REQUEST['inbound_email_id']) && !empty($_REQUEST['inbound_email_id'])) { // fake this case like it's already saved. require_once 'modules/Emails/Email.php'; $email = new Email(); $email->retrieve($_REQUEST['inbound_email_id']); $email->parent_type = 'Cases'; $email->parent_id = $focus->id; $email->assigned_user_id = $current_user->id; $email->status = 'read'; $email->save(); $email->load_relationship('cases'); $email->cases->add($focus->id); if (!empty($email->reply_to_addr)) { $contactAddr = $email->reply_to_addr; } else { $contactAddr = $email->from_addr; } if ($contactIds = $email->getRelatedId($contactAddr, 'contacts')) { $focus->load_relationship('contacts'); $focus->contacts->add($contactIds); } header("Location: index.php?&module=Emails&action=EditView&type=out&inbound_email_id=" . $_REQUEST['inbound_email_id'] . "&parent_id=" . $email->parent_id . "&parent_type=" . $email->parent_type . '&start=' . $_REQUEST['start'] . '&assigned_user_id=' . $current_user->id); break; } //// END INBOUND EMAIL HANDLING ///////////////////////////////////////////////////////////////////////////////
/** * shiny new importOneEmail() method * @param int msgNo * @param bool forDisplay * @param clean_email boolean, default true, */ function importOneEmail($msgNo, $uid, $forDisplay = false, $clean_email = true) { $GLOBALS['log']->debug("InboundEmail processing 1 email {$msgNo}-----------------------------------------------------------------------------------------"); global $timedate; global $app_strings; global $app_list_strings; global $sugar_config; global $current_user; // Bug # 45477 // So, on older versions of PHP (PHP VERSION < 5.3), // calling imap_headerinfo and imap_fetchheader can cause a buffer overflow for exteremly large headers, // This leads to the remaining messages not being read because Sugar crashes everytime it tries to read the headers. // The workaround is to mark a message as read before making trying to read the header of the msg in question // This forces this message not be read again, and we can continue processing remaining msgs. // UNCOMMENT THIS IF YOU HAVE THIS PROBLEM! See notes on Bug # 45477 // $this->markEmails($uid, "read"); $header = imap_headerinfo($this->conn, $msgNo); $fullHeader = imap_fetchheader($this->conn, $msgNo); // raw headers // reset inline images cache $this->inlineImages = array(); // handle messages deleted on server if (empty($header)) { if (!isset($this->email) || empty($this->email)) { $this->email = new Email(); } $q = ""; if ($this->isPop3Protocol()) { $this->email->name = $app_strings['LBL_EMAIL_ERROR_MESSAGE_DELETED']; $q = "DELETE FROM email_cache WHERE message_id = '{$uid}' AND ie_id = '{$this->id}' AND mbox = '{$this->mailbox}'"; } else { $this->email->name = $app_strings['LBL_EMAIL_ERROR_IMAP_MESSAGE_DELETED']; $q = "DELETE FROM email_cache WHERE imap_uid = {$uid} AND ie_id = '{$this->id}' AND mbox = '{$this->mailbox}'"; } // else // delete local cache $r = $this->db->query($q); $this->email->date_sent = $timedate->nowDb(); return false; //return "Message deleted from server."; } /////////////////////////////////////////////////////////////////////// //// DUPLICATE CHECK $dupeCheckResult = $this->importDupeCheck($header->message_id, $header, $fullHeader); if ($forDisplay || $dupeCheckResult) { $GLOBALS['log']->debug('*********** NO duplicate found, continuing with processing.'); $structure = imap_fetchstructure($this->conn, $msgNo); // map of email /////////////////////////////////////////////////////////////////// //// CREATE SEED EMAIL OBJECT $email = new Email(); $email->isDuplicate = $dupeCheckResult ? false : true; $email->mailbox_id = $this->id; $message = array(); $email->id = create_guid(); $email->new_with_id = true; //forcing a GUID here to prevent double saves. //// END CREATE SEED EMAIL /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //// PREP SYSTEM USER if (empty($current_user)) { // I-E runs as admin, get admin prefs $current_user = new User(); $current_user->getSystemUser(); } $tPref = $current_user->getUserDateTimePreferences(); //// END USER PREP /////////////////////////////////////////////////////////////////// if (!empty($header->date)) { $unixHeaderDate = $timedate->fromString($header->date); } /////////////////////////////////////////////////////////////////// //// HANDLE EMAIL ATTACHEMENTS OR HTML TEXT //// Inline images require that I-E handle attachments before body text // parts defines attachments - be mindful of .html being interpreted as an attachment if ($structure->type == 1 && !empty($structure->parts)) { $GLOBALS['log']->debug('InboundEmail found multipart email - saving attachments if found.'); $this->saveAttachments($msgNo, $structure->parts, $email->id, 0, $forDisplay); } elseif ($structure->type == 0) { $uuemail = $this->isUuencode($email->description) ? true : false; /* * UUEncoded attachments - legacy, but still have to deal with it * format: * begin 777 filename.txt * UUENCODE * * end */ // set body to the filtered one if ($uuemail) { $email->description = $this->handleUUEncodedEmailBody($email->description, $email->id); $email->retrieve($email->id); $email->save(); } } else { if ($this->port != 110) { $GLOBALS['log']->debug('InboundEmail found a multi-part email (id:' . $msgNo . ') with no child parts to parse.'); } } //// END HANDLE EMAIL ATTACHEMENTS OR HTML TEXT /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //// ASSIGN APPROPRIATE ATTRIBUTES TO NEW EMAIL OBJECT // handle UTF-8/charset encoding in the ***headers*** global $db; $email->name = $this->handleMimeHeaderDecode($header->subject); $email->date_start = !empty($unixHeaderDate) ? $timedate->asUserDate($unixHeaderDate) : ""; $email->time_start = !empty($unixHeaderDate) ? $timedate->asUserTime($unixHeaderDate) : ""; $email->type = 'inbound'; $email->date_created = !empty($unixHeaderDate) ? $timedate->asUser($unixHeaderDate) : ""; $email->status = 'unread'; // this is used in Contacts' Emails SubPanel if (!empty($header->toaddress)) { $email->to_name = $this->handleMimeHeaderDecode($header->toaddress); $email->to_addrs_names = $email->to_name; } if (!empty($header->to)) { $email->to_addrs = $this->convertImapToSugarEmailAddress($header->to); } $email->from_name = $this->handleMimeHeaderDecode($header->fromaddress); $email->from_addr_name = $email->from_name; $email->from_addr = $this->convertImapToSugarEmailAddress($header->from); if (!empty($header->cc)) { $email->cc_addrs = $this->convertImapToSugarEmailAddress($header->cc); } if (!empty($header->ccaddress)) { $email->cc_addrs_names = $this->handleMimeHeaderDecode($header->ccaddress); } // if $email->reply_to_name = $this->handleMimeHeaderDecode($header->reply_toaddress); $email->reply_to_email = $this->convertImapToSugarEmailAddress($header->reply_to); if (!empty($email->reply_to_email)) { $email->reply_to_addr = $email->reply_to_name; } $email->intent = $this->mailbox_type; $email->message_id = $this->compoundMessageId; // filled by importDupeCheck(); $oldPrefix = $this->imagePrefix; if (!$forDisplay) { // Store CIDs in imported messages, convert on display $this->imagePrefix = "cid:"; } // handle multi-part email bodies $email->description_html = $this->getMessageText($msgNo, 'HTML', $structure, $fullHeader, $clean_email); // runs through handleTranserEncoding() already $email->description = $this->getMessageText($msgNo, 'PLAIN', $structure, $fullHeader, $clean_email); // runs through handleTranserEncoding() already $this->imagePrefix = $oldPrefix; // empty() check for body content if (empty($email->description)) { $GLOBALS['log']->debug('InboundEmail Message (id:' . $email->message_id . ') has no body'); } // assign_to group if (!empty($_REQUEST['user_id'])) { $email->assigned_user_id = $_REQUEST['user_id']; } else { // Samir Gandhi : Commented out this code as its not needed //$email->assigned_user_id = $this->group_id; } //Assign Parent Values if set if (!empty($_REQUEST['parent_id']) && !empty($_REQUEST['parent_type'])) { $email->parent_id = $_REQUEST['parent_id']; $email->parent_type = $_REQUEST['parent_type']; $mod = strtolower($email->parent_type); $rel = array_key_exists($mod, $email->field_defs) ? $mod : $mod . "_activities_emails"; //Custom modules rel name if (!$email->load_relationship($rel)) { return FALSE; } $email->{$rel}->add($email->parent_id); } // override $forDisplay w/user pref if ($forDisplay) { if ($this->isAutoImport()) { $forDisplay = false; // triggers save of imported email } } if (!$forDisplay) { $email->save(); $email->new_with_id = false; // to allow future saves by UPDATE, instead of INSERT //// ASSIGN APPROPRIATE ATTRIBUTES TO NEW EMAIL OBJECT /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //// LINK APPROPRIATE BEANS TO NEWLY SAVED EMAIL //$contactAddr = $this->handleLinking($email); //// END LINK APPROPRIATE BEANS TO NEWLY SAVED EMAIL /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //// MAILBOX TYPE HANDLING $this->handleMailboxType($email, $header); //// END MAILBOX TYPE HANDLING /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// //// SEND AUTORESPONSE if (!empty($email->reply_to_email)) { $contactAddr = $email->reply_to_email; } else { $contactAddr = $email->from_addr; } if (!$this->isMailBoxTypeCreateCase()) { $this->handleAutoresponse($email, $contactAddr); } //// END SEND AUTORESPONSE /////////////////////////////////////////////////////////////////// //// END IMPORT ONE EMAIL /////////////////////////////////////////////////////////////////// } } else { // only log if not POP3; pop3 iterates through ALL mail if ($this->protocol != 'pop3') { $GLOBALS['log']->info("InboundEmail found a duplicate email: " . $header->message_id); //echo "This email has already been imported"; } return false; } //// END DUPLICATE CHECK /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// //// DEAL WITH THE MAILBOX if (!$forDisplay) { imap_setflag_full($this->conn, $msgNo, '\\SEEN'); // if delete_seen, mark msg as deleted if ($this->delete_seen == 1 && !$forDisplay) { $GLOBALS['log']->info("INBOUNDEMAIL: delete_seen == 1 - deleting email"); imap_setflag_full($this->conn, $msgNo, '\\DELETED'); } } else { // for display - don't touch server files? //imap_setflag_full($this->conn, $msgNo, '\\UNSEEN'); } $GLOBALS['log']->debug('********************************* InboundEmail finished import of 1 email: ' . $email->name); //// END DEAL WITH THE MAILBOX /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// //// TO SUPPORT EMAIL 2.0 $this->email = $email; if (empty($this->email->et)) { $this->email->email2init(); } return true; }