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

Last change on this file since 1685 was 1676, checked in by rub, 18 years ago

Apply Nicco's style footer to HTML mail.
Add 2 news functions to indicate to build URL with full path

File size: 7.8 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-2006 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  // what to display at the bottom of each mail ?
68  $conf_mail['text_footer'] =
69    "\n\n-- \nPhpWebGallery ".($conf['show_version'] ? PHPWG_VERSION : '');
70 
71  return $conf_mail;
72}
73
74/**
75 * Returns an email address with an associated real name
76 *
77 * @param string name
78 * @param string email
79 */
80function format_email($name, $email)
81{
82  global $conf;
83
84  if ($conf['enabled_format_email'])
85  {
86    $cvt7b_name = str_translate_to_ascii7bits($name);
87
88    if (strpos($email, '<') === false)
89    {
90      return $cvt7b_name.' <'.$email.'>';
91    }
92    else
93    {
94      return $cvt7b_name.$email;
95    }
96  }
97  else
98  {
99    return $email;
100  }
101}
102
103/**
104 * Returns an new mail template
105 *
106 * @param none
107 */
108function get_mail_template()
109{
110  global $conf;
111
112  // for mail, default template are used
113  list($tmpl, $thm) = explode('/', $conf['default_template']);
114  $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$tmpl, $thm);
115
116  return $mail_template;
117}
118
119/**
120 * sends an email, using PhpWebGallery specific informations
121 */
122function pwg_mail($to, $from = '', $subject = 'PhpWebGallery', $infos = '', $format_infos = 'text/plain', $email_format = null)
123{
124  global $conf, $conf_mail, $lang_info, $user, $page;
125
126  $cvt7b_subject = str_translate_to_ascii7bits($subject);
127
128  if (!isset($conf_mail))
129  {
130    $conf_mail = get_mail_configuration();
131  }
132
133  if (is_null($email_format))
134  {
135    $email_format = $conf_mail['default_email_format'];
136  }
137
138  if (($format_infos == 'text/html') and ($email_format == 'text/plain'))
139  {
140    // Todo find function to convert html text to plain text
141    return false;
142  }
143
144  // Compute root_path in order have complete path
145  if ($email_format == 'text/html')
146  {
147    set_make_full_url();
148  }
149
150  $to = format_email('', $to);
151
152  if ($from == '')
153  {
154    $from = $conf_mail['formated_email_webmaster'];
155  }
156  else
157  {
158    $from = format_email('', $from);
159  }
160
161  $headers = 'From: '.$from."\n";
162  $headers.= 'Reply-To: '.$from."\n";
163  $headers.= 'Content-Type: '.$email_format.';format=flowed;charset="'.$lang_info['charset'].'";';
164  $headers.= 'reply-type=original'."\n";
165
166  if ($conf_mail['send_bcc_mail_webmaster'])
167  {
168    $headers.= 'Bcc: '.$conf_mail['formated_email_webmaster']."\n";
169  }
170
171  $content = '';
172
173  if (!isset($conf_mail[$email_format][$lang_info['charset']]['header']))
174  {
175    if ($email_format == 'text/html')
176    {
177      $mail_template = get_mail_template();
178
179      $mail_template->set_filenames(array('mail_header'=>'mail/header.tpl'));
180
181      $mail_template->assign_vars(
182        array(
183          'BODY_ID' =>
184            isset($page['body_id']) ?
185              $page['body_id'] : '',
186
187          'CONTENT_ENCODING' => $lang_info['charset'],
188          'LANG' => $lang_info['code'],
189          'DIR' => $lang_info['direction']
190          ));
191
192      $conf_mail[$email_format][$lang_info['charset']]['header'] =
193        $mail_template->parse('mail_header', true);
194    }
195    else
196    {
197      $conf_mail[$email_format][$lang_info['charset']]['header'] = '';
198    }
199  }
200
201  $content.= $conf_mail[$email_format][$lang_info['charset']]['header'];
202
203  if (($format_infos == 'text/plain') and ($email_format == 'text/html'))
204  {
205    $content.= '<pre>'.htmlentities($infos).'</pre>';
206  }
207  else
208  {
209    $content.= $infos;
210  }
211
212  if (!isset($conf_mail[$email_format][$lang_info['charset']]['footer']))
213  {
214    if ($email_format == 'text/html')
215    {
216      $mail_template->set_filenames(array('mail_footer'=>'mail/footer.tpl'));
217
218      $mail_template->assign_vars(
219        array(
220          'GALLERY_URL' =>
221            isset($page['gallery_url']) ?
222                  $page['gallery_url'] : $conf['gallery_url'],
223          'GALLERY_TITLE' =>
224            isset($page['gallery_title']) ?
225                  $page['gallery_title'] : $conf['gallery_title'],
226          'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
227
228          'TITLE_MAIL' => urlencode(l10n('title_send_mail')),
229          'MAIL' => get_webmaster_mail_address()
230          ));
231
232      $conf_mail[$email_format][$lang_info['charset']]['footer'] =
233        $mail_template->parse('mail_footer', true);
234    }
235    else
236    {
237      $conf_mail[$email_format][$lang_info['charset']]['footer'] = $conf_mail['text_footer'];
238    }
239  }
240
241  $content.= $conf_mail[$email_format][$lang_info['charset']]['footer'];
242 
243   // Undo Compute root_path in order have complete path
244  if ($email_format == 'text/html')
245  {
246    unset_make_full_url();
247  }
248
249  if ($conf_mail['mail_options'])
250  {
251    $options = '-f '.$conf_mail['email_webmaster'];
252   
253    return mail($to, $cvt7b_subject, $content, $headers, $options);
254  }
255  else
256  {
257    return mail($to, $cvt7b_subject, $content, $headers);
258  }
259}
260
261?>
Note: See TracBrowser for help on using the repository browser.