Proof of concept:
[platal.git] / include / xorg.mailer.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 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
22require_once('diogenes/diogenes.hermes.inc.php');
23require_once('Smarty.class.php');
24
25// {{{ class XOrgMailer
26
27/** Classe de mail avec corps en templates.
28 */
29class XOrgMailer extends Smarty
30{
31 // {{{ properties
32
33 /** Directory used to store mails_templates.
34 * Smarty::template_dir subdir used to sotre the mails templates.
35 * The body of the message is taken from a tsmarty template
36 */
37 var $mail_dir = "mails";
38 /** stores the mail template name */
39 var $_tpl;
40
41 /** stores the mail From: header */
42 var $_from;
43 /** stores the recipients of the mail */
44 var $_to = Array();
45 /** stores the Cc recipients of the mail */
46 var $_cc = Array();
47 /** stores the Bcc recipients of the mail */
48 var $_bcc = Array();
49 /** stores the subject of the mail */
50 var $_subject;
51
52 // }}}
53 // {{{ constructor
54
55 function XorgMailer($tpl)
56 {
57 global $globals;
58 $this->_tpl = $tpl;
59 $this->caching=false;
60 $this->compile_check=true;
61
62 $this->template_dir = $globals->root . "/templates/";
796aea34 63 $this->compile_dir = $globals->root . "/spool/templates_c/";
0337d704 64 $this->config_dir = $globals->root . "/configs/";
65
66 $this->register_outputfilter('mail_format');
67 $this->register_function('from', 'set_from');
68 $this->register_function('to', 'set_to');
69 $this->register_function('cc', 'set_cc');
70 $this->register_function('bcc', 'set_bcc');
71 $this->register_function('subject', 'set_subject');
72 }
73
74 // }}}
75 // {{{ function send()
76
77 function send()
78 {
79 // do not try to optimize, in the templates, some function can modify our object, then we
80 // have to fetch in the first time, and only then send the mail.
81 $body = $this->fetch($this->mail_dir."/".$this->_tpl);
82 $mailer = new HermesMailer();
83 $mailer->setFrom($this->_from);
84 $mailer->addTo(implode(',',$this->_to));
85 $mailer->setSubject($this->_subject);
86 if (!empty($this->_cc)) {
87 $mailer->addCc(implode(',',$this->_cc));
88 }
89 if (!empty($this->_bcc)) {
90 $mailer->addBcc(implode(',',$this->_bcc));
91 }
92 $mailer->setTxtBody($body);
93 $mailer->send();
94 }
95
96 // }}}
97}
98
99// }}}
100// {{{ function mail_format()
101
102/** used to remove the empty lines due to {from ...}, {to ...} ... functions */
103function mail_format($output, &$smarty)
104{
105 return wordwrap("\n".trim($output)."\n",75);
106}
107
108// }}}
109// {{{ function format_addr()
110
111function format_addr(&$params)
112{
113 if (isset($params['full'])) {
114 return $params['full'];
115 } elseif (empty($params['text'])) {
116 return $params['addr'];
117 } else {
118 return $params['text'].' <'.$params['addr'].'>';
119 }
120}
121
122// }}}
123// {{{ function set_from()
124
125/** template function : from.
126 * {from full=...} for an already formatted address
127 * {from addr=... [text=...]} else
128 */
129function set_from($params, &$smarty)
130{ $smarty->_from = format_addr($params); }
131
132// }}}
133// {{{ function set_to()
134
135/** template function : to.
136 * {to full=...} for an already formatted address
137 * {to addr=... [text=...]} else
138 */
139function set_to($params, &$smarty)
140{ $smarty->_to[] = format_addr($params); }
141
142// }}}
143// {{{ function set_cc()
144
145/** template function : cc.
146 * {cc full=...} for an already formatted address
147 * {cc addr=... [text=...]} else
148 */
149function set_cc($params, &$smarty)
150{ $smarty->_cc[] = format_addr($params); }
151
152// }}}
153// {{{ function set_bcc()
154
155/** template function : bcc.
156 * {bcc full=...} for an already formatted address
157 * {bcc addr=... [text=...]} else
158 */
159function set_bcc($params, &$smarty)
160{ $smarty->_bcc[] = format_addr($params); }
161
162// }}}
163// {{{ function set_subject()
164
165/** template function : subject.
166 * {subject text=...}
167 */
168function set_subject($params, &$smarty)
169{
170 $smarty->_subject = $params['text'];
171}
172
173// }}}
174
175// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
176?>