2b66a8e08f0f090453a1a76a8a32f18760ad67e6
[platal.git] / modules / xnetgrp / mail.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ get_all_redirects
23
24 function get_all_redirects($membres, $mls, &$client)
25 {
26 global $globals;
27
28 $tos = array();
29
30 // TODO: add more filters to choose users
31 if (!empty($membres)) {
32 $uf = new UserFilter(new UFC_Group($globals->asso('id')))
33 $tos = $uf->getUsers();
34 }
35
36 foreach ($mls as $ml) {
37 if (list(,$members) = $client->get_members($ml)) {
38 foreach ($members as $mem) {
39 $uf = new UserFilter(new UFC_Mail($mem[1]));
40 $user = $uf->getUsers();
41 if ($user) {
42 $tos[] = $user;
43 } else {
44 $tos[] = $mem[1];
45 }
46 }
47 }
48 }
49
50 return $tos;
51 }
52
53 // }}}
54 // {{{ _send_xnet_mail
55
56 function _send_xnet_mail($user, $body, $wiki, $mailer, $replyto = null)
57 {
58 if ($user instanceof PlUser) {
59 $cher = $user->isFemale() ? 'Chère' : 'Cher';
60 $nom = $user->displayName();
61 $pnom = '';
62 } else {
63 $cher = 'Cher(e)';
64 $nom = $user;
65 $pnom = '';
66 }
67
68 $text = $body;
69 $text = preg_replace('!<cher>!i', $cher, $text);
70 $text = preg_replace('!<nom>!i', $nom, $text);
71 $text = preg_replace('!<prenom>!i', $pnom, $text);
72
73 $mailer->addTo($user);
74 if ($replyto) {
75 $mailer->addHeader('Reply-To', $replyto);
76 }
77 if ($wiki) {
78 $mailer->setWikiBody($text);
79 } else {
80 $mailer->setTxtBody(wordwrap($text, 72));
81 }
82 $mailer->send();
83 }
84
85 // }}}
86 // {{{ send_xnet_mails
87
88 function send_xnet_mails($from, $sujet, $body, $wiki, $tos, $replyto = null, $upload = null, $name = null)
89 {
90 global $globals;
91 $sent = array();
92
93 $mailer = new PlMailer();
94 $mailer->setSubject($sujet);
95 $mailer->setFrom($from);
96 if ($upload && $upload->exists()) {
97 $mailer->addUploadAttachment($upload, $name);
98 }
99
100 foreach ($tos as $user) {
101 if ($user instanceof $user) {
102 $email = $user->bestEmail();
103 } else {
104 $email = $user;
105 }
106 if ($sent[$email]) {
107 continue;
108 }
109 _send_xnet_mail($user, $body, $wiki, $mailer, $replyto);
110 $sent[$email] = true;
111 }
112 }
113
114 // }}}
115
116 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
117 ?>