source: tags/release-1_7_0RC1/include/functions_mail.inc.php @ 4383

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

My last improvements before 1.7.0RC1.

Can include Cc & Bcc on mail.
Send mail to all administrators on new comment or new users.
Add validate link on new comment mail.
Try to detect if the NBM complementary content is HTML or plain text. With plain text, this content is convert to readable HTML.

File size: 12.4 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-2007 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// |                               functions                               |
31// +-----------------------------------------------------------------------+
32
33/*
34 * Returns an array of mail configuration parameters :
35 *
36 * - mail_options: see $conf['mail_options']
37 * - send_bcc_mail_webmaster: see $conf['send_bcc_mail_webmaster']
38 * - email_webmaster: mail corresponding to $conf['webmaster_id']
39 * - formated_email_webmaster: the name of webmaster is $conf['gallery_title']
40 * - text_footer: PhpWebGallery and version
41 *
42 * @return array
43 */
44function get_mail_configuration()
45{
46  global $conf;
47
48  $conf_mail = array(
49    'mail_options' => $conf['mail_options'],
50    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
51    'default_email_format' => $conf['default_email_format']
52    );
53
54  // we have webmaster id among user list, what's his email address ?
55  $conf_mail['email_webmaster'] = get_webmaster_mail_address();
56
57  // name of the webmaster is the title of the gallery
58  $conf_mail['formated_email_webmaster'] =
59    format_email($conf['gallery_title'], $conf_mail['email_webmaster']);
60
61  $conf_mail['boundary_key'] = generate_key(32);
62
63  return $conf_mail;
64}
65
66/**
67 * Returns an email address with an associated real name
68 *
69 * @param string name
70 * @param string email
71 */
72function format_email($name, $email)
73{
74  global $conf;
75
76  if ($conf['enabled_format_email'])
77  {
78    $cvt7b_name = str_translate_to_ascii7bits($name);
79
80    if (strpos($email, '<') === false)
81    {
82      return $cvt7b_name.' <'.$email.'>';
83    }
84    else
85    {
86      return $cvt7b_name.$email;
87    }
88  }
89  else
90  {
91    return $email;
92  }
93}
94
95/**
96 * Returns an completed array template/theme
97 * completed with $conf['default_template']
98 *
99 * @params:
100 *   - args: incompleted array of template/theme
101 *       o template: template to use [default $conf['default_template']]
102 *       o theme: template to use [default $conf['default_template']]
103 */
104function get_array_template_theme($args = array())
105{
106  global $conf;
107
108  $res = array();
109 
110  if (empty($args['template']) or empty($args['theme']))
111  {
112    list($res['template'], $res['theme']) = explode('/', $conf['default_template']);
113  }
114
115  if (!empty($args['template']))
116  {
117    $res['template'] = $args['template'];
118  }
119
120  if (!empty($args['theme']))
121  {
122    $res['theme'] = $args['theme'];
123  }
124
125  return $res;
126}
127
128/**
129 * Return an new mail template
130 *
131 * @params:
132 *   - email_format: mail format
133 *   - args: function params of mail function:
134 *       o template: template to use [default $conf['default_template']]
135 *       o theme: template to use [default $conf['default_template']]
136 */
137function get_mail_template($email_format, $args = array())
138{
139  $args = get_array_template_theme($args);
140
141  $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']);
142  $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format);
143
144  return $mail_template;
145}
146
147/**
148 * Return string email format (html or not)
149 *
150 * @param string format
151 */
152function get_str_email_format($is_html)
153{
154  return ($is_html ? 'text/html' : 'text/plain');
155}
156
157/**
158 * Returns email of all administrator
159 *
160 * @return string
161 */
162function get_administrators_email()
163{
164  global $conf;
165
166  $result = array();
167
168  $query = '
169select
170  U.'.$conf['user_fields']['username'].' as username,
171  U.'.$conf['user_fields']['email'].' as mail_address
172from
173  '.USERS_TABLE.' as U,
174  '.USER_INFOS_TABLE.' as I
175where
176  I.user_id =  U.'.$conf['user_fields']['id'].' and
177  I.status in (\'webmaster\',  \'admin\') and
178  '.$conf['user_fields']['email'].' is not null
179order by
180  username
181';
182
183  $datas = pwg_query($query);
184  if (!empty($datas))
185  {
186    while ($admin = mysql_fetch_array($datas))
187    {
188      if (!empty($admin['mail_address']))
189      {
190        array_push($result, format_email($admin['username'], $admin['mail_address']));
191      }
192    }
193  }
194
195  return $result;
196}
197
198/**
199 * sends an email, using PhpWebGallery specific informations
200 *
201 * @param:
202 *   - to: Receiver, or receivers of the mail.
203 *   - args: function params of mail function:
204 *       o from: sender [default value webmaster email]
205 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
206 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
207 *       o subject  [default value 'PhpWebGallery']
208 *       o content: content of mail    [default value '']
209 *       o content_format: format of mail content  [default value 'text/plain']
210 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
211 *       o template: template to use [default $conf['default_template']]
212 *       o theme: template to use [default $conf['default_template']]
213 */
214function pwg_mail($to, $args = array())
215{
216  global $conf, $conf_mail, $lang_info, $page;
217
218  if (!isset($conf_mail))
219  {
220    $conf_mail = get_mail_configuration();
221  }
222
223  if (empty($args['email_format']))
224  {
225    $args['email_format'] = $conf_mail['default_email_format'];
226  }
227
228  // Compute root_path in order have complete path
229  if ($args['email_format'] == 'text/html')
230  {
231    set_make_full_url();
232  }
233
234  $to = format_email('', $to);
235
236  if (empty($args['from']))
237  {
238    $args['from'] = $conf_mail['formated_email_webmaster'];
239  }
240  else
241  {
242    $args['from'] = format_email('', $args['from']);
243  }
244
245  if (empty($args['subject']))
246  {
247    $args['subject'] = 'PhpWebGallery';
248  }
249  $cvt7b_subject = str_translate_to_ascii7bits($args['subject']);
250
251  if (!isset($args['content']))
252  {
253    $args['content'] = '';
254  }
255
256  if (empty($args['content_format']))
257  {
258    $args['content_format'] = 'text/plain';
259  }
260
261  if ($conf_mail['send_bcc_mail_webmaster'])
262  {
263    $args['Bcc'][] = $conf_mail['formated_email_webmaster'];
264  }
265
266  if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain'))
267  {
268    // Todo find function to convert html text to plain text
269    return false;
270  }
271
272  $args = array_merge($args, get_array_template_theme($args));
273
274  $headers = 'From: '.$args['from']."\n";
275  $headers.= 'Reply-To: '.$args['from']."\n";
276
277  if (!empty($args['Cc']))
278  {
279    $headers.= 'Cc: '.implode(',', $args['Cc'])."\n";
280  }
281
282  if (!empty($args['Bcc']))
283  {
284    $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n";
285  }
286
287  $headers.= 'Content-Type: multipart/alternative;'."\n";
288  $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'."\n";
289  $headers.= '  reply-type=original'."\n";
290  $headers.= 'MIME-Version: 1.0'."\n";
291
292  $content = '';
293
294  if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']]))
295  {
296    if (!isset($mail_template))
297    {
298      $mail_template = get_mail_template($args['email_format']);
299    }
300
301    $mail_template->set_filename('mail_header', 'header.tpl');
302    $mail_template->set_filename('mail_footer', 'footer.tpl');
303
304    $mail_template->assign_vars(
305      array(
306        //Header
307        'BOUNDARY_KEY' => $conf_mail['boundary_key'],
308        'CONTENT_TYPE' => $args['email_format'],
309        'CONTENT_ENCODING' => $lang_info['charset'],
310        'LANG' => $lang_info['code'],
311        'DIR' => $lang_info['direction'],
312       
313        // Footer
314        'GALLERY_URL' =>
315          isset($page['gallery_url']) ?
316                $page['gallery_url'] : $conf['gallery_url'],
317        'GALLERY_TITLE' =>
318          isset($page['gallery_title']) ?
319                $page['gallery_title'] : $conf['gallery_title'],
320        'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
321        'PHPWG_URL' => PHPWG_URL,
322
323        'TITLE_MAIL' => urlencode(l10n('title_send_mail')),
324        'MAIL' => get_webmaster_mail_address()
325        ));
326
327    if ($args['email_format'] == 'text/html')
328    {
329      $old_root = $mail_template->root;
330
331      if (is_file($mail_template->root.'/global-mail-css.tpl'))
332      {
333        $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl');
334        $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css');
335      }
336
337      $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme'];
338      if (is_file($mail_template->root.'/mail-css.tpl'))
339      {
340        $mail_template->set_filename('mail_css', 'mail-css.tpl');
341        $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css');
342      }
343
344      $mail_template->root = PHPWG_ROOT_PATH.'template-common';
345      if (is_file($mail_template->root.'/local-mail-css.tpl'))
346      {
347        $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl');
348        $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css');
349      }
350
351      $mail_template->root = $old_root;
352    }
353
354    // what are displayed on the header of each mail ?
355    $conf_mail[$args['email_format']]
356      [$lang_info['charset']]
357      [$args['template']][$args['theme']]['header'] =
358        $mail_template->parse('mail_header', true);
359
360    // what are displayed on the footer of each mail ?
361    $conf_mail[$args['email_format']]
362      [$lang_info['charset']]
363      [$args['template']][$args['theme']]['footer'] =
364        $mail_template->parse('mail_footer', true);
365  }
366
367  // Header
368  $content.= $conf_mail[$args['email_format']]
369              [$lang_info['charset']]
370              [$args['template']][$args['theme']]['header'];
371
372  // Content
373  if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html'))
374  {
375    $content.= '<p>'.nl2br(htmlentities($args['content'])).'</p>';
376  }
377  else
378  {
379    $content.= $args['content'];
380  }
381
382  // Footer
383  $content.= $conf_mail[$args['email_format']]
384              [$lang_info['charset']]
385              [$args['template']][$args['theme']]['footer'];
386
387  // Close boundary
388  $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
389
390   // Undo Compute root_path in order have complete path
391  if ($args['email_format'] == 'text/html')
392  {
393    unset_make_full_url();
394  }
395
396  /*Testing block
397  {
398    global $user;
399    @mkdir(PHPWG_ROOT_PATH.'testmail');
400    $filename = PHPWG_ROOT_PATH.'testmail/mail.'.$user['username'];
401    if ($args['content_format'] == 'text/plain')
402    {
403      $filename .= '.txt';
404    }
405    else
406    {
407      $filename .= '.html';
408    }
409    $file = fopen($filename, 'w+');
410    fwrite($file, $content);
411    fclose($file);
412    return true;
413  }*/
414
415  if ($conf_mail['mail_options'])
416  {
417    $options = '-f '.$conf_mail['email_webmaster'];
418   
419    return mail($to, $cvt7b_subject, $content, $headers, $options);
420  }
421  else
422  {
423    return mail($to, $cvt7b_subject, $content, $headers);
424  }
425}
426
427?>
Note: See TracBrowser for help on using the repository browser.