-
Notifications
You must be signed in to change notification settings - Fork 173
/
SendReminder.php
288 lines (249 loc) · 10.5 KB
/
SendReminder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
////////////////////////////////////////////////////
// PHPMailer - PHP email class
//
// Class for sending email using either
// sendmail, PHP mail(), or SMTP. Methods are
// based upon the standard AspEmail(tm) classes.
//
// Copyright (C) 2001 - 2003 Brent R. Matzelle
//
// License: LGPL, see LICENSE
////////////////////////////////////////////////////
/**
* PHPMailer - PHP email transport class
* @package PHPMailer
* @author Brent R. Matzelle
* @copyright 2001 - 2003 Brent R. Matzelle
*/
//file modified by richie
require_once('include/utils/utils.php');
require("modules/Emails/class.phpmailer.php");
require_once('include/logging.php');
require("config.php");
$current_user = Users::getActiveAdminUser();
// Set the default sender email id
global $HELPDESK_SUPPORT_EMAIL_ID;
$from = $HELPDESK_SUPPORT_EMAIL_ID;
if(empty($from)) {
// default configuration is empty?
$from = "[email protected]";
}
// Get the list of activity for which reminder needs to be sent
global $adb;
global $log;
$log =& LoggerManager::getLogger('SendReminder');
$log->debug(" invoked SendReminder ");
// retrieve the translated strings.
if(empty($current_language))
$current_language = 'en_us';
$app_strings = return_application_language($current_language);
//modified query for recurring events -Jag
$query="select vtiger_crmentity.crmid,vtiger_seactivityrel.crmid as setype,vtiger_activity.*,vtiger_activity_reminder.reminder_time,vtiger_activity_reminder.reminder_sent,vtiger_activity_reminder.recurringid,vtiger_recurringevents.recurringdate from vtiger_activity inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_activity.activityid inner join vtiger_activity_reminder on vtiger_activity.activityid=vtiger_activity_reminder.activity_id left outer join vtiger_recurringevents on vtiger_activity.activityid=vtiger_recurringevents.activityid left outer join vtiger_seactivityrel on vtiger_seactivityrel.activityid = vtiger_activity.activityid where DATE_FORMAT(vtiger_activity.date_start,'%Y-%m-%d, %H:%i:%s') >= '".date('Y-m-d')."' and vtiger_crmentity.crmid != 0 and vtiger_activity.eventstatus = 'Planned' and vtiger_activity_reminder.reminder_sent = 0 group by vtiger_activity.activityid,vtiger_recurringevents.recurringid";
$result = $adb->pquery($query, array());
if($adb->num_rows($result) >= 1)
{
while($result_set = $adb->fetch_array($result))
{
$date_start = $result_set['date_start'];
$time_start = $result_set['time_start'];
$reminder_time = $result_set['reminder_time'];
$date = new DateTimeField( null );
$curr_time = strtotime($date->getDisplayDateTimeValue())/60;
$activity_id = $result_set['activityid'];
$activitymode = ($result_set['activitytype'] == "Task")?"Task":"Events";
$parent_type = $result_set['setype'];
$activity_sub = $result_set['subject'];
$to_addr='';
if($parent_type!='')
$parent_content = getParentInfo($parent_type)."\n";
else
$parent_content = "";
//code included for recurring events by jaguar starts
$recur_id = $result_set['recurringid'];
$current_date=date('Y-m-d');
if($recur_id == 0)
{
$date_start = $result_set['date_start'];
}
else
{
$date_start = $result_set['recurringdate'];
}
//code included for recurring events by jaguar ends
$date = new DateTimeField("$date_start $time_start");
$activity_time = strtotime($date->getDisplayDateTimeValue())/60;
if (($activity_time - $curr_time) > 0 && ($activity_time - $curr_time) <= $reminder_time)
{
$log->debug(" InSide REMINDER");
$query_user="SELECT vtiger_users.email1,vtiger_salesmanactivityrel.smid FROM vtiger_salesmanactivityrel inner join vtiger_users on vtiger_users.id=vtiger_salesmanactivityrel.smid where vtiger_salesmanactivityrel.activityid =? and vtiger_users.deleted=0";
$user_result = $adb->pquery($query_user, array($activity_id));
if($adb->num_rows($user_result)>=1)
{
while($user_result_row = $adb->fetch_array($user_result))
{
if($user_result_row['email1']!='' || $user_result_row['email1'] !=NULL)
{
$to_addr[] = $user_result_row['email1'];
}
}
}
// Retriving the Subject and message from reminder table
$sql = "select active,notificationsubject,notificationbody from vtiger_notificationscheduler where schedulednotificationid=8";
$result_main = $adb->pquery($sql, array());
$subject = $app_strings['Reminder'].$result_set['activitytype']." @ ".
$result_set['date_start']." ".$result_set['time_start']."] (". DateTimeField::getDBTimeZone() .")".
$adb->query_result($result_main,0,'notificationsubject');
//Set the mail body/contents here
$contents = nl2br($adb->query_result($result_main,0,'notificationbody')) ."\n\n ".
$app_strings['Subject']." : ".$activity_sub."\n ". $parent_content ." ".
$app_strings['Date & Time']." : ".$date_start." ".$time_start."(". DateTimeField::getDBTimeZone() .")\n\n ".
$app_strings['Visit_Link']." <a href='".$site_URL."/index.php?action=DetailView&module=Calendar&record=".$activity_id."&activity_mode=".$activitymode."'>".$app_strings['Click here']."</a>";
if(count($to_addr) >=1)
{
send_mail($to_addr,$from,$subject,$contents,$mail_server,$mail_server_username,$mail_server_password);
$upd_query = "UPDATE vtiger_activity_reminder SET reminder_sent=1 where activity_id=?";
$upd_params = array($activity_id);
if($recur_id!=0)
{
$upd_query.=" and recurringid =?";
array_push($upd_params, $recur_id);
}
$adb->pquery($upd_query, $upd_params);
}
}
}
}
/**
This function is used to assign parameters to the mail object and send it.
It takes the following as parameters.
$to as string - to address
$from as string - from address
$subject as string - subject if the mail
$contents as text - content of the mail
$mail_server as string - sendmail server name
$mail_server_username as string - sendmail server username
$mail_server_password as string - sendmail server password
*/
function send_mail($to,$from,$subject,$contents,$mail_server,$mail_server_username,$mail_server_password)
{
global $adb;
global $log;
$log->info("This is send_mail function in SendReminder.php(vtiger home).");
global $root_directory;
$mail = new PHPMailer();
$mail->Subject = $subject;
$mail->Body = nl2br($contents);//"This is the HTML message body <b>in bold!</b>";
$mail->IsSMTP(); // set mailer to use SMTP
$mailserverresult=$adb->pquery("select * from vtiger_systems where server_type='email'", array());
$mail_server = $adb->query_result($mailserverresult,0,'server');
$mail_server_username = $adb->query_result($mailserverresult,0,'server_username');
$mail_server_password = $adb->query_result($mailserverresult,0,'server_password');
$smtp_auth = $adb->query_result($mailserverresult,0,'smtp_auth');
$_REQUEST['server']=$mail_server;
$log->info("Mail Server Details => '".$mail_server."','".$mail_server_username."','".$mail_server_password."'");
$mail->Host = $mail_server; // specify main and backup server
if($smtp_auth == 'true')
$mail->SMTPAuth = true;
else
$mail->SMTPAuth = false;
$mail->Username = $mail_server_username ; // SMTP username
$mail->Password = $mail_server_password ; // SMTP password
$mail->From = $from;
$mail->FromName = $initialfrom;
$log->info("Mail sending process : From Name & email id => '".$initialfrom."','".$from."'");
foreach($to as $pos=>$addr)
{
$mail->AddAddress($addr); // name is optional
$log->info("Mail sending process : To Email id = '".$addr."' (set in the mail object)");
}
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
$flag = MailSend($mail);
$log->info("After executing the mail->Send() function.");
}
/**
This function is used to ensure mail has been sent sucessfully with out error.
It takes the mail object as the input and returns true if sucess else an error messaget.
*/
function MailSend($mail)
{
global $log;
if(!$mail->Send())
{
$log->info("Error in Mail Sending : Error log = '".$mail->ErrorInfo."'");
$msg = $mail->ErrorInfo;
}
else
{
$log->info("Mail has been sent from the vtigerCRM system : Status : '".$mail->ErrorInfo."'");
return true;
}
}
/**
This function is used to get the Parent mail id
It takes the input returnmodule as string and parentid as integer, returns the parent mailid as string.
*/
function getParentMailId($returnmodule,$parentid)
{
global $adb;
if($returnmodule == 'Leads')
{
$tablename = 'vtiger_leaddetails';
$idname = 'leadid';
}
if($returnmodule == 'Contacts' || $returnmodule == 'HelpDesk')
{
if($returnmodule == 'HelpDesk')
$parentid = $_REQUEST['contact_id'];
$tablename = 'vtiger_contactdetails';
$idname = 'contactid';
}
if($parentid != '')
{
$query = 'select * from '.$tablename.' where '.$idname.' = ?';
$res = $adb->pquery($query, array($parentid));
$mailid = $adb->query_result($res,0,'email');
}
if($mailid == '' && $returnmodule =='Contacts')
{
$mailid = $adb->query_result($res,0,'otheremail');
if($mailid == '')
$mailid = $adb->query_result($res,0,'secondaryemail');
}
return $mailid;
}
/**
This function is used to get the Parent type and its Name
It takes the input integer - crmid and returns the parent type and its name as string.
*/
function getParentInfo($value)
{
global $adb;
$parent_module = getSalesEntityType($value);
if($parent_module == "Leads")
{
$sql = "select * from vtiger_leaddetails where leadid=?";
$result = $adb->pquery($sql, array($value));
$first_name = $adb->query_result($result,0,"firstname");
$last_name = $adb->query_result($result,0,"lastname");
$parent_name = $last_name.' '.$first_name;
}
elseif($parent_module == "Accounts")
{
$sql = "select * from vtiger_account where accountid=?";
$result = $adb->pquery($sql, array($value));
$account_name = $adb->query_result($result,0,"accountname");
$parent_name =$account_name;
}
elseif($parent_module == "Potentials")
{
$sql = "select * from vtiger_potential where potentialid=?";
$result = $adb->pquery($sql, array($value));
$potentialname = $adb->query_result($result,0,"potentialname");
$parent_name =$potentialname;
}
return $parent_module ." : ".$parent_name;
}
?>