ryxeo-glpi-git / htdocs / inc / mailing.class.php @ 1c14bcc4
Historique | Voir | Annoter | Télécharger (36,3 ko)
1 | 1c14bcc4 | Eric Seigne | <?php
|
---|---|---|---|
2 | /*
|
||
3 | * @version $Id: mailing.class.php 7763 2009-01-06 18:44:50Z moyo $
|
||
4 | -------------------------------------------------------------------------
|
||
5 | GLPI - Gestionnaire Libre de Parc Informatique
|
||
6 | Copyright (C) 2003-2009 by the INDEPNET Development Team.
|
||
7 | |||
8 | http://indepnet.net/ http://glpi-project.org
|
||
9 | -------------------------------------------------------------------------
|
||
10 | |||
11 | LICENSE
|
||
12 | |||
13 | This file is part of GLPI.
|
||
14 | |||
15 | GLPI is free software; you can redistribute it and/or modify
|
||
16 | it under the terms of the GNU General Public License as published by
|
||
17 | the Free Software Foundation; either version 2 of the License, or
|
||
18 | (at your option) any later version.
|
||
19 | |||
20 | GLPI is distributed in the hope that it will be useful,
|
||
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
23 | GNU General Public License for more details.
|
||
24 | |||
25 | You should have received a copy of the GNU General Public License
|
||
26 | along with GLPI; if not, write to the Free Software
|
||
27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
28 | --------------------------------------------------------------------------
|
||
29 | */
|
||
30 | |||
31 | // ----------------------------------------------------------------------
|
||
32 | // Original Author of file:
|
||
33 | // Purpose of file:
|
||
34 | // ----------------------------------------------------------------------
|
||
35 | |||
36 | if (!defined('GLPI_ROOT')){ |
||
37 | die("Sorry. You can't access directly to this file"); |
||
38 | } |
||
39 | |||
40 | require_once(GLPI_ROOT . "/lib/phpmailer/class.phpmailer.php"); |
||
41 | |||
42 | /**
|
||
43 | * glpi_phpmailer class extends
|
||
44 | */
|
||
45 | class glpi_phpmailer extends phpmailer { |
||
46 | |||
47 | /// Set default variables for all new objects
|
||
48 | var $WordWrap = 80; |
||
49 | /// Defaut charset
|
||
50 | var $CharSet ="utf-8"; |
||
51 | |||
52 | |||
53 | /**
|
||
54 | * Constructor
|
||
55 | **/
|
||
56 | function glpi_phpmailer(){ |
||
57 | global $CFG_GLPI; |
||
58 | |||
59 | // Comes from config
|
||
60 | $this->SetLanguage("en", GLPI_ROOT . "/lib/phpmailer/language/"); |
||
61 | if($CFG_GLPI['smtp_mode'] == '1') { |
||
62 | $this->Host = $CFG_GLPI['smtp_host']; |
||
63 | $this->Port = $CFG_GLPI['smtp_port']; |
||
64 | |||
65 | if($CFG_GLPI['smtp_username'] != '') { |
||
66 | $this->SMTPAuth = true; |
||
67 | $this->Username = $CFG_GLPI['smtp_username']; |
||
68 | $this->Password = $CFG_GLPI['smtp_password']; |
||
69 | } |
||
70 | |||
71 | /*if($CFG_GLPI['debug']=="2"){
|
||
72 | $this->SMTPDebug = TRUE;
|
||
73 | }*/
|
||
74 | |||
75 | $this->Mailer = "smtp"; |
||
76 | } |
||
77 | |||
78 | } |
||
79 | |||
80 | |||
81 | |||
82 | |||
83 | } |
||
84 | |||
85 | |||
86 | /**
|
||
87 | * Mailing class for trackings
|
||
88 | */
|
||
89 | class Mailing |
||
90 | { |
||
91 | //! mailing type (new,attrib,followup,finish)
|
||
92 | var $type=NULL; |
||
93 | /** Job class variable - job to be mailed
|
||
94 | * @see Job
|
||
95 | */
|
||
96 | var $job=NULL; |
||
97 | /** User class variable - user who make changes
|
||
98 | * @see User
|
||
99 | */
|
||
100 | var $user=NULL; |
||
101 | /// Is the followupadded private ?
|
||
102 | var $followupisprivate=NULL; |
||
103 | |||
104 | |||
105 | /**
|
||
106 | * Constructor
|
||
107 | * @param $type mailing type (new,attrib,followup,finish)
|
||
108 | * @param $job Job to mail
|
||
109 | * @param $user User who made change
|
||
110 | * @param $followupisprivate true if the currently added/modified followup is private
|
||
111 | * @return nothing
|
||
112 | */
|
||
113 | function Mailing ($type="",$job=NULL,$user=NULL,$followupisprivate=false) |
||
114 | { |
||
115 | $this->type=$type; |
||
116 | if (!isset($job->hardwaredatas)||!count($job->hardwaredatas)){ |
||
117 | $job->getHardwareData();
|
||
118 | } |
||
119 | $this->job=$job; |
||
120 | $this->user=$user; |
||
121 | $this->followupisprivate=$followupisprivate; |
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | * Give mails to send the mail
|
||
126 | *
|
||
127 | * Determine email to send mail using global config and Mailing type
|
||
128 | * @param $sendprivate false : all users; true : only users who have the right to see private followups
|
||
129 | *
|
||
130 | * @return array containing email
|
||
131 | */
|
||
132 | function get_users_to_send_mail($sendprivate=false) |
||
133 | { |
||
134 | global $DB,$CFG_GLPI; |
||
135 | |||
136 | $emails=array(); |
||
137 | |||
138 | $query="SELECT * FROM glpi_mailing WHERE type='".$this->type."'"; |
||
139 | $result=$DB->query($query); |
||
140 | if ($DB->numrows($result)){ |
||
141 | |||
142 | $select =""; |
||
143 | $join=""; |
||
144 | $joinprofile=""; |
||
145 | // If send private is the user can see private followups ?
|
||
146 | if ($sendprivate){ |
||
147 | $join=" INNER JOIN glpi_users_profiles |
||
148 | ON (glpi_users_profiles.FK_users = glpi_users.ID
|
||
149 | ".getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$this->job->fields['FK_entities'],true).") |
||
150 | INNER JOIN glpi_profiles
|
||
151 | ON (glpi_profiles.ID = glpi_users_profiles.FK_profiles AND glpi_profiles.interface='central' AND glpi_profiles.show_full_ticket = '1') ";
|
||
152 | $joinprofile= "INNER JOIN glpi_profiles |
||
153 | ON (glpi_profiles.ID = glpi_users_profiles.FK_profiles AND glpi_profiles.interface='central' AND glpi_profiles.show_full_ticket = '1') ";
|
||
154 | |||
155 | } |
||
156 | |||
157 | while ($data=$DB->fetch_assoc($result)){ |
||
158 | switch ($data["item_type"]){ |
||
159 | case USER_MAILING_TYPE : |
||
160 | switch($data["FK_item"]){ |
||
161 | // ADMIN SEND
|
||
162 | case ADMIN_MAILING : |
||
163 | if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]])){ |
||
164 | $emails[$CFG_GLPI["admin_email"]]=$CFG_GLPI["default_language"]; |
||
165 | } |
||
166 | break;
|
||
167 | // ADMIN ENTITY SEND
|
||
168 | case ADMIN_ENTITY_MAILING : |
||
169 | $query2 = "SELECT admin_email AS EMAIL |
||
170 | FROM glpi_entities_data
|
||
171 | WHERE (FK_entities = '".$this->job->fields["FK_entities"]."')"; |
||
172 | if ($result2 = $DB->query($query2)) { |
||
173 | if ($DB->numrows($result2)==1){ |
||
174 | $row = $DB->fetch_array($result2); |
||
175 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
176 | $emails[$row['EMAIL']]=$CFG_GLPI["default_language"]; |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | break;
|
||
181 | // ASSIGN SEND
|
||
182 | case ASSIGN_MAILING : |
||
183 | if (isset($this->job->fields["assign"])&&$this->job->fields["assign"]>0){ |
||
184 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
185 | FROM glpi_users $join
|
||
186 | WHERE (glpi_users.ID = '".$this->job->fields["assign"]."')"; |
||
187 | if ($result2 = $DB->query($query2)) { |
||
188 | if ($DB->numrows($result2)==1){ |
||
189 | $row = $DB->fetch_array($result2); |
||
190 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
191 | $emails[$row['EMAIL']]=$row['LANG']; |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | break;
|
||
197 | // ASSIGN SEND
|
||
198 | case ASSIGN_ENT_MAILING : |
||
199 | |||
200 | if (!$sendprivate&&isset($this->job->fields["assign_ent"])&&$this->job->fields["assign_ent"]>0){ |
||
201 | $query2 = "SELECT DISTINCT glpi_enterprises.email AS EMAIL |
||
202 | FROM glpi_enterprises
|
||
203 | WHERE (glpi_enterprises.ID = '".$this->job->fields["assign_ent"]."')"; |
||
204 | if ($result2 = $DB->query($query2)) { |
||
205 | if ($DB->numrows($result2)==1){ |
||
206 | $row = $DB->fetch_array($result2); |
||
207 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
208 | $emails[$row['EMAIL']]=$CFG_GLPI["default_language"]; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | break;
|
||
214 | // ASSIGN GROUP SEND
|
||
215 | case ASSIGN_GROUP_MAILING : |
||
216 | if (isset($this->job->fields["assign_group"])&&$this->job->fields["assign_group"]>0){ |
||
217 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
218 | FROM glpi_users_groups
|
||
219 | INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID) $join
|
||
220 | WHERE glpi_users.deleted=0 AND glpi_users_groups.FK_groups='".$this->job->fields["assign_group"]."'"; |
||
221 | |||
222 | if ($result2= $DB->query($query)){ |
||
223 | if ($DB->numrows($result2)){ |
||
224 | while ($row=$DB->fetch_assoc($result2)){ |
||
225 | if (isValidEmail($row["EMAIL"])&&!isset($emails[$row['EMAIL']])){ |
||
226 | $emails[$row['EMAIL']]=$row['LANG']; |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | break;
|
||
233 | // SUPERVISOR ASSIGN GROUP SEND
|
||
234 | case SUPERVISOR_ASSIGN_GROUP_MAILING : |
||
235 | if (isset($this->job->fields["assign_group"])&&$this->job->fields["assign_group"]>0){ |
||
236 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
237 | FROM glpi_groups
|
||
238 | LEFT JOIN glpi_users ON (glpi_users.ID = glpi_groups.FK_users) $join
|
||
239 | WHERE (glpi_groups.ID = '".$this->job->fields["assign_group"]."')"; |
||
240 | if ($result2 = $DB->query($query2)) { |
||
241 | if ($DB->numrows($result2)==1){ |
||
242 | $row = $DB->fetch_array($result2); |
||
243 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
244 | $emails[$row['EMAIL']]=$row['LANG']; |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | break;
|
||
250 | |||
251 | |||
252 | // RECIPIENT SEND
|
||
253 | case RECIPIENT_MAILING : |
||
254 | if (isset($this->job->fields["recipient"])&&$this->job->fields["recipient"]>0){ |
||
255 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
256 | FROM glpi_users $join
|
||
257 | WHERE (glpi_users.ID = '".$this->job->fields["recipient"]."')"; |
||
258 | if ($result2 = $DB->query($query2)) { |
||
259 | if ($DB->numrows($result2)==1){ |
||
260 | $row = $DB->fetch_array($result2); |
||
261 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
262 | $emails[$row['EMAIL']]=$row['LANG']; |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | break;
|
||
268 | |||
269 | // AUTHOR SEND
|
||
270 | case AUTHOR_MAILING : |
||
271 | if ($this->job->fields["emailupdates"]&&isValidEmail($this->job->fields["uemail"])&&!isset($emails[$this->job->fields["uemail"]])){ |
||
272 | // Uemail = mail of the author ? -> use right of the author to see private followups
|
||
273 | // Else not see private
|
||
274 | $authorsend=false; |
||
275 | $authorlang=$CFG_GLPI["default_language"]; |
||
276 | if (!$sendprivate){ |
||
277 | $authorsend=true; |
||
278 | } |
||
279 | |||
280 | // Is the user have the same mail that uemail ?
|
||
281 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
282 | FROM glpi_users $join
|
||
283 | WHERE (glpi_users.ID = '".$this->job->fields["author"]."')"; |
||
284 | if ($result2 = $DB->query($query2)) { |
||
285 | if ($DB->numrows($result2)==1){ |
||
286 | $row = $DB->fetch_array($result2); |
||
287 | if ($row['EMAIL']==$this->job->fields["uemail"]){ |
||
288 | $authorsend=true; |
||
289 | $authorlang=$row['LANG']; |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | if ($authorsend){ |
||
295 | $emails[$this->job->fields["uemail"]]=$authorlang; |
||
296 | } |
||
297 | } |
||
298 | break;
|
||
299 | // SUPERVISOR ASSIGN GROUP SEND
|
||
300 | case SUPERVISOR_AUTHOR_GROUP_MAILING : |
||
301 | if (isset($this->job->fields["FK_group"])&&$this->job->fields["FK_group"]>0){ |
||
302 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
303 | FROM glpi_groups
|
||
304 | LEFT JOIN glpi_users ON (glpi_users.ID = glpi_groups.FK_users) $join
|
||
305 | WHERE (glpi_groups.ID = '".$this->job->fields["FK_group"]."')"; |
||
306 | if ($result2 = $DB->query($query2)) { |
||
307 | if ($DB->numrows($result2)==1){ |
||
308 | $row = $DB->fetch_array($result2); |
||
309 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
310 | $emails[$row['EMAIL']]=$row['LANG']; |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | break;
|
||
316 | |||
317 | // OLD ASSIGN SEND
|
||
318 | case OLD_ASSIGN_MAILING : |
||
319 | if (isset($this->job->fields["_old_assign"])&&$this->job->fields["_old_assign"]>0){ |
||
320 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
321 | FROM glpi_users $join
|
||
322 | WHERE (glpi_users.ID = '".$this->job->fields["_old_assign"]."')"; |
||
323 | if ($result2 = $DB->query($query2)) { |
||
324 | if ($DB->numrows($result2)==1){ |
||
325 | $row = $DB->fetch_array($result2); |
||
326 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
327 | $emails[$row['EMAIL']]=$row['LANG']; |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | break;
|
||
333 | // TECH SEND
|
||
334 | case TECH_MAILING : |
||
335 | if (isset($this->job->fields["computer"])&&$this->job->fields["computer"]>0&&isset($this->job->fields["device_type"])&&$this->job->fields["device_type"]>0){ |
||
336 | $ci= new CommonItem(); |
||
337 | $ci->getFromDB($this->job->fields["device_type"],$this->job->fields["computer"]); |
||
338 | if ($tmp=$ci->getField('tech_num')){ |
||
339 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
340 | FROM glpi_users $join
|
||
341 | WHERE (glpi_users.ID = '".$tmp."')"; |
||
342 | if ($result2 = $DB->query($query2)) { |
||
343 | if ($DB->numrows($result2)==1){ |
||
344 | $row = $DB->fetch_array($result2); |
||
345 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
346 | $emails[$row['EMAIL']]=$row['LANG']; |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | } |
||
352 | break;
|
||
353 | // USER SEND
|
||
354 | case USER_MAILING : |
||
355 | if (isset($this->job->fields["computer"])&&$this->job->fields["computer"]>0&&isset($this->job->fields["device_type"])&&$this->job->fields["device_type"]>0){ |
||
356 | $ci= new CommonItem(); |
||
357 | $ci->getFromDB($this->job->fields["device_type"],$this->job->fields["computer"]); |
||
358 | if ($tmp=$ci->getField('FK_users')){ |
||
359 | $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
360 | FROM glpi_users $join
|
||
361 | WHERE (glpi_users.ID = '".$tmp."')"; |
||
362 | if ($result2 = $DB->query($query2)) { |
||
363 | if ($DB->numrows($result2)==1){ |
||
364 | $row = $DB->fetch_array($result2); |
||
365 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
366 | $emails[$row['EMAIL']]=$row['LANG']; |
||
367 | } |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | break;
|
||
373 | |||
374 | } |
||
375 | break;
|
||
376 | case PROFILE_MAILING_TYPE : |
||
377 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
378 | FROM glpi_users_profiles
|
||
379 | INNER JOIN glpi_users ON (glpi_users_profiles.FK_users = glpi_users.ID) $joinprofile
|
||
380 | WHERE glpi_users.deleted=0 AND glpi_users_profiles.FK_profiles='".$data["FK_item"]."' ". |
||
381 | getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$this->job->fields['FK_entities'],true); |
||
382 | |||
383 | if ($result2= $DB->query($query)){ |
||
384 | if ($DB->numrows($result2)) |
||
385 | while ($row=$DB->fetch_assoc($result2)){ |
||
386 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
387 | $emails[$row['EMAIL']]=$row['LANG']; |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | break;
|
||
392 | case GROUP_MAILING_TYPE : |
||
393 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
394 | FROM glpi_users_groups
|
||
395 | INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID) $join
|
||
396 | WHERE glpi_users.deleted=0 AND glpi_users_groups.FK_groups='".$data["FK_item"]."'"; |
||
397 | |||
398 | if ($result2= $DB->query($query)){ |
||
399 | if ($DB->numrows($result2)) |
||
400 | while ($row=$DB->fetch_assoc($result2)){ |
||
401 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
402 | $emails[$row['EMAIL']]=$row['LANG']; |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | break;
|
||
407 | } |
||
408 | } |
||
409 | } |
||
410 | |||
411 | return $emails; |
||
412 | } |
||
413 | |||
414 | /**
|
||
415 | * Format the mail body to send
|
||
416 | * @param $format text or html
|
||
417 | * @param $sendprivate true if the email contains private followups
|
||
418 | * @return mail body string
|
||
419 | */
|
||
420 | function get_mail_body($format="text", $sendprivate=false) |
||
421 | { |
||
422 | global $CFG_GLPI, $LANG; |
||
423 | |||
424 | // Create message body from Job and type
|
||
425 | $body=""; |
||
426 | |||
427 | if($format=="html"){ |
||
428 | if ($CFG_GLPI["url_in_mail"]&&!empty($CFG_GLPI["url_base"])){ |
||
429 | $body.="URL :<a href=\"".$CFG_GLPI["url_base"]."/index.php?redirect=tracking_".$this->job->fields["ID"]."\">".$CFG_GLPI["url_base"]."/index.php?redirect=tracking_".$this->job->fields["ID"]." </a><br><br>"; |
||
430 | |||
431 | } |
||
432 | |||
433 | $body.=$this->job->textDescription($format); |
||
434 | $body.=$this->job->textFollowups($format, $sendprivate); |
||
435 | |||
436 | $body.="<br>-- <br>".$CFG_GLPI["mailing_signature"]; |
||
437 | $body.="</body></html>"; |
||
438 | $body=ereg_replace("\n","<br>\n",$body); |
||
439 | |||
440 | }else{ // text format |
||
441 | |||
442 | if ($CFG_GLPI["url_in_mail"]&&!empty($CFG_GLPI["url_base"])){ |
||
443 | $body.=$LANG["mailing"][1]."\n"; $body.="URL : ".$CFG_GLPI["url_base"]."/index.php?redirect=tracking_".$this->job->fields["ID"]."\n"; |
||
444 | |||
445 | } |
||
446 | |||
447 | $body.=$this->job->textDescription($format); |
||
448 | $body.=$this->job->textFollowups($format, $sendprivate); |
||
449 | |||
450 | $body.="\n-- \n".$CFG_GLPI["mailing_signature"]; |
||
451 | $body=ereg_replace("<br />","\n",$body); |
||
452 | $body=ereg_replace("<br>","\n",$body); |
||
453 | } |
||
454 | |||
455 | return $body; |
||
456 | } |
||
457 | |||
458 | /**
|
||
459 | * Format the mail sender to send
|
||
460 | * @return mail sender email string
|
||
461 | */
|
||
462 | function get_mail_sender(){ |
||
463 | global $CFG_GLPI,$DB; |
||
464 | |||
465 | $query = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '".$this->job->fields["FK_entities"]."')"; |
||
466 | if ($result=$DB->query($query)){ |
||
467 | if ($DB->numrows($result)){ |
||
468 | $data=$DB->fetch_assoc($result); |
||
469 | if (isValidEmail($data["EMAIL"])){ |
||
470 | return $data["EMAIL"]; |
||
471 | } |
||
472 | } |
||
473 | } |
||
474 | |||
475 | return $CFG_GLPI["admin_email"]; |
||
476 | } |
||
477 | |||
478 | /**
|
||
479 | * Format the mail subject to send
|
||
480 | * @return mail subject string
|
||
481 | */
|
||
482 | function get_mail_subject() |
||
483 | { |
||
484 | global $LANG; |
||
485 | |||
486 | // Create the message subject
|
||
487 | $subject=sprintf("%s%07d%s","[SUPPORT RYXEO #",$this->job->fields["ID"],"] "); |
||
488 | |||
489 | if (isMultiEntitiesMode()){
|
||
490 | $subject.=getDropdownName("glpi_entities",$this->job->fields['FK_entities'])." | "; |
||
491 | } |
||
492 | |||
493 | switch ($this->type){ |
||
494 | case "new": |
||
495 | $subject.=$LANG["mailing"][9]; |
||
496 | break;
|
||
497 | case "attrib": |
||
498 | $subject.=$LANG["mailing"][12]; |
||
499 | break;
|
||
500 | case "followup": |
||
501 | $subject.=$LANG["mailing"][10]; |
||
502 | break;
|
||
503 | case "update": |
||
504 | $subject.=$LANG["mailing"][30]; |
||
505 | break;
|
||
506 | case "finish": |
||
507 | $subject.=$LANG["mailing"][11]." ".convDateTime($this->job->fields["closedate"]); |
||
508 | break;
|
||
509 | default :
|
||
510 | $subject.=$LANG["mailing"][13]; |
||
511 | break;
|
||
512 | } |
||
513 | |||
514 | if (strlen($this->job->fields['name'])>150){ |
||
515 | $subject.=" - ".utf8_substr($this->job->fields['name'],0,150)." (...)"; |
||
516 | }else{
|
||
517 | $subject.=" - ".$this->job->fields['name']; |
||
518 | } |
||
519 | |||
520 | return $subject; |
||
521 | } |
||
522 | |||
523 | /**
|
||
524 | * Get reply to address
|
||
525 | * @param $sender sender address
|
||
526 | * @return return mail
|
||
527 | */
|
||
528 | function get_reply_to_address ($sender){ |
||
529 | global $CFG_GLPI,$DB; |
||
530 | |||
531 | $replyto=$CFG_GLPI["admin_email"]; |
||
532 | |||
533 | // Entity conf
|
||
534 | $query = "SELECT admin_email AS EMAIL, admin_reply AS REPLY FROM glpi_entities_data WHERE (FK_entities = '".$this->job->fields["FK_entities"]."')"; |
||
535 | if ($result=$DB->query($query)){ |
||
536 | if ($DB->numrows($result)){ |
||
537 | $data=$DB->fetch_assoc($result); |
||
538 | if (isValidEmail($data["REPLY"])){ |
||
539 | return $data["REPLY"]; |
||
540 | } else if (isValidEmail($data["EMAIL"])){ |
||
541 | $replyto=$data["EMAIL"]; |
||
542 | } |
||
543 | } |
||
544 | } |
||
545 | // Global conf
|
||
546 | if (isValidEmail($CFG_GLPI["admin_reply"])){ |
||
547 | return $CFG_GLPI["admin_reply"]; |
||
548 | } |
||
549 | |||
550 | // No specific config
|
||
551 | switch ($this->type){ |
||
552 | case "new": |
||
553 | if (isset($this->job->fields["uemail"]) && isValidEmail($this->job->fields["uemail"])) { |
||
554 | $replyto=$this->job->fields["uemail"]; |
||
555 | } else {
|
||
556 | $replyto=$sender; |
||
557 | } |
||
558 | break;
|
||
559 | case "followup": |
||
560 | case "update": |
||
561 | if (isset($this->user->fields["email"]) && isValidEmail($this->user->fields["email"])) { |
||
562 | $replyto=$this->user->fields["email"]; |
||
563 | } else {
|
||
564 | $replyto=$sender; |
||
565 | } |
||
566 | break;
|
||
567 | } |
||
568 | return $replyto; |
||
569 | } |
||
570 | /**
|
||
571 | * Send mail function
|
||
572 | *
|
||
573 | * Construct email and send it
|
||
574 | *
|
||
575 | * @return mail subject string
|
||
576 | */
|
||
577 | function send() |
||
578 | { |
||
579 | global $CFG_GLPI,$LANG; |
||
580 | if ($CFG_GLPI["mailing"]) |
||
581 | { |
||
582 | if (!is_null($this->job)&&in_array($this->type,array("new","update","followup","finish"))) |
||
583 | { |
||
584 | $senderror=false; |
||
585 | // get users to send mail
|
||
586 | $users=array(); |
||
587 | // All users
|
||
588 | $users[0]=$this->get_users_to_send_mail(0); |
||
589 | // Users who could see private followups
|
||
590 | $users[1]=$this->get_users_to_send_mail(1); |
||
591 | // Delete users who can see private followups to all users list
|
||
592 | foreach ($users[1] as $email => $lang){ |
||
593 | if (isset($users[0][$email])){ |
||
594 | unset($users[0][$email]); |
||
595 | } |
||
596 | } |
||
597 | |||
598 | // New Followup is private : do not send to common users
|
||
599 | if ($this->followupisprivate){ |
||
600 | unset($users[0]); |
||
601 | } |
||
602 | |||
603 | $subjects=array(); |
||
604 | // get sender
|
||
605 | $sender= $this->get_mail_sender(); |
||
606 | // get reply-to address : user->email ou job_email if not set OK
|
||
607 | $replyto=$this->get_reply_to_address ($sender); |
||
608 | |||
609 | $messageerror=$LANG["mailing"][47]; |
||
610 | // Send all mails
|
||
611 | foreach ($users as $private=>$someusers) { |
||
612 | if (count($someusers)){ |
||
613 | |||
614 | $mmail=new glpi_phpmailer(); |
||
615 | $mmail->From=$sender; |
||
616 | $mmail->AddReplyTo("$replyto", ''); |
||
617 | $mmail->FromName=$sender; |
||
618 | $mmail->isHTML(true); |
||
619 | |||
620 | $bodys=array(); |
||
621 | $altbodys=array(); |
||
622 | foreach ($someusers as $email => $lang){ |
||
623 | if (!isset($subjects[$lang])||!isset($bodys[$lang])||!isset($altbodys[$lang])){ |
||
624 | loadLanguage($lang);
|
||
625 | if (!isset($subjects[$lang])){ |
||
626 | $subjects[$lang]=$this->get_mail_subject(); |
||
627 | } |
||
628 | $bodys[$lang]=$this->get_mail_body("html",$private); |
||
629 | $altbodys[$lang]=$this->get_mail_body("text",$private); |
||
630 | } |
||
631 | $mmail->Subject=$subjects[$lang]; |
||
632 | $mmail->Body=$bodys[$lang]; |
||
633 | $mmail->AltBody=$altbodys[$lang]; |
||
634 | |||
635 | $mmail->AddAddress($email, ""); |
||
636 | |||
637 | if(!$mmail->Send()){ |
||
638 | $senderror=true; |
||
639 | addMessageAfterRedirect($messageerror."<br>".$mmail->ErrorInfo); |
||
640 | }else{
|
||
641 | logInFile("mail",$LANG["tracking"][38]." ".$email.": ".$subjects[$lang]."\n"); |
||
642 | } |
||
643 | |||
644 | $mmail->ClearAddresses(); |
||
645 | } |
||
646 | } |
||
647 | } |
||
648 | // Reinit language
|
||
649 | loadLanguage(); |
||
650 | if ($senderror){ |
||
651 | return false; |
||
652 | } |
||
653 | } else {
|
||
654 | addMessageAfterRedirect($LANG["mailing"][112]); |
||
655 | } |
||
656 | } |
||
657 | return true; |
||
658 | } |
||
659 | } |
||
660 | |||
661 | /**
|
||
662 | * Mailing class for reservations
|
||
663 | */
|
||
664 | class MailingResa{ |
||
665 | /** ReservationResa class variable
|
||
666 | * @see ReservationResa
|
||
667 | */
|
||
668 | var $resa; |
||
669 | //! type of mailing (new, update, delete)
|
||
670 | var $type; |
||
671 | |||
672 | /**
|
||
673 | * Constructor
|
||
674 | * @param $type mailing type (new,attrib,followup,finish)
|
||
675 | * @param $resa ReservationResa to mail
|
||
676 | * @return nothing
|
||
677 | */
|
||
678 | function MailingResa ($resa,$type="new") |
||
679 | { |
||
680 | $this->resa=$resa; |
||
681 | $this->type=$type; |
||
682 | |||
683 | } |
||
684 | |||
685 | /**
|
||
686 | * Give mails to send the mail
|
||
687 | *
|
||
688 | * Determine email to send mail using global config and Mailing type
|
||
689 | *
|
||
690 | * @return array containing email
|
||
691 | */
|
||
692 | function get_users_to_send_mail() |
||
693 | { |
||
694 | global $DB,$CFG_GLPI; |
||
695 | |||
696 | $emails=array(); |
||
697 | |||
698 | $query="SELECT * FROM glpi_mailing WHERE type='resa'"; |
||
699 | $result=$DB->query($query); |
||
700 | if ($DB->numrows($result)){ |
||
701 | while ($data=$DB->fetch_assoc($result)){ |
||
702 | switch ($data["item_type"]){ |
||
703 | case USER_MAILING_TYPE : |
||
704 | switch ($data["FK_item"]){ |
||
705 | // ADMIN SEND
|
||
706 | case ADMIN_MAILING : |
||
707 | if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]])) |
||
708 | $emails[$CFG_GLPI["admin_email"]]=$CFG_GLPI["default_language"]; |
||
709 | break;
|
||
710 | // ADMIN ENTITY SEND
|
||
711 | case ADMIN_ENTITY_MAILING : |
||
712 | |||
713 | $ri=new ReservationItem(); |
||
714 | $ci=new CommonItem(); |
||
715 | $entity=-1; |
||
716 | if ($ri->getFromDB($this->resa->fields["id_item"])){ |
||
717 | if ($ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device']) ){ |
||
718 | $entity=$ci->getField('FK_entities'); |
||
719 | } |
||
720 | } |
||
721 | |||
722 | if ($entity>=0){ |
||
723 | $query2 = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '".$entity."')"; |
||
724 | if ($result2 = $DB->query($query2)) { |
||
725 | if ($DB->numrows($result2)==1){ |
||
726 | $row = $DB->fetch_array($result2); |
||
727 | if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]])){ |
||
728 | $emails[$row['EMAIL']]=$CFG_GLPI["default_language"]; |
||
729 | } |
||
730 | } |
||
731 | } |
||
732 | } |
||
733 | break;
|
||
734 | // AUTHOR SEND
|
||
735 | case AUTHOR_MAILING : |
||
736 | $user = new User; |
||
737 | if ($user->getFromDB($this->resa->fields["id_user"])) |
||
738 | if (isValidEmail($user->fields["email"])&&!isset($emails[$user->fields["email"]])){ |
||
739 | $emails[$user->fields["email"]]=$user->fields['language']; |
||
740 | } |
||
741 | break;
|
||
742 | // TECH SEND
|
||
743 | case TECH_MAILING : |
||
744 | $ri=new ReservationItem(); |
||
745 | if ($ri->getFromDB($this->resa->fields["id_item"])){ |
||
746 | $ci=new CommonItem(); |
||
747 | $ci->getFromDB($ri->fields["device_type"],$ri->fields["id_device"]); |
||
748 | |||
749 | if ($tmp=$ci->getField('tech_num')){ |
||
750 | $query2 = "SELECT glpi_users.email as EMAIL, glpi_users.language as LANG |
||
751 | FROM glpi_users WHERE (glpi_users.ID = '".$tmp."')"; |
||
752 | if ($result2 = $DB->query($query2)) { |
||
753 | if ($DB->numrows($result2)==1){ |
||
754 | $row = $DB->fetch_row($result2); |
||
755 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
756 | $emails[$row['EMAIL']]=$row['LANG']; |
||
757 | } |
||
758 | } |
||
759 | } |
||
760 | } |
||
761 | } |
||
762 | break;
|
||
763 | // USER SEND
|
||
764 | case USER_MAILING : |
||
765 | $ri=new ReservationItem(); |
||
766 | if ($ri->getFromDB($this->resa->fields["id_item"])){ |
||
767 | $ci=new CommonItem(); |
||
768 | $ci->getFromDB($ri->fields["device_type"],$ri->fields["id_device"]); |
||
769 | |||
770 | if ($tmp=$ci->getField('FK_users')){ |
||
771 | $query2 = "SELECT glpi_users.email AS EMAIL, glpi_users.language as LANG |
||
772 | FROM glpi_users WHERE (glpi_users.ID = '".$tmp."')"; |
||
773 | if ($result2 = $DB->query($query2)) { |
||
774 | if ($DB->numrows($result2)==1){ |
||
775 | $row = $DB->fetch_row($result2); |
||
776 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
777 | $emails[$row['EMAIL']]=$row['LANG']; |
||
778 | } |
||
779 | } |
||
780 | } |
||
781 | } |
||
782 | } |
||
783 | break;
|
||
784 | |||
785 | } |
||
786 | break;
|
||
787 | case PROFILE_MAILING_TYPE : |
||
788 | // Get entity
|
||
789 | $ri=new ReservationItem(); |
||
790 | $ri->getFromDB($this->resa->fields['id_item']); |
||
791 | $ci = new CommonItem(); |
||
792 | $ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device']); |
||
793 | $FK_entities=$ci->getField('FK_entities'); |
||
794 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language as LANG |
||
795 | FROM glpi_users_profiles
|
||
796 | INNER JOIN glpi_users ON (glpi_users_profiles.FK_users = glpi_users.ID)
|
||
797 | WHERE glpi_users_profiles.FK_profiles='".$data["FK_item"]."' |
||
798 | ".getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$FK_entities,true); |
||
799 | if ($result2= $DB->query($query)){ |
||
800 | if ($DB->numrows($result2)) |
||
801 | while ($row=$DB->fetch_assoc($result2)){ |
||
802 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
803 | $emails[$row['EMAIL']]=$row['LANG']; |
||
804 | } |
||
805 | } |
||
806 | } |
||
807 | break;
|
||
808 | case GROUP_MAILING_TYPE : |
||
809 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language as LANG |
||
810 | FROM glpi_users_groups
|
||
811 | INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID)
|
||
812 | WHERE glpi_users_groups.FK_groups='".$data["FK_item"]."'"; |
||
813 | if ($result2= $DB->query($query)){ |
||
814 | if ($DB->numrows($result2)) |
||
815 | while ($row=$DB->fetch_assoc($result2)){ |
||
816 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
817 | $emails[$row['EMAIL']]=$row['LANG']; |
||
818 | } |
||
819 | } |
||
820 | } |
||
821 | break;
|
||
822 | } |
||
823 | } |
||
824 | } |
||
825 | |||
826 | return $emails; |
||
827 | } |
||
828 | |||
829 | |||
830 | /**
|
||
831 | * Format the mail sender to send
|
||
832 | * @return mail sender email string
|
||
833 | */
|
||
834 | function get_mail_sender(){ |
||
835 | global $CFG_GLPI,$DB; |
||
836 | |||
837 | |||
838 | $ri=new ReservationItem(); |
||
839 | $ci=new CommonItem(); |
||
840 | $entity=-1; |
||
841 | if ($ri->getFromDB($this->resa->fields["id_item"])){ |
||
842 | if ($ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device']) ){ |
||
843 | $entity=$ci->getField('FK_entities'); |
||
844 | } |
||
845 | } |
||
846 | if ($entity>=0){ |
||
847 | $query = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '$entity')"; |
||
848 | if ($result=$DB->query($query)){ |
||
849 | if ($DB->numrows($result)){ |
||
850 | $data=$DB->fetch_assoc($result); |
||
851 | if (isValidEmail($data["EMAIL"])){ |
||
852 | return $data["EMAIL"]; |
||
853 | } |
||
854 | } |
||
855 | } |
||
856 | } |
||
857 | |||
858 | return $CFG_GLPI["admin_email"]; |
||
859 | } |
||
860 | |||
861 | |||
862 | /**
|
||
863 | * Format the mail body to send
|
||
864 | * @param $format text or html
|
||
865 | * @return mail body string
|
||
866 | */
|
||
867 | function get_mail_body($format="text"){ |
||
868 | global $CFG_GLPI; |
||
869 | |||
870 | // Create message body from Job and type
|
||
871 | $body=""; |
||
872 | |||
873 | if($format=="html"){ |
||
874 | |||
875 | $body.=$this->resa->textDescription("html"); |
||
876 | $body.="<br>-- <br>".$CFG_GLPI["mailing_signature"]; |
||
877 | $body.="</body></html>"; |
||
878 | $body=ereg_replace("\n","<br>",$body); |
||
879 | }else{ // text format |
||
880 | |||
881 | $body.=$this->resa->textDescription(); |
||
882 | $body.="\n-- \n".$CFG_GLPI["mailing_signature"]; |
||
883 | $body=ereg_replace("<br />","\n",$body); |
||
884 | $body=ereg_replace("<br>","\n",$body); |
||
885 | } |
||
886 | return $body; |
||
887 | } |
||
888 | /**
|
||
889 | * Format the mail subject to send
|
||
890 | * @return mail subject string
|
||
891 | */
|
||
892 | function get_mail_subject() |
||
893 | { |
||
894 | global $LANG; |
||
895 | |||
896 | // Create the message subject
|
||
897 | if ($this->type=="new") |
||
898 | $subject="[SUPPORT RYXEO] ".$LANG["mailing"][19]; |
||
899 | else if ($this->type=="update") $subject="[SUPPORT RYXEO] ".$LANG["mailing"][23]; |
||
900 | else if ($this->type=="delete") $subject="[SUPPORT RYXEO] ".$LANG["mailing"][29]; |
||
901 | |||
902 | return $subject; |
||
903 | } |
||
904 | |||
905 | /**
|
||
906 | * Get reply to address
|
||
907 | * @param $sender sender address
|
||
908 | * @return return mail
|
||
909 | */
|
||
910 | function get_reply_to_address ($sender){ |
||
911 | global $CFG_GLPI; |
||
912 | $replyto=""; |
||
913 | |||
914 | $user = new User; |
||
915 | if ($user->getFromDB($this->resa->fields["id_user"])){ |
||
916 | if (isValidEmail($user->fields["email"])) $replyto=$user->fields["email"]; |
||
917 | else $replyto=$sender; |
||
918 | } |
||
919 | else $replyto=$sender; |
||
920 | |||
921 | return $replyto; |
||
922 | } |
||
923 | /**
|
||
924 | * Send mail function
|
||
925 | *
|
||
926 | * Construct email and send it
|
||
927 | *
|
||
928 | * @return mail subject string
|
||
929 | */
|
||
930 | function send() |
||
931 | { |
||
932 | global $CFG_GLPI,$LANG; |
||
933 | if ($CFG_GLPI["mailing"]&&isValidEmail($CFG_GLPI["admin_email"])) |
||
934 | { |
||
935 | // get users to send mail
|
||
936 | $users=$this->get_users_to_send_mail(); |
||
937 | |||
938 | // get sender
|
||
939 | $sender= $this->get_mail_sender(); |
||
940 | // get reply-to address : user->email ou job_email if not set OK
|
||
941 | $replyto=$this->get_reply_to_address ($sender); |
||
942 | |||
943 | |||
944 | $mmail=new glpi_phpmailer(); |
||
945 | $mmail->From=$sender; |
||
946 | $mmail->AddReplyTo("$replyto", ''); |
||
947 | $mmail->FromName=$sender; |
||
948 | |||
949 | $mmail->isHTML(true); |
||
950 | |||
951 | // get subject
|
||
952 | $bodys=array(); |
||
953 | $altbodys=array(); |
||
954 | $subjects=array(); |
||
955 | // Send all mails
|
||
956 | if (count($users)){ |
||
957 | foreach ($users as $email => $lang){ |
||
958 | if (!isset($subjects[$lang])||!isset($bodys[$lang])||!isset($altbodys[$lang])){ |
||
959 | loadLanguage($lang);
|
||
960 | $subjects[$lang]=$this->get_mail_subject(); |
||
961 | $bodys[$lang]=$this->get_mail_body("html"); |
||
962 | $altbodys[$lang]=$this->get_mail_body("text"); |
||
963 | } |
||
964 | |||
965 | $mmail->Subject=$subjects[$lang]; |
||
966 | |||
967 | $mmail->Body=$bodys[$lang]; |
||
968 | $mmail->AltBody=$altbodys[$lang]; |
||
969 | |||
970 | $mmail->AddAddress($email, ""); |
||
971 | |||
972 | if(!$mmail->Send()){ |
||
973 | echo "<div class='center'>".$LANG["mailing"][47]."</div>"; |
||
974 | return false; |
||
975 | }else{
|
||
976 | logInFile("mail",$LANG["reservation"][40]." ".$email.": ".$subjects[$lang]."\n"); |
||
977 | } |
||
978 | |||
979 | $mmail->ClearAddresses(); |
||
980 | } |
||
981 | } else {
|
||
982 | return false; |
||
983 | } |
||
984 | } |
||
985 | return true; |
||
986 | } |
||
987 | |||
988 | } |
||
989 | |||
990 | |||
991 | /**
|
||
992 | * Mailing class for alerts
|
||
993 | */
|
||
994 | class MailingAlert |
||
995 | { |
||
996 | /// mailing type (contract,infocom,cartridge,consumable)
|
||
997 | var $type=NULL; |
||
998 | /// message to send
|
||
999 | var $message=""; |
||
1000 | /// working entity
|
||
1001 | var $entity=""; |
||
1002 | |||
1003 | /**
|
||
1004 | * Constructor
|
||
1005 | * @param $type mailing type (new,attrib,followup,finish)
|
||
1006 | * @param $message Message to send
|
||
1007 | * @param $entity Restrict to a defined entity
|
||
1008 | * @return nothing
|
||
1009 | */
|
||
1010 | function MailingAlert ($type,$message,$entity=-1) |
||
1011 | { |
||
1012 | $this->type=$type; |
||
1013 | $this->message=$message; |
||
1014 | $this->entity=$entity; |
||
1015 | } |
||
1016 | |||
1017 | |||
1018 | /**
|
||
1019 | * Give mails to send the mail
|
||
1020 | *
|
||
1021 | * Determine email to send mail using global config and Mailing type
|
||
1022 | *
|
||
1023 | * @return array containing email
|
||
1024 | */
|
||
1025 | function get_users_to_send_mail() |
||
1026 | { |
||
1027 | global $DB,$CFG_GLPI; |
||
1028 | |||
1029 | $emails=array(); |
||
1030 | |||
1031 | $query="SELECT * FROM glpi_mailing WHERE type='".$this->type."'"; |
||
1032 | $result=$DB->query($query); |
||
1033 | if ($DB->numrows($result)){ |
||
1034 | while ($data=$DB->fetch_assoc($result)){ |
||
1035 | switch ($data["item_type"]){ |
||
1036 | case USER_MAILING_TYPE : |
||
1037 | switch($data["FK_item"]){ |
||
1038 | // ADMIN SEND
|
||
1039 | case ADMIN_MAILING : |
||
1040 | if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]])) |
||
1041 | $emails[$CFG_GLPI["admin_email"]]=$CFG_GLPI["default_language"]; |
||
1042 | break;
|
||
1043 | // ADMIN ENTITY SEND
|
||
1044 | case ADMIN_ENTITY_MAILING : |
||
1045 | $query2 = "SELECT admin_email AS EMAIL |
||
1046 | FROM glpi_entities_data
|
||
1047 | WHERE (FK_entities = '".$this->entity."')"; |
||
1048 | if ($result2 = $DB->query($query2)) { |
||
1049 | if ($DB->numrows($result2)==1){ |
||
1050 | $row = $DB->fetch_array($result2); |
||
1051 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
1052 | $emails[$row['EMAIL']]=$CFG_GLPI["default_language"]; |
||
1053 | } |
||
1054 | } |
||
1055 | } |
||
1056 | break;
|
||
1057 | |||
1058 | } |
||
1059 | break;
|
||
1060 | case PROFILE_MAILING_TYPE : |
||
1061 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
1062 | FROM glpi_users_profiles
|
||
1063 | INNER JOIN glpi_users ON (glpi_users_profiles.FK_users = glpi_users.ID)
|
||
1064 | WHERE glpi_users_profiles.FK_profiles='".$data["FK_item"]."' |
||
1065 | ".getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$this->entity,true); |
||
1066 | |||
1067 | if ($result2= $DB->query($query)){ |
||
1068 | if ($DB->numrows($result2)){ |
||
1069 | while ($row=$DB->fetch_assoc($result2)){ |
||
1070 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
1071 | $emails[$row['EMAIL']]=$row['LANG']; |
||
1072 | } |
||
1073 | } |
||
1074 | } |
||
1075 | } |
||
1076 | break;
|
||
1077 | case GROUP_MAILING_TYPE : |
||
1078 | $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG |
||
1079 | FROM glpi_users_groups
|
||
1080 | INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID)
|
||
1081 | WHERE glpi_users_groups.FK_groups='".$data["FK_item"]."'"; |
||
1082 | |||
1083 | if ($result2= $DB->query($query)){ |
||
1084 | if ($DB->numrows($result2)) |
||
1085 | while ($row=$DB->fetch_assoc($result2)){ |
||
1086 | if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){ |
||
1087 | $emails[$row['EMAIL']]=$row['LANG']; |
||
1088 | } |
||
1089 | } |
||
1090 | } |
||
1091 | break;
|
||
1092 | } |
||
1093 | } |
||
1094 | } |
||
1095 | |||
1096 | return $emails; |
||
1097 | } |
||
1098 | |||
1099 | /**
|
||
1100 | * Format the mail body to send
|
||
1101 | * @param $format text or html
|
||
1102 | * @return mail body string
|
||
1103 | */
|
||
1104 | function get_mail_body($format="text"){ |
||
1105 | global $CFG_GLPI, $LANG; |
||
1106 | |||
1107 | // Create message body from Job and type
|
||
1108 | $body=""; |
||
1109 | |||
1110 | if($format=="html"){ |
||
1111 | |||
1112 | $body.=$this->message; |
||
1113 | $body.="<br>-- <br>".$CFG_GLPI["mailing_signature"]; |
||
1114 | $body.="</body></html>"; |
||
1115 | $body=ereg_replace("\n","<br>",$body); |
||
1116 | }else{ // text format |
||
1117 | |||
1118 | $body.=$this->message; |
||
1119 | $body.="\n-- \n".$CFG_GLPI["mailing_signature"]; |
||
1120 | $body=ereg_replace("<br />","\n",$body); |
||
1121 | $body=ereg_replace("<br>","\n",$body); |
||
1122 | } |
||
1123 | return $body; |
||
1124 | } |
||
1125 | |||
1126 | /**
|
||
1127 | * Format the mail subject to send
|
||
1128 | * @return mail subject string
|
||
1129 | */
|
||
1130 | function get_mail_subject() |
||
1131 | { |
||
1132 | global $LANG; |
||
1133 | |||
1134 | // Create the message subject
|
||
1135 | $subject="[SUPPORT RYXEO]"; |
||
1136 | |||
1137 | switch ($this->type){ |
||
1138 | case "alertcartridge" : |
||
1139 | $subject.=" ".$LANG["mailing"][33]. " - ".getDropdownName("glpi_entities",$this->entity); |
||
1140 | break;
|
||
1141 | case "alertconsumable": |
||
1142 | $subject.=" ".$LANG["mailing"][36]. " - ".getDropdownName("glpi_entities",$this->entity); |
||
1143 | break;
|
||
1144 | case "alertcontract": |
||
1145 | $subject.=" ".$LANG["mailing"][39]. " - ".getDropdownName("glpi_entities",$this->entity); |
||
1146 | break;
|
||
1147 | case "alertinfocom": |
||
1148 | $subject.=" ".$LANG["mailing"][41]. " - ".getDropdownName("glpi_entities",$this->entity); |
||
1149 | break;
|
||
1150 | case "alertlicense": |
||
1151 | $subject.=" ".$LANG["mailing"][52]. " - ".getDropdownName("glpi_entities",$this->entity); |
||
1152 | break;
|
||
1153 | } |
||
1154 | return $subject; |
||
1155 | } |
||
1156 | |||
1157 | /**
|
||
1158 | * Format the mail sender to send
|
||
1159 | * @return mail sender email string
|
||
1160 | */
|
||
1161 | function get_mail_sender(){ |
||
1162 | global $CFG_GLPI,$DB; |
||
1163 | |||
1164 | $query = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '".$this->entity."')"; |
||
1165 | if ($result=$DB->query($query)){ |
||
1166 | if ($DB->numrows($result)){ |
||
1167 | $data=$DB->fetch_assoc($result); |
||
1168 | if (isValidEmail($data["EMAIL"])){ |
||
1169 | return $data["EMAIL"]; |
||
1170 | } |
||
1171 | } |
||
1172 | } |
||
1173 | |||
1174 | return $CFG_GLPI["admin_email"]; |
||
1175 | } |
||
1176 | |||
1177 | |||
1178 | /**
|
||
1179 | * Send mail function
|
||
1180 | *
|
||
1181 | * Construct email and send it
|
||
1182 | *
|
||
1183 | * @return mail subject string
|
||
1184 | */
|
||
1185 | function send() |
||
1186 | { |
||
1187 | global $CFG_GLPI,$LANG; |
||
1188 | if ($CFG_GLPI["mailing"]) |
||
1189 | { |
||
1190 | // get users to send mail
|
||
1191 | $users=$this->get_users_to_send_mail(); |
||
1192 | // get subject OK
|
||
1193 | $subject=$this->get_mail_subject(); |
||
1194 | // get sender : OK
|
||
1195 | $sender= $this->get_mail_sender(); |
||
1196 | // get reply-to address : user->email ou job_email if not set OK
|
||
1197 | $replyto=$sender; |
||
1198 | |||
1199 | |||
1200 | $mmail=new glpi_phpmailer(); |
||
1201 | $mmail->From=$sender; |
||
1202 | $mmail->AddReplyTo("$replyto", ''); |
||
1203 | $mmail->FromName=$sender; |
||
1204 | |||
1205 | $mmail->isHTML(true); |
||
1206 | |||
1207 | // Send all mails
|
||
1208 | |||
1209 | // get subject
|
||
1210 | $bodys=array(); |
||
1211 | $altbodys=array(); |
||
1212 | $subjects=array(); |
||
1213 | // Send all mails
|
||
1214 | if (count($users)){ |
||
1215 | foreach ($users as $email => $lang){ |
||
1216 | if (!isset($subjects[$lang])||!isset($bodys[$lang])||!isset($altbodys[$lang])){ |
||
1217 | loadLanguage($lang);
|
||
1218 | $subjects[$lang]=$this->get_mail_subject(); |
||
1219 | $bodys[$lang]=$this->get_mail_body("html"); |
||
1220 | $altbodys[$lang]=$this->get_mail_body("text"); |
||
1221 | } |
||
1222 | |||
1223 | $mmail->Subject=$subjects[$lang]; |
||
1224 | |||
1225 | $mmail->Body=$bodys[$lang]; |
||
1226 | $mmail->AltBody=$altbodys[$lang]; |
||
1227 | |||
1228 | $mmail->AddAddress($email, ""); |
||
1229 | |||
1230 | if(!$mmail->Send()){ |
||
1231 | addMessageAfterRedirect($LANG["mailing"][47]); |
||
1232 | return false; |
||
1233 | }else{
|
||
1234 | logInFile("mail",$LANG["mailing"][111]." ".$email.": ".$subjects[$lang]."\n"); |
||
1235 | } |
||
1236 | $mmail->ClearAddresses(); |
||
1237 | } |
||
1238 | } else {
|
||
1239 | return false; |
||
1240 | } |
||
1241 | } |
||
1242 | return true; |
||
1243 | } |
||
1244 | } |
||
1245 | |||
1246 | ?> |