function campaign_process_bounced_emails(&$email, &$email_header) { if (preg_match('/MAILER-DAEMON/i', $email_header->fromaddress)) { //do we have the identifier tag in the email? $matches = array(); if (preg_match('/removeme.php\\?identifier=[a-z0-9\\-]*/', $email->description, $matches)) { $identifiers = preg_split('/removeme.php\\?identifier=/', $matches[0], -1, PREG_SPLIT_NO_EMPTY); if (!empty($identifiers)) { //array should have only one element in it. $identifier = trim($identifiers[0]); if (!class_exists('CampaignLog')) { require_once 'modules/CampaignLog/CampaignLog.php'; } $targeted = new CampaignLog(); $where = "campaign_log.activity_type='targeted' and campaign_log.target_tracker_key='{$identifier}'"; $query = $targeted->create_list_query('', $where); $result = $targeted->db->query($query); $row = $targeted->db->fetchByAssoc($result); if (!empty($row)) { //found entry //do not create another campaign_log record is we already have an //invalid email or send error entry for this tracker key. $query_log = "select * from campaign_log where target_tracker_key='{$row['target_tracker_key']}'"; $query_log .= " and (activity_type='invalid email' or activity_type='send error')"; $result_log = $targeted->db->query($query_log); $row_log = $targeted->db->fetchByAssoc($result_log); if (empty($row_log)) { $bounce = new CampaignLog(); $bounce->campaign_id = $row['campaign_id']; $bounce->target_tracker_key = $row['target_tracker_key']; $bounce->target_id = $row['target_id']; $bounce->target_type = $row['target_type']; $bounce->list_id = $row['list_id']; $bounce->activity_date = $email->date_created; $bounce->related_type = 'Emails'; $bounce->related_id = $email->id; //do we have the phrase permanent error in the email body. if (preg_match('/permanent[ ]*error/', $email->description)) { //invalid email address $bounce->activity_type = 'invalid email'; } else { //other -bounced email. $bounce->activity_type = 'send error'; } $return_id = $bounce->save(); } } else { $GLOBALS['log']->info("Warning: skipping bounced email with this tracker_key(identifier) in the message body " . $identifier); } } else { //todo mark the email address as invalid. search for prospects/leads/contact associated //with this email address and set the invalid_email flag... also make email available. } } else { $GLOBALS['log']->info("Warning: skipping bounced email because it does not have the removeme link."); } } else { $GLOBALS['log']->info("Warning: skipping bounced email because the sender is not MAILER-DAEMON."); } }
public function testget_related_name() { $campaignLog = new CampaignLog(); //execute the method and verify that it retunrs expected results for all type parameters $this->assertEquals('1Emails', $campaignLog->get_related_name(1, 'Emails')); $this->assertEquals('1Contacts', $campaignLog->get_related_name(1, 'Contacts')); $this->assertEquals('1Leads', $campaignLog->get_related_name(1, 'Leads')); $this->assertEquals('1Prospects', $campaignLog->get_related_name(1, 'Prospects')); $this->assertEquals('1CampaignTrackers', $campaignLog->get_related_name(1, 'CampaignTrackers')); $this->assertEquals('1Accounts', $campaignLog->get_related_name(1, 'Accounts')); }
protected function create_campaign_log($campaign, $target, $marketing, $prospectlist, $activity_type, $target_tracker_key = '') { $this->markTestSkipped('Marking this skipped until we figure out why it is causing the SQL server connection to go away.'); $campaign_log = new CampaignLog(); $campaign_log->campaign_id = $campaign->id; $campaign_log->target_tracker_key = $target_tracker_key; $campaign_log->target_id = $target->id; $campaign_log->target_type = $target->module_dir; $campaign_log->marketing_id = $marketing->id; $campaign_log->more_information = $target->email1; $campaign_log->activity_type = $activity_type; $campaign_log->activity_date = $GLOBALS['timedate']->to_display_date_time(gmdate($GLOBALS['timedate']->get_db_date_time_format())); $campaign_log->list_id = $prospectlist->id; $campaign_log->related_type = 'Emails'; $campaign_log->save(); }
function set_as_sent($email_address, $delete = true, $email_id = null, $email_type = null, $activity_type = null) { global $timedate; $this->send_attempts++; $this->id = (int) $this->id; if ($delete || $this->send_attempts > 5) { //create new campaign log record. $campaign_log = new CampaignLog(); $campaign_log->campaign_id = $this->campaign_id; $campaign_log->target_tracker_key = $this->target_tracker_key; $campaign_log->target_id = $this->related_id; $campaign_log->target_type = $this->related_type; $campaign_log->marketing_id = $this->marketing_id; //if test suppress duplicate email address checking. if (!$this->test) { $campaign_log->more_information = $email_address; } $campaign_log->activity_type = $activity_type; $campaign_log->activity_date = $timedate->now(); $campaign_log->list_id = $this->list_id; $campaign_log->related_id = $email_id; $campaign_log->related_type = $email_type; $campaign_log->save(); $query = "DELETE FROM emailman WHERE id = {$this->id}"; $this->db->query($query); } else { //try to send the email again a day later. $query = 'UPDATE ' . $this->table_name . " SET in_queue='1', send_attempts='{$this->send_attempts}', in_queue_date=" . $this->db->now() . " WHERE id = {$this->id}"; $this->db->query($query); } }
/** * Get the existing campaign log entry by tracker key. * * @param string Target Key * @return array Campaign Log Row */ function getExistingCampaignLogEntry($identifier) { $row = FALSE; $targeted = new CampaignLog(); $where = "campaign_log.activity_type='targeted' and campaign_log.target_tracker_key='{$identifier}'"; $query = $targeted->create_new_list_query('', $where); $result = $targeted->db->query($query); $row = $targeted->db->fetchByAssoc($result); return $row; }
function create_campaign_log_entry($campaign_id, $focus, $rel_name, $rel_bean, $target_id = ''){ global $timedate; $target_ids = array(); //check if this is specified for one target/contact/prospect/lead (from contact/lead detail subpanel) if(!empty($target_id)){ $target_ids[] = $target_id; }else{ //this is specified for all, so load target/prospect relationships (mark as sent button) $focus->load_relationship($rel_name); $target_ids = $focus->$rel_name->get(); } if(count($target_ids)>0){ //retrieve the target beans and create campaign log entry foreach($target_ids as $id){ //perform duplicate check $dup_query = "select id from campaign_log where campaign_id = '$campaign_id' and target_id = '$id'"; $dup_result = $focus->db->query($dup_query); $row = $focus->db->fetchByAssoc($dup_result); //process if this is not a duplicate campaign log entry if(empty($row)){ //create campaign tracker id and retrieve related bio bean $tracker_id = create_guid(); $rel_bean->retrieve($id); //create new campaign log record. $campaign_log = new CampaignLog(); $campaign_log->campaign_id = $campaign_id; $campaign_log->target_tracker_key = $tracker_id; $campaign_log->target_id = $rel_bean->id; $campaign_log->target_type = $rel_bean->module_dir; $campaign_log->activity_type = 'targeted'; $campaign_log->activity_date=$timedate->now(); //save the campaign log entry $campaign_log->save(); } } } }
function campaign_process_bounced_emails(&$email, &$email_header) { global $sugar_config; $emailFromAddress = $email_header->fromaddress; $email_description = $email->description; $query1 = "SELECT id, file_mime_type FROM notes WHERE file_mime_type like 'message/r%' and parent_id = '" . $email->id . "'"; $result1 = $GLOBALS['db']->query($query1); if (count($result1) > 0) { $row = $GLOBALS['db']->fetchByAssoc($result1); $attachId = $row['id']; if ($fp = fopen($sugar_config['upload_dir'] . $attachId, 'rb')) { $contents = fread($fp, filesize($sugar_config['upload_dir'] . $attachId)); $emailFromAddress = $emailFromAddress . $contents; $email_description = $email_description . $contents; fclose($fp); } } if (preg_match('/MAILER-DAEMON|POSTMASTER/i', $emailFromAddress)) { //do we have the identifier tag in the email? $email_description = quoted_printable_decode($email_description); $matches = array(); if (preg_match('/index.php\\?entryPoint=removeme&identifier=[a-z0-9\\-]*/', $email_description, $matches)) { $identifiers = preg_split('/index.php\\?entryPoint=removeme&identifier=/', $matches[0], -1, PREG_SPLIT_NO_EMPTY); if (!empty($identifiers)) { //array should have only one element in it. $identifier = trim($identifiers[0]); if (!class_exists('CampaignLog')) { } $targeted = new CampaignLog(); $where = "campaign_log.activity_type='targeted' and campaign_log.target_tracker_key='{$identifier}'"; $query = $targeted->create_new_list_query('', $where); $result = $targeted->db->query($query); $row = $targeted->db->fetchByAssoc($result); if (!empty($row)) { //found entry //do not create another campaign_log record is we already have an //invalid email or send error entry for this tracker key. $query_log = "select * from campaign_log where target_tracker_key='{$row['target_tracker_key']}'"; $query_log .= " and (activity_type='invalid email' or activity_type='send error')"; $result_log = $targeted->db->query($query_log); $row_log = $targeted->db->fetchByAssoc($result_log); if (empty($row_log)) { $bounce = new CampaignLog(); $bounce->campaign_id = $row['campaign_id']; $bounce->target_tracker_key = $row['target_tracker_key']; $bounce->target_id = $row['target_id']; $bounce->target_type = $row['target_type']; $bounce->list_id = $row['list_id']; $bounce->marketing_id = $row['marketing_id']; $bounce->activity_date = $email->date_created; $bounce->related_type = 'Emails'; $bounce->related_id = $email->id; //do we have the phrase permanent error in the email body. if (preg_match('/permanent[ ]*error/', $email_description)) { //invalid email address $bounce->activity_type = 'invalid email'; } else { //other -bounced email. $bounce->activity_type = 'send error'; } $return_id = $bounce->save(); } } else { $GLOBALS['log']->info("Warning: skipping bounced email with this tracker_key(identifier) in the message body " . $identifier); } } else { //todo mark the email address as invalid. search for prospects/leads/contact associated //with this email address and set the invalid_email flag... also make email available. } } else { $GLOBALS['log']->info("Warning: skipping bounced email because it does not have the removeme link."); } } else { $GLOBALS['log']->info("Warning: skipping bounced email because the sender is not MAILER-DAEMON."); } }
$prefix = $_POST['prefix']; } if (empty($lead->id)) { $lead->id = create_guid(); $lead->new_with_id = true; } $GLOBALS['check_notify'] = true; //bug: 42398 - have to unset the id from the required_fields since it is not populated in the $_POST unset($lead->required_fields['id']); unset($lead->required_fields['team_name']); unset($lead->required_fields['team_count']); // checkRequired needs a major overhaul before it works for web to lead forms. $lead = $leadForm->handleSave($prefix, false, false, false, $lead); if (!empty($lead)) { //create campaign log $camplog = new CampaignLog(); $camplog->campaign_id = $_POST['campaign_id']; $camplog->related_id = $lead->id; $camplog->related_type = $lead->module_dir; $camplog->activity_type = "lead"; $camplog->target_type = $lead->module_dir; $campaign_log->activity_date = $timedate->now(); $camplog->target_id = $lead->id; $camplog->save(); //link campaignlog and lead if (isset($_POST['webtolead_email1']) && $_POST['webtolead_email1'] != null) { $lead->email1 = $_POST['webtolead_email1']; } if (isset($_POST['webtolead_email2']) && $_POST['webtolead_email2'] != null) { $lead->email2 = $_POST['webtolead_email2']; }
public function setup() { global $current_user; $current_user = SugarTestUserUtilities::createAnonymousUser(); //for the purpose of this test, we need to create some records with fake campaign and prospect list data, //however we do need to create some targets for the prospect list //create campaign tracker $ct = new CampaignTracker(); $ct->tracker_name = 'Campaign Log Unit Test Tracker'; $ct->tracker_url = 'sugarcrm.com'; $ct->campaign_id = $this->campaign_id; $ct->save(); $this->campaign_tracker = $ct; //for each type, create an object and populate the campaignLog list foreach ($this->targetObjectArray as $type) { //skip campaign tracker if ($type == 'CampaignTracker') { continue; } //create the new bean $bean = new $type(); if ($type == 'Account') { $bean->name = 'CampLog Unit Test Account'; } else { $bean->first_name = 'CampaignLog'; $bean->last_name = 'Test ' . $type; } $type_obj = 'target_' . $type; $bean->save(); $this->{$type_obj} = $bean; //save email $sea = new SugarEmailAddress(); $id = $this->{$type_obj}->id; $module = $this->{$type_obj}->module_dir; $new_addrs = array(); $primary = ''; $replyTo = ''; $invalid = ''; $optOut = ''; $in_workflow = false; $_REQUEST[$module . '_email_widget_id'] = 0; $_REQUEST[$module . '0emailAddress0'] = $type . '*****@*****.**'; $_REQUEST[$module . 'emailAddressPrimaryFlag'] = '0emailAddress0'; $_REQUEST[$module . 'emailAddressVerifiedFlag0'] = 'true'; $_REQUEST[$module . 'emailAddressVerifiedValue0'] = '*****@*****.**'; $requestVariablesSet = array('0emailAddress0', 'emailAddressPrimaryFlag', 'emailAddressVerifiedFlag0', 'emailAddressVerifiedValue0'); $sea->save($id, $module, $new_addrs, $primary, $replyTo, $invalid, $optOut, $in_workflow); //unset email request values for next run foreach ($requestVariablesSet as $k) { unset($_REQUEST[$k]); } //now create the campaign log $cl = new CampaignLog(); $cl->campaign_id = $this->campaign_id; $cl->tracker_key = $ct->tracker_key; $cl->target_id = $bean->id; $cl->target_type = $bean->module_dir; $cl->activity_type = 'targeted'; //options are targeted (user was sent an email), link (user clicked on link), removed (user opted out) and viewed (viewed) $cl->activity_date = date('Y-m-d H:i:s'); $cl->related_id = 'somebogusemailid' . date('His'); // this means link will not really work, but we are not testing email $cl->related_type = 'Emails'; $cl->list_id = $this->prospect_list_id; $cl->marketing_id = $this->email_marketing_id; $cl->save(); } //keep last created campaign log bean to be used to call functions $this->campaign_log = $cl; }
function set_as_sent($email_address, $success = true, $delete = true, $email_id = null, $email_type = null, $activity_type = null, $no_log = false) { //no_log is true when this process encounters an emailaddress from the suppression list. if ($no_log) { $this->table_name = 'emailman'; $query = 'DELETE FROM ' . $this->table_name . " WHERE id = {$this->id}"; $this->db->query($query); } else { global $timedate; $this->send_attempts++; if ($delete || $this->send_attempts > 5) { //create new campaign log record. require_once 'modules/CampaignLog/CampaignLog.php'; $campaign_log = new CampaignLog(); $campaign_log->campaign_id = $this->campaign_id; $campaign_log->target_tracker_key = $this->target_tracker_key; $campaign_log->target_id = $this->related_id; $campaign_log->target_type = $this->related_type; if (!$this->test) { $campaign_log->more_information = $email_address; } if (!empty($activity_type)) { $campaign_log->activity_type = $activity_type; } else { $campaign_log->activity_type = 'send error'; } $campaign_log->activity_date = $timedate->to_display_date_time(gmdate("Y-m-d H:i:s")); $campaign_log->list_id = $this->list_id; if ($success) { $campaign_log->related_id = $email_id; $campaign_log->related_type = $email_type; } $campaign_log->save(); if (!$success) { $campaign_log = new CampaignLog(); $campaign_log->campaign_id = $this->campaign_id; $campaign_log->target_tracker_key = $this->target_tracker_key; $campaign_log->target_id = $this->related_id; $campaign_log->target_type = $this->related_type; if (!$this->test) { $campaign_log->more_information = $email_address; } if (!empty($activity_type)) { $campaign_log->activity_type = $activity_type; } else { $campaign_log->activity_type = 'send error'; } $campaign_log->activity_date = $timedate->to_display_date_time(gmdate("Y-m-d H:i:s")); $campaign_log->list_id = $this->list_id; $campaign_log->save(); } $this->table_name = 'emailman'; $query = 'DELETE FROM ' . $this->table_name . " WHERE id = {$this->id}"; $this->db->query($query); } else { //try to send the email again at a later date. currently this timeperiod is set to one day. $query = 'UPDATE ' . $this->table_name . " SET in_queue='1', send_attempts='{$this->send_attempts}', in_queue_date='" . gmdate('Y-m-d H:i:s') . "' WHERE id = '{$this->id}'"; $this->db->query($query); } } }