Merge branch 'xorg/maint'
[platal.git] / modules / xnetgrp / mail.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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 // $list_members is a (list_details, members, list_owners) array, where
38 // members is an array of (0 => name, 1 => email) arrays.
39 $list_members = $client->get_members($ml);
40 if ($list_members) {
41 foreach ($list_members[1] as $mem) {
42 $uf = new UserFilter(new UFC_Email($mem[1]));
43 $user = $uf->getUser();
44 if ($user) {
45 $tos[] = $user;
46 } else {
47 $tos[] = $mem[1];
48 }
49 }
50 }
51 }
52
53 return $tos;
54 }
55
56 // }}}
57 // {{{ _send_xnet_mail
58
59 function _send_xnet_mail($user, $body, $wiki, $mailer, $replyto = null)
60 {
61 if ($user instanceof PlUser) {
62 $dear = $user->isFemale() ? 'Chère' : 'Cher';
63 $lastname = $user->lastName();
64 $firstname = $user->firstName();
65 } else {
66 $dear = 'Cher(e)';
67 $lastname = '';
68 $firstname = $user;
69 }
70
71 global $globals;
72 $grp_name = $globals->asso('nom');
73 $grp_short = $globals->asso('diminutif');
74
75 $text = str_ireplace(array('<cher>', '<nom>', '<prenom>'),
76 array($dear, $lastname, $firstname), $body);
77 if ($wiki) {
78 $text .= (
79 "\n" .
80 "---- \n" .
81 "Message envoyé pour le groupe [[http://www.polytechnique.net/${grp_short}|${grp_name}]] via les outils de Polytechnique.org \\\\\n" .
82 "[[http://www.polytechnique.net/${grp_short}/annuaire|Annuaire]] | [[http://www.polytechnique.net/${grp_short}/unsubscribe|Se désinscrire]]"
83 );
84 } else {
85 $text .= (
86 "\n" .
87 "---- \n" .
88 "Message envoyé pour le groupe ${grp_name} via les outils de Polytechnique.org\n" .
89 "Annuaire : http://www.polytechnique.net/${grp_short}/annuaire | Désinscription : http://www.polytechnique.net/${grp_short}/unsubscribe"
90 );
91 }
92
93 $mailer->setTo($user);
94 if ($replyto) {
95 $mailer->addHeader('Reply-To', $replyto);
96 }
97 if ($wiki) {
98 $mailer->setWikiBody($text);
99 } else {
100 $mailer->setTxtBody(wordwrap($text, 72));
101 }
102 $mailer->send();
103 }
104
105 // }}}
106 // {{{ send_xnet_mails
107
108 function send_xnet_mails($from, $sujet, $body, $wiki, $tos, $replyto = null, $upload = null, $name = null)
109 {
110 global $globals;
111 $sent = array();
112
113 $mailer = new PlMailer();
114 $mailer->setSubject($sujet);
115 $mailer->setFrom($from);
116 if ($upload && $upload->exists()) {
117 $mailer->addUploadAttachment($upload, $name);
118 }
119
120 foreach ($tos as $user) {
121 if ($user instanceof User) {
122 $email = $user->bestEmail();
123 } else {
124 $email = $user;
125 }
126 if (!$email) {
127 continue;
128 }
129
130 if (!isset($sent[$email])) {
131 _send_xnet_mail($user, $body, $wiki, $mailer, $replyto);
132 $sent[$email] = true;
133 }
134 }
135 }
136
137 // }}}
138
139 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
140 ?>