source: subversion/trunk/roundcubemail/program/steps/mail/sendmail.inc @ 140

Last change on this file since 140 was 140, checked in by roundcube, 7 years ago

Applied several patches

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/sendmail.inc                                       |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Compose a new mail message with all headers and attachments         |
13 |   and send it using IlohaMail's SMTP methods or with PHP mail()       |
14 |                                                                       |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17 +-----------------------------------------------------------------------+
18
19 $Id$
20
21*/
22
23
24//require_once('lib/smtp.inc');
25require_once('include/rcube_smtp.inc');
26require_once('Mail/mime.php');
27
28
29if (!isset($_SESSION['compose']['id']))
30  {
31  rcmail_overwrite_action('list');
32  return;
33  }
34
35
36/****** message sending functions ********/
37
38
39
40function rcmail_get_identity($id)
41  {
42  global $DB;
43 
44  // get identity record
45  $sql_result = $DB->query("SELECT *, email AS mailto
46                            FROM ".get_table_name('identities')."
47                            WHERE  identity_id=?
48                            AND    user_id=?
49                            AND    del<>1",
50                            $id,$_SESSION['user_id']);
51                                   
52  if ($DB->num_rows($sql_result))
53    {
54    $sql_arr = $DB->fetch_assoc($sql_result);
55    $out = $sql_arr;
56    $out['string'] = sprintf('%s <%s>', $sql_arr['name'], $sql_arr['mailto']);
57    return $out;
58    }
59
60  return FALSE; 
61  }
62
63
64
65/****** check submission and compose message ********/
66
67
68if (empty($_POST['_to']) && empty($_POST['_subject']) && $_POST['_message'])
69  {
70  show_message("sendingfailed", 'error');
71  rcmail_overwrite_action('compose');
72  return;
73  }
74
75
76// set default charset
77if (empty($CHARSET))
78  $CHARSET = 'ISO-8859-1';
79
80$input_charset = $CHARSET;
81$message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
82
83$mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m');
84$mailto_replace = array(', ', ', ', '');
85
86// repalce new lines and strip ending ', '
87$mailto = preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_to']));
88
89// decode address strings
90$to_address_arr = $IMAP->decode_address_list($mailto);
91$identity_arr = rcmail_get_identity($_POST['_from']);
92
93
94$from = $identity_arr['mailto'];
95$first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto;
96
97
98// create unique message-id
99$message_id = sprintf('<%s@%s>', md5(uniqid('rcmail')), $_SESSION['imap_host']);
100
101
102// compose headers array
103$headers = array('Date' => date('D, j M Y G:i:s O'),
104                 'From' => $identity_arr['string'],
105                 'To'   => rcube_charset_convert($mailto, $input_charset, $message_charset));
106
107// additional recipients
108if ($_POST['_cc'])
109  $headers['Cc'] = rcube_charset_convert(preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_cc'])), $input_charset, $message_charset);
110
111if ($_POST['_bcc'])
112  $headers['Bcc'] = rcube_charset_convert(preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_bcc'])), $input_charset, $message_charset);
113 
114if (strlen($identity_arr['bcc']))
115  $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
116
117// add subject
118$headers['Subject'] = rcube_charset_convert(trim($_POST['_subject']), $input_charset, $message_charset);
119
120if (strlen($identity_arr['organization']))
121  $headers['Organization'] = $identity_arr['organization'];
122
123if (strlen($identity_arr['reply-to']))
124  $headers['Reply-To'] = $identity_arr['reply-to'];
125
126if (!empty($_SESSION['compose']['reply_msgid']))
127  $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
128
129if (!empty($_SESSION['compose']['references']))
130  $headers['References'] = $_SESSION['compose']['references'];
131
132if ($_POST['_priority'])
133  {
134  $priority = (int)$_POST['_priority'];
135  $a_priorities = array(1=>'lowest', 2=>'low', 4=>'high', 5=>'highest');
136  if ($str_priority = $a_priorities[$priority])
137    $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
138  }
139
140
141// additional headers
142$headers['Message-ID'] = $message_id;
143$headers['X-Sender'] = $from;
144
145if ($CONFIG['useragent'])
146  $headers['User-Agent'] = $CONFIG['useragent'];
147
148// fetch message body
149$message_body = rcube_charset_convert($_POST['_message'], $input_charset, $message_charset);
150
151// append generic footer to all messages
152if (!empty($CONFIG['generic_message_footer']))
153  {
154  $file = realpath($CONFIG['generic_message_footer']);
155  if($fp = fopen($file, 'r'))
156    {
157    $content = fread($fp, filesize($file));
158    fclose($fp);
159    $message_body .= "\r\n" . rcube_charset_convert($content, 'UTF-8', $message_charset);
160    }
161  }
162
163
164// use the configured delimiter for headers
165$header_delm = $rcmail_config['mail_header_delimiter'] ? $rcmail_config['mail_header_delimiter'] : "\r\n";
166
167// create PEAR::Mail_mime instance
168$MAIL_MIME = new Mail_mime($header_delm);
169$MAIL_MIME->setTXTBody($message_body, FALSE, TRUE);
170//$MAIL_MIME->setTXTBody(wordwrap($message_body), FALSE, TRUE);
171
172
173// add stored attachments, if any
174if (is_array($_SESSION['compose']['attachments']))
175  foreach ($_SESSION['compose']['attachments'] as $attachment)
176    $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE);
177
178 
179// add submitted attachments
180if (is_array($_FILES['_attachments']['tmp_name']))
181  foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath)
182    $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE);
183
184
185// chose transfer encoding
186$charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15');
187$transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit';
188
189// encoding settings for mail composing
190$message_param = array('text_encoding' => $transfer_encoding,
191                       'html_encoding' => 'quoted-printable',
192                       'head_encoding' => 'quoted-printable',
193                       'head_charset'  => $message_charset,
194                       'html_charset'  => $message_charset,
195                       'text_charset'  => $message_charset);
196
197// compose message body and get headers
198$msg_body = $MAIL_MIME->get($message_param);
199
200$msg_subject = $headers['Subject'];
201global $MBSTRING;
202if ($MBSTRING&&function_exists( "mb_encode_mimeheader"))
203  $headers['Subject'] = mb_encode_mimeheader( $headers['Subject'],$message_charset);
204
205// send thru SMTP server using cusotm SMTP library
206if ($CONFIG['smtp_server'])
207  {
208  // generate list of recipients
209  $a_recipients = array($mailto);
210
211  if (strlen($headers['Cc']))
212    $a_recipients[] = $headers['Cc'];
213  if (strlen($headers['Bcc']))
214    $a_recipients[] = $headers['Bcc'];
215
216  // clean Bcc from header for recipients
217  $send_headers = $headers;
218  unset($send_headers['Bcc']);
219
220  // generate message headers
221  $header_str = $MAIL_MIME->txtHeaders($send_headers);
222
223  // send message
224  $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body);
225
226  // log error
227  if (!$sent)
228    {
229    raise_error(array('code' => 800,
230                      'type' => 'smtp',
231                      'line' => __LINE__,
232                      'file' => __FILE__,
233                      'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE);
234    }
235  }
236
237// send mail using PHP's mail() function
238else
239  {
240  // unset some headers because they will be added by the mail() function
241  $headers_php = $MAIL_MIME->_headers;
242  $headers_enc = $MAIL_MIME->headers($headers);
243  unset($headers_php['To'], $headers_php['Subject']);
244
245  // reset stored headers and overwrite
246  $MAIL_MIME->_headers = array();
247  $header_str = $MAIL_MIME->txtHeaders($headers_php);
248
249  if(ini_get('safe_mode'))
250    $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
251  else
252    $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
253  }
254
255
256// return to compose page if sending failed
257if (!$sent)
258  {
259  show_message("sendingfailed", 'error');
260  rcmail_overwrite_action('compose');
261  return;
262  }
263
264
265// set repliead flag
266if ($_SESSION['compose']['reply_uid'])
267  $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED');
268
269
270// copy message to sent folder
271if ($CONFIG['sent_mbox'])
272  {
273  // create string of complete message headers
274  $header_str = $MAIL_MIME->txtHeaders($headers);
275
276  // check if mailbox exists
277  if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes()))
278    $mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
279  else
280    $mbox = TRUE;
281
282  // append message to sent box
283  if ($mbox)
284    $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
285
286  // raise error if saving failed
287  if (!$saved)
288    raise_error(array('code' => 800,
289                      'type' => 'imap',
290                      'file' => __FILE__,
291                      'message' => "Could not save message in $CONFIG[sent_mbox]"), TRUE, FALSE);
292  }
293
294
295// log mail sending
296if ($CONFIG['smtp_log'])
297  {
298  $log_entry = sprintf("[%s] User: %d on %s; Message for %s; Subject: %s\n",
299               date("d-M-Y H:i:s O", mktime()),
300               $_SESSION['user_id'],
301               $_SERVER['REMOTE_ADDR'],
302               $mailto,
303               $msg_subject);
304
305  if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a'))
306    {
307    fwrite($fp, $log_entry);
308    fclose($fp);
309    }
310  }
311
312
313// show confirmation
314show_message('messagesent', 'confirmation');
315
316
317// kill compose entry from session
318rcmail_compose_cleanup();
319
320?>
Note: See TracBrowser for help on using the repository browser.