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

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

Added patches for BCC headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 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 ($_SESSION['compose']['reply_msgid'])
127  $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
128
129
130if ($_POST['_priority'])
131  {
132  $priority = (int)$_POST['_priority'];
133  $a_priorities = array(1=>'lowest', 2=>'low', 4=>'high', 5=>'highest');
134  if ($str_priority = $a_priorities[$priority])
135    $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
136  }
137
138
139// additional headers
140$headers['Message-ID'] = $message_id;
141$headers['X-Sender'] = $from;
142
143if ($CONFIG['useragent'])
144  $headers['User-Agent'] = $CONFIG['useragent'];
145
146// fetch message body
147$message_body = rcube_charset_convert($_POST['_message'], $input_charset, $message_charset);
148
149// append generic footer to all messages
150if (!empty($CONFIG['generic_message_footer']))
151  {
152  $file = realpath($CONFIG['generic_message_footer']);
153  if($fp = fopen($file, 'r'))
154    {
155    $content = fread($fp, filesize($file));
156    fclose($fp);
157    $message_body .= "\r\n" . rcube_charset_convert($content, 'UTF-8', $message_charset);
158    }
159  }
160
161
162// use the configured delimiter for headers
163$header_delm = $rcmail_config['mail_header_delimiter'] ? $rcmail_config['mail_header_delimiter'] : "\r\n";
164
165// create PEAR::Mail_mime instance
166$MAIL_MIME = new Mail_mime($header_delm);
167$MAIL_MIME->setTXTBody($message_body, FALSE, TRUE);
168//$MAIL_MIME->setTXTBody(wordwrap($message_body), FALSE, TRUE);
169
170
171// add stored attachments, if any
172if (is_array($_SESSION['compose']['attachments']))
173  foreach ($_SESSION['compose']['attachments'] as $attachment)
174    $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE);
175
176 
177// add submitted attachments
178if (is_array($_FILES['_attachments']['tmp_name']))
179  foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath)
180    $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE);
181
182
183// encoding settings for mail composing
184$message_param = array('text_encoding' => '8bit',
185                       'html_encoding' => 'quoted-printable',
186                       'head_encoding' => 'quoted-printable',
187                       'head_charset'  => $message_charset,
188                       'html_charset'  => $message_charset,
189                       'text_charset'  => $message_charset);
190
191// compose message body and get headers
192$msg_body = $MAIL_MIME->get($message_param);
193$msg_subject = $headers['Subject'];
194
195
196// send thru SMTP server using cusotm SMTP library
197if ($CONFIG['smtp_server'])
198  {
199  // generate list of recipients
200  $a_recipients = array($mailto);
201
202  if (strlen($headers['Cc']))
203    $a_recipients[] = $headers['Cc'];
204  if (strlen($headers['Bcc']))
205    $a_recipients[] = $headers['Bcc'];
206
207  // clean Bcc from header for recipients
208  $send_headers = $headers;
209  unset($send_headers['Bcc']);
210
211  // generate message headers
212  $header_str = $MAIL_MIME->txtHeaders($send_headers);
213
214  // send message
215  $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body);
216
217  // log error
218  if (!$sent)
219    {
220    raise_error(array('code' => 800,
221                      'type' => 'smtp',
222                      'line' => __LINE__,
223                      'file' => __FILE__,
224                      'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE);
225    }
226  }
227
228// send mail using PHP's mail() function
229else
230  {
231  // unset some headers because they will be added by the mail() function
232  $headers_php = $MAIL_MIME->_headers;
233  $headers_enc = $MAIL_MIME->headers($headers);
234  unset($headers_php['To'], $headers_php['Subject']);
235
236  // reset stored headers and overwrite
237  $MAIL_MIME->_headers = array();
238  $header_str = $MAIL_MIME->txtHeaders($headers_php);
239
240  if(ini_get('safe_mode'))
241    $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
242  else
243    $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
244  }
245
246
247// return to compose page if sending failed
248if (!$sent)
249  {
250  show_message("sendingfailed", 'error');
251  rcmail_overwrite_action('compose');
252  return;
253  }
254
255
256// set repliead flag
257if ($_SESSION['compose']['reply_uid'])
258  $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED');
259
260
261// copy message to sent folder
262if ($CONFIG['sent_mbox'])
263  {
264  // create string of complete message headers
265  $header_str = $MAIL_MIME->txtHeaders($headers);
266
267  // check if mailbox exists
268  if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes()))
269    $mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
270  else
271    $mbox = TRUE;
272
273  // append message to sent box
274  if ($mbox)
275    $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
276
277  // raise error if saving failed
278  if (!$saved)
279    raise_error(array('code' => 800,
280                      'type' => 'imap',
281                      'file' => __FILE__,
282                      'message' => "Could not save message in $CONFIG[sent_mbox]"), TRUE, FALSE);
283  }
284
285
286// log mail sending
287if ($CONFIG['smtp_log'])
288  {
289  $log_entry = sprintf("[%s] User: %d on %s; Message for %s; Subject: %s\n",
290               date("d-M-Y H:i:s O", mktime()),
291               $_SESSION['user_id'],
292               $_SERVER['REMOTE_ADDR'],
293               $mailto,
294               $msg_subject);
295
296  if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a'))
297    {
298    fwrite($fp, $log_entry);
299    fclose($fp);
300    }
301  }
302
303
304// show confirmation
305show_message('messagesent', 'confirmation');
306
307
308// kill compose entry from session
309rcmail_compose_cleanup();
310
311?>
Note: See TracBrowser for help on using the repository browser.