2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: Schedules email for delivery. emailman table holds emails for delivery.
41 * A cron job polls the emailman table and delivers emails when intended send date time is reached.
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 * Contributor(s): ______________________________________..
45 ********************************************************************************/
55 $campaign = new Campaign();
56 $campaign->retrieve($_REQUEST['record']);
57 $err_messages=array();
60 if (isset($_REQUEST['mode']) && $_REQUEST['mode'] =='test') {
64 //this is to account for the case of sending directly from summary page in wizards
66 if (isset($_REQUEST['wiz_mass'])) {
67 $mass[] = $_REQUEST['wiz_mass'];
68 $_POST['mass'] = $mass;
71 if (isset($_REQUEST['from_wiz'])) {
75 //if campaign status is 'sending' disallow this step.
76 if (!empty($campaign->status) && $campaign->status == 'sending') {
77 $err_messages[]=$mod_strings['ERR_SENDING_NOW'];
79 $current_date = $campaign->db->now();
81 //start scheduling now.....
82 foreach ($_POST['mass'] as $message_id) {
84 //fetch email marketing definition.
85 if (!class_exists('EmailMarketing')) require_once('modules/EmailMarketing/EmailMarketing.php');
88 $marketing = new EmailMarketing();
89 $marketing->retrieve($message_id);
91 //make sure that the marketing message has a mailbox.
93 if (empty($marketing->inbound_email_id)) {
96 echo "<h4>{$mod_strings['ERR_NO_MAILBOX']}</h4>";
97 echo "<BR><a href='index.php?module=EmailMarketing&action=EditView&record={$marketing->id}'>$marketing->name</a>";
104 $mergedvalue=$timedate->merge_date_time($marketing->date_start,$marketing->time_start);
106 $send_date_time = $timedate->getNow()->get("-60 seconds")->asDb();
108 $send_date_time = $timedate->to_db($mergedvalue);
110 $send_date_time = $campaign->db->convert($campaign->db->quoted($send_date_time), "datetime");
112 //find all prospect lists associated with this email marketing message.
113 if ($marketing->all_prospect_lists == 1) {
114 $query="SELECT prospect_lists.id prospect_list_id from prospect_lists ";
115 $query.=" INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = prospect_lists.id";
116 $query.=" WHERE plc.campaign_id='{$campaign->id}'";
117 $query.=" AND prospect_lists.deleted=0";
118 $query.=" AND plc.deleted=0";
120 $query.=" AND prospect_lists.list_type='test'";
122 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
125 $query="select email_marketing_prospect_lists.* FROM email_marketing_prospect_lists ";
126 $query.=" inner join prospect_lists on prospect_lists.id = email_marketing_prospect_lists.prospect_list_id";
127 $query.=" WHERE prospect_lists.deleted=0 and email_marketing_id = '$message_id' and email_marketing_prospect_lists.deleted=0";
130 $query.=" AND prospect_lists.list_type='test'";
132 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
135 $result=$campaign->db->query($query);
136 while (($row=$campaign->db->fetchByAssoc($result))!=null ) {
137 $prospect_list_id=$row['prospect_list_id'];
139 //delete all messages for the current campaign and current email marketing message.
140 $delete_emailman_query="delete from emailman where campaign_id='{$campaign->id}' and marketing_id='{$message_id}' and list_id='{$prospect_list_id}'";
141 $campaign->db->query($delete_emailman_query);
142 $auto = $campaign->db->getAutoIncrementSQL("emailman", "id");
144 $insert_query= "INSERT INTO emailman (date_entered, user_id, campaign_id, marketing_id,list_id, related_id, related_type, send_date_time";
145 $insert_query.= empty($auto)?"":",id";
147 $insert_query.= " SELECT $current_date,'{$current_user->id}',plc.campaign_id,'{$message_id}',plp.prospect_list_id, plp.related_id, plp.related_type,{$send_date_time}";
148 $insert_query.= empty($auto)?"":",$auto";
149 $insert_query.= " FROM prospect_lists_prospects plp ";
150 $insert_query.= "INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = plp.prospect_list_id ";
151 $insert_query.= "WHERE plp.prospect_list_id = '{$prospect_list_id}' ";
152 $insert_query.= "AND plp.deleted=0 ";
153 $insert_query.= "AND plc.deleted=0 ";
154 $insert_query.= "AND plc.campaign_id='{$campaign->id}'";
156 $campaign->db->query($insert_query);
160 //delete all entries from the emailman table that belong to the exempt list.
161 //TODO:SM: may want to move this to query clause above instead
163 $delete_query = "DELETE FROM emailman WHERE emailman.campaign_id='{$campaign->id}' AND (emailman.related_id, emailman.related_type) IN
164 (SELECT plp.related_id, plp.related_type FROM prospect_lists_prospects plp
165 INNER JOIN prospect_lists pl ON pl.id = plp.prospect_list_id
166 INNER JOIN prospect_list_campaigns plc ON plp.prospect_list_id = plc.prospect_list_id
167 WHERE plp.deleted = 0 AND plc.deleted = 0 AND pl.deleted = 0 AND pl.list_type = 'exempt' AND plc.campaign_id = '{$campaign->id}')
169 $campaign->db->query($delete_query);
172 $return_module=isset($_REQUEST['return_module'])?$_REQUEST['return_module']:'Campaigns';
173 $return_action=isset($_REQUEST['return_action'])?$_REQUEST['return_action']:'DetailView';
174 $return_id=$_REQUEST['record'];
177 //navigate to EmailManDelivery..
178 $header_URL = "Location: index.php?action=EmailManDelivery&module=EmailMan&campaign_id={$_REQUEST['record']}&return_module={$return_module}&return_action={$return_action}&return_id={$return_id}&mode=test";
179 if($from_wiz){$header_URL .= "&from_wiz=true";}
181 //navigate back to campaign detail view...
182 $header_URL = "Location: index.php?action={$return_action}&module={$return_module}&record={$return_id}";
183 if($from_wiz){$header_URL .= "&from=send";}
185 $GLOBALS['log']->debug("about to post header URL of: $header_URL");