source: trunk/include/functions_mail.inc.php @ 1809

Last change on this file since 1809 was 1809, checked in by rub, 17 years ago

When not template are selected for mail, PWG uses default template..

Sent multi-part message in MIME format. (With only one part for the moment).

Improvement pwg_mail function.

File size: 11.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// | Copyright (C) 2006 Ruben ARNAUD - team@phpwebgallery.net              |
7// +-----------------------------------------------------------------------+
8// | branch        : BSF (Best So Far)
9// | file          : $RCSfile$
10// | last update   : $Date: 2005-11-26 21:15:50 +0100 (sam., 26 nov. 2005) $
11// | last modifier : $Author: plg $
12// | revision      : $Revision: 958 $
13// +-----------------------------------------------------------------------+
14// | This program is free software; you can redistribute it and/or modify  |
15// | it under the terms of the GNU General Public License as published by  |
16// | the Free Software Foundation                                          |
17// |                                                                       |
18// | This program is distributed in the hope that it will be useful, but   |
19// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
20// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
21// | General Public License for more details.                              |
22// |                                                                       |
23// | You should have received a copy of the GNU General Public License     |
24// | along with this program; if not, write to the Free Software           |
25// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
26// | USA.                                                                  |
27// +-----------------------------------------------------------------------+
28
29/**
30 * - Extract mail fonctions of password.php
31 * - Modify pwg_mail (add pararameters + news fonctionnalities)
32 * - Var conf_mail, function get_mail_configuration, format_email, pwg_mail
33 */
34
35// +-----------------------------------------------------------------------+
36// |                               functions                               |
37// +-----------------------------------------------------------------------+
38
39/*
40 * Returns an array of mail configuration parameters :
41 *
42 * - mail_options: see $conf['mail_options']
43 * - send_bcc_mail_webmaster: see $conf['send_bcc_mail_webmaster']
44 * - email_webmaster: mail corresponding to $conf['webmaster_id']
45 * - formated_email_webmaster: the name of webmaster is $conf['gallery_title']
46 * - text_footer: PhpWebGallery and version
47 *
48 * @return array
49 */
50function get_mail_configuration()
51{
52  global $conf;
53
54  $conf_mail = array(
55    'mail_options' => $conf['mail_options'],
56    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
57    'default_email_format' => $conf['default_email_format']
58    );
59
60  // we have webmaster id among user list, what's his email address ?
61  $conf_mail['email_webmaster'] = get_webmaster_mail_address();
62
63  // name of the webmaster is the title of the gallery
64  $conf_mail['formated_email_webmaster'] =
65    format_email($conf['gallery_title'], $conf_mail['email_webmaster']);
66
67  $conf_mail['boundary_key'] = generate_key(32);
68
69  return $conf_mail;
70}
71
72/**
73 * Returns an email address with an associated real name
74 *
75 * @param string name
76 * @param string email
77 */
78function format_email($name, $email)
79{
80  global $conf;
81
82  if ($conf['enabled_format_email'])
83  {
84    $cvt7b_name = str_translate_to_ascii7bits($name);
85
86    if (strpos($email, '<') === false)
87    {
88      return $cvt7b_name.' <'.$email.'>';
89    }
90    else
91    {
92      return $cvt7b_name.$email;
93    }
94  }
95  else
96  {
97    return $email;
98  }
99}
100
101/**
102 * Return an completed array template/theme
103 * completed with $conf['default_template']
104 *
105 * @params:
106 *   - args: incompleted array of template/theme
107 *       o template: template to use [default $conf['default_template']]
108 *       o theme: template to use [default $conf['default_template']]
109 */
110function get_array_template_theme($args = array())
111{
112  global $conf;
113
114  $res = array();
115 
116  if (empty($args['template']) or empty($args['theme']))
117  {
118    list($res['template'], $res['theme']) = explode('/', $conf['default_template']);
119  }
120
121  if (!empty($args['template']))
122  {
123    $res['template'] = $args['template'];
124  }
125
126  if (!empty($args['theme']))
127  {
128    $res['theme'] = $args['theme'];
129  }
130
131  return $res;
132}
133
134/**
135 * Return an new mail template
136 *
137 * @params:
138 *   - email_format: mail format
139 *   - args: function params of mail function:
140 *       o template: template to use [default $conf['default_template']]
141 *       o theme: template to use [default $conf['default_template']]
142 */
143function get_mail_template($email_format, $args = array())
144{
145  $args = get_array_template_theme($args);
146
147  $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']);
148  $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format);
149
150  return $mail_template;
151}
152
153/**
154 * Return string email format (html or not)
155 *
156 * @param string format
157 */
158function get_str_email_format($is_html)
159{
160  return ($is_html ? 'text/html' : 'text/plain');
161}
162
163/**
164 * sends an email, using PhpWebGallery specific informations
165 *
166 * @param:
167 *   - to: Receiver, or receivers of the mail.
168 *   - args: function params of mail function:
169 *       o from: sender [default value webmaster email]
170 *       o subject  [default value 'PhpWebGallery']
171 *       o content: content of mail    [default value '']
172 *       o content_format: format of mail content  [default value 'text/plain']
173 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
174 *       o template: template to use [default $conf['default_template']]
175 *       o theme: template to use [default $conf['default_template']]
176 */
177//function pwg_mail($to, $from = '', $subject = 'PhpWebGallery', $infos = '', $infos_format = 'text/plain', $email_format = null)
178function pwg_mail($to, $args = array())
179{
180  global $conf, $conf_mail, $lang_info, $page;
181
182  if (!isset($conf_mail))
183  {
184    $conf_mail = get_mail_configuration();
185  }
186
187  if (empty($args['email_format']))
188  {
189    $args['email_format'] = $conf_mail['default_email_format'];
190  }
191
192  // Compute root_path in order have complete path
193  if ($args['email_format'] == 'text/html')
194  {
195    set_make_full_url();
196  }
197
198  $to = format_email('', $to);
199
200  if (empty($args['from']))
201  {
202    $args['from'] = $conf_mail['formated_email_webmaster'];
203  }
204  else
205  {
206    $args['from'] = format_email('', $args['from']);
207  }
208
209  if (empty($args['subject']))
210  {
211    $args['subject'] = 'PhpWebGallery';
212  }
213  $cvt7b_subject = str_translate_to_ascii7bits($args['subject']);
214
215  if (!isset($args['content']))
216  {
217    $args['content'] = '';
218  }
219
220  if (empty($args['content_format']))
221  {
222    $args['content_format'] = 'text/plain';
223  }
224
225  if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain'))
226  {
227    // Todo find function to convert html text to plain text
228    return false;
229  }
230
231  $args = array_merge($args, get_array_template_theme($args));
232
233  $headers = 'From: '.$args['from']."\n";
234  $headers.= 'Reply-To: '.$args['from']."\n";
235  $headers.= 'Content-Type: multipart/alternative;'."\n";
236  $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'."\n";
237  $headers.= '  reply-type=original'."\n";
238  $headers.= 'MIME-Version: 1.0'."\n";
239
240  if ($conf_mail['send_bcc_mail_webmaster'])
241  {
242    $headers.= 'Bcc: '.$conf_mail['formated_email_webmaster']."\n";
243  }
244
245  $content = '';
246
247  if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']]))
248  {
249    if (!isset($mail_template))
250    {
251      $mail_template = get_mail_template($args['email_format']);
252    }
253
254    $mail_template->set_filename('mail_header', 'header.tpl');
255    $mail_template->set_filename('mail_footer', 'footer.tpl');
256
257    $mail_template->assign_vars(
258      array(
259        //Header
260        'BOUNDARY_KEY' => $conf_mail['boundary_key'],
261        'CONTENT_TYPE' => $args['email_format'],
262        'CONTENT_ENCODING' => $lang_info['charset'],
263        'LANG' => $lang_info['code'],
264        'DIR' => $lang_info['direction'],
265       
266        // Footer
267        'GALLERY_URL' =>
268          isset($page['gallery_url']) ?
269                $page['gallery_url'] : $conf['gallery_url'],
270        'GALLERY_TITLE' =>
271          isset($page['gallery_title']) ?
272                $page['gallery_title'] : $conf['gallery_title'],
273        'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
274        'PHPWG_URL' => PHPWG_URL,
275
276        'TITLE_MAIL' => urlencode(l10n('title_send_mail')),
277        'MAIL' => get_webmaster_mail_address()
278        ));
279
280    if ($args['email_format'] == 'text/html')
281    {
282      $old_root = $mail_template->root;
283
284      if (is_file($mail_template->root.'/global-mail-css.tpl'))
285      {
286        $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl');
287        $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css');
288      }
289
290      $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme'];
291      if (is_file($mail_template->root.'/mail-css.tpl'))
292      {
293        $mail_template->set_filename('mail_css', 'mail-css.tpl');
294        $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css');
295      }
296
297      $mail_template->root = PHPWG_ROOT_PATH.'template-common';
298      if (is_file($mail_template->root.'/local-mail-css.tpl'))
299      {
300        $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl');
301        $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css');
302      }
303
304      $mail_template->root = $old_root;
305    }
306
307    // what are displayed on the header of each mail ?
308    $conf_mail[$args['email_format']]
309      [$lang_info['charset']]
310      [$args['template']][$args['theme']]['header'] =
311        $mail_template->parse('mail_header', true);
312
313    // what are displayed on the footer of each mail ?
314    $conf_mail[$args['email_format']]
315      [$lang_info['charset']]
316      [$args['template']][$args['theme']]['footer'] =
317        $mail_template->parse('mail_footer', true);
318  }
319
320  // Header
321  $content.= $conf_mail[$args['email_format']]
322              [$lang_info['charset']]
323              [$args['template']][$args['theme']]['header'];
324
325  // Content
326  if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html'))
327  {
328    $content.= '<p>'.nl2br(htmlentities($args['content'])).'</p>';
329  }
330  else
331  {
332    $content.= $args['content'];
333  }
334
335  // Footer
336  $content.= $conf_mail[$args['email_format']]
337              [$lang_info['charset']]
338              [$args['template']][$args['theme']]['footer'];
339
340  // Close boundary
341  $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
342
343   // Undo Compute root_path in order have complete path
344  if ($args['email_format'] == 'text/html')
345  {
346    unset_make_full_url();
347  }
348
349  /*Testing block
350  {
351    global $user;
352    @mkdir(PHPWG_ROOT_PATH.'testmail');
353    $filename = PHPWG_ROOT_PATH.'testmail/mail.'.$user['username'];
354    if ($args['content_format'] == 'text/plain')
355    {
356      $filename .= '.txt';
357    }
358    else
359    {
360      $filename .= '.html';
361    }
362    $file = fopen($filename, 'w+');
363    fwrite($file, $content);
364    fclose($file);
365    return true;
366  }*/
367
368  if ($conf_mail['mail_options'])
369  {
370    $options = '-f '.$conf_mail['email_webmaster'];
371   
372    return mail($to, $cvt7b_subject, $content, $headers, $options);
373  }
374  else
375  {
376    return mail($to, $cvt7b_subject, $content, $headers);
377  }
378}
379
380?>
Note: See TracBrowser for help on using the repository browser.