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