-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
mailsend.php
234 lines (217 loc) · 9.33 KB
/
mailsend.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
<?php
/*+********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
********************************************************************************/
require_once 'include/utils/GetGroupUsers.php';
require_once 'include/utils/UserInfoUtil.php';
global $adb, $current_user;
//set the return module and return action and set the return id based on return module and record
$returnmodule = isset($_REQUEST['return_module']) ? vtlib_purify($_REQUEST['return_module']) : '';
$returnaction = isset($_REQUEST['return_action']) ? vtlib_purify($_REQUEST['return_action']) : '';
if ((($returnmodule != 'Emails') || ($returnmodule == 'Emails' && empty($_REQUEST['record']))) && !empty($_REQUEST['return_id'])) {
$returnid = vtlib_purify($_REQUEST['return_id']);
} else {
$returnid = $focus->id;
}
$adb->println("\n\nMail Sending Process has been started.");
//This function call is used to send mail to the assigned to user. In this mail CC and BCC addresses will be added.
if (isset($_REQUEST['assigntype']) && $_REQUEST['assigntype'] == 'T' && !empty($_REQUEST['assigned_group_id'])) {
$grp_obj = new GetGroupUsers();
$grp_obj->getAllUsersInGroup($_REQUEST['assigned_group_id']);
$users_list = constructList($grp_obj->group_users, 'INTEGER');
if (count($users_list) > 0) {
$sql = 'select first_name, last_name, email1, email2, secondaryemail from vtiger_users where id in ('. generateQuestionMarks($users_list) .')';
$params = array($users_list);
} else {
$sql = 'select first_name, last_name, email1, email2, secondaryemail from vtiger_users';
$params = array();
}
$res = $adb->pquery($sql, $params);
$user_email = '';
while ($user_info = $adb->fetch_array($res)) {
$email = $user_info['email1'];
if ($email == '' || $email == 'NULL') {
$email = $user_info['email2'];
if ($email == '' || $email == 'NULL') {
$email = $user_info['secondaryemail '];
}
}
if ($user_email=='') {
$user_email .= $user_info['first_name'].' '.$user_info['last_name'].'<'.$email.'>';
} else {
$user_email .= ','.$user_info['first_name'].' '.$user_info['last_name'].'<'.$email.'>';
}
$email='';
}
$to_email = $user_email;
} else {
$to_email = getUserEmailId('id', $focus->column_fields['assigned_user_id']);
}
$replyto = $_REQUEST['replyto'];
$cc = $_REQUEST['ccmail'];
$bcc = $_REQUEST['bccmail'];
$errorheader1 = 0;
$errorheader2 = 0;
if ($to_email == '' && $cc == '' && $bcc == '') {
$adb->println('Mail Error : send_mail function not called because To email id of assigned to user, CC and BCC are empty');
$mail_status_str = "'".$to_email."'=0&&&";
$errorheader1 = 1;
} else {
$res1 = $adb->pquery('select email1 from vtiger_users where id =?', array($current_user->id));
$val = $adb->query_result($res1, 0, 'email1');
$query = 'update vtiger_emaildetails set email_flag ="SENT",from_email =? where emailid=?';
$adb->pquery($query, array($val, $focus->id));
$mail_status_str = '';
}
$parentid= vtlib_purify($_REQUEST['parent_id']);
$myids=explode('|', $parentid);
$all_to_emailids = array();
$from_name = $current_user->user_name;
$from_address = $current_user->column_fields['email1'];
if (!empty($_REQUEST['from_email'])) {
$from_address = $_REQUEST['from_email'];
if ($from_address!=$val) {
$query = "update vtiger_emaildetails set from_email = concat(from_email,' > ',?) where emailid=?";
$adb->pquery($query, array($from_address, $focus->id));
}
$from_address = 'FROM:::>'.$from_address;
}
// Group emails
if (isset($_REQUEST['individual_emails']) && ($_REQUEST['individual_emails']=='on' || $_REQUEST['individual_emails']=='true' || $_REQUEST['individual_emails']=='1')) {
$individual_emails = 1;
} else {
$individual_emails = 0;
}
$logo = '';
$subject = '';
$description = '';
$Email_AutomaticMerge = GlobalVariable::getVariable('Email_Template_AutomaticMerge', 1);
$context = array(
'Email_AutomaticMerge' => $Email_AutomaticMerge
);
for ($i=0; $i<(count($myids)-1); $i++) {
$realid=explode('@', $myids[$i]);
$nemail=count($realid);
$mycrmid=$realid[0];
if (getModuleForField($realid[1]) == 'Users') {
//handle the mail send to vtiger_users
$rs = $adb->pquery('select email1 from vtiger_users where id=?', array($mycrmid));
$emailadd = $adb->query_result($rs, 0, 'email1');
$pmodule = 'Users';
$description = getMergedDescription($_REQUEST['description'], $mycrmid, $pmodule, $context);
$all_to_emailids[]= $emailadd;
if ($individual_emails) {
$mail_status = send_mail('Emails', $emailadd, $from_name, $from_address, $_REQUEST['subject'], $description, $cc, '', 'all', $focus->id);
$mail_status_str .= $emailadd.'='.$mail_status.'&&&';
}
} else {
//Send mail to account, lead or contact based on their ids
$pmodule=getSalesEntityType($mycrmid);
$subject = $_REQUEST['subject'];
$description = $_REQUEST['description'];
// Merge template
$ids = array();
if (isset($_REQUEST['merge_template_with']) && $_REQUEST['merge_template_with'] != '') {
$ids = explode(',', $_REQUEST['merge_template_with']);
}
if (count($ids) > 0) {
foreach ($ids as $id) {
$module = getSalesEntityType($id);
$subject = getMergedDescription($subject, $id, $module, $context);
$description = getMergedDescription($description, $id, $module, $context);
}
}
for ($j=1; $j<$nemail; $j++) {
$temp=$realid[$j];
$myquery='select fieldname from vtiger_field where fieldid=? and vtiger_field.presence in (0,2)';
$fresult=$adb->pquery($myquery, array($temp));
// vtlib customization: Enabling mail send from other modules
$myfocus = CRMEntity::getInstance($pmodule);
$myfocus->retrieve_entity_info($mycrmid, $pmodule);
$subject=getMergedDescription($subject, $mycrmid, $pmodule, $context);
$description = getMergedDescription($description, $mycrmid, $pmodule, $context);
$subject=getMergedDescription($subject, $current_user->id, 'Users', $context);
$description=getMergedDescription($description, $current_user->id, 'Users', $context);
$accid = getRelatedAccountContact($mycrmid, 'Accounts');
if (!empty($accid)) {
$subject=getMergedDescription($subject, $accid, 'Accounts', $context);
$description=getMergedDescription($description, $accid, 'Accounts', $context);
}
$fldname=$adb->query_result($fresult, 0, 'fieldname');
$emailadd=br2nl($myfocus->column_fields[$fldname]);
if ($emailadd != '') {
//Email Open/Stat Tracking
global $site_URL, $application_unique_key;
$EMail_OpenTrackingEnabled = GlobalVariable::getVariable('EMail_OpenTrackingEnabled', 1, 'Emails');
if ($EMail_OpenTrackingEnabled) {
$emailid = $focus->id;
$track_URL = "$site_URL/modules/Emails/TrackAccess.php?record=$mycrmid&mailid=$emailid&app_key=$application_unique_key";
$description = "$description<img src='$track_URL' alt='' width='1' height='1'>";
}
$pos = strpos($description, '$logo$');
if ($pos !== false) {
$description =str_replace('$logo$', '<img src="cid:logo" />', $description);
$logo=1;
} else {
$logo = 0;
}
$all_to_emailids[]= $emailadd;
if ($individual_emails) {
if (isPermitted($pmodule, 'DetailView', $mycrmid) == 'yes') {
$mail_status = send_mail('Emails', $emailadd, $from_name, $from_address, $subject, $description, $cc, '', 'all', $focus->id, $logo, $replyto);
}
$mail_status_str .= $emailadd.'='.$mail_status.'&&&';
//added to get remain the EditView page if an error occurs in mail sending
if ($mail_status != 1) {
$errorheader2 = 1;
}
}
}
}
}
}
// Sending group emails
if (!$individual_emails) {
$mail_status = send_mail('Emails', implode(',', $all_to_emailids), $from_name, $from_address, $subject, $description, $cc, '', 'all', $focus->id, $logo, $replyto);
$mail_status_str .= $mail_status.'&&&';
//added to get remain the EditView page if an error occurs in mail sending
if ($mail_status != 1) {
$errorheader2 = 1;
}
}
//Added to redirect the page to Emails/EditView if there is an error in mail sending
if ($errorheader1 == 1 || $errorheader2 == 1) {
$returnmodule = 'Emails';
$returnaction = 'EditView';
//This condition is added to set the record(email) id when we click on send mail button after returning mail error
if ($_REQUEST['mode'] == 'edit') {
$returnid = vtlib_purify($_REQUEST['record']);
} else {
$returnid = vtlib_purify($_REQUEST['currentid']);
}
$returnset = 'return_module='.$returnmodule.'&return_action='.$returnaction.'&return_id='.$returnid;
} else {
global $adb;
$date_var = date('Ymd');
$query = 'update vtiger_activity set date_start =? where activityid = ?';
$adb->pquery($query, array($date_var, $returnid));
}
//The following function call is used to parse and form a encoded error message and then pass to result page
$mail_error_str = getMailErrorString($mail_status_str);
$adb->println("Mail Sending Process has been finished.\n\n");
if (!empty($_REQUEST['popupaction']) && !isset($_REQUEST['ajax'])) {
echo '<script>
if (window.opener !== null) {
window.opener.location.href=window.opener.location.href;window.self.close();
} else {
history.back();
}
</script>';
die(); // to avoid unnecessay output to closing screen
}
?>