thomascube
2005-11-06 10a699759d4f106f29c077a6d65d3b8d212825e5
commit | author | age
4e17e6 1 <?php
T 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                      |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 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
968bdc 24 //require_once('lib/smtp.inc');
T 25 require_once('include/rcube_smtp.inc');
4e17e6 26 require_once('Mail/mime.php');
T 27
28
29 if (!isset($_SESSION['compose']['id']))
30   {
10a699 31   rcmail_overwrite_action('list');
4e17e6 32   return;
T 33   }
34
35
36 /****** message sending functions ********/
37
38
39
40 function rcmail_get_identity($id)
41   {
42   global $DB;
43   
44   // get identity record
d7cb77 45   $sql_result = $DB->query("SELECT *, email AS mailto
S 46                             FROM ".get_table_name('identities')."
47                             WHERE  identity_id=?
48                             AND    user_id=?
49                             AND    del<>'1'",
50                             $id,$_SESSION['user_id']);
4e17e6 51                                    
T 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 ********/
10a699 66
T 67
68 if (empty($_POST['_to']) && empty($_POST['_subject']) && $_POST['_message'])
69   {
70   show_message("sendingfailed", 'error'); 
71   rcmail_overwrite_action('compose');
72   return;
73   }
4e17e6 74
T 75
6a35c8 76 $mailto_regexp = array('/,\s*[\r\n]+/', '/[\r\n]+/', '/,\s*$/m');
T 77 $mailto_replace = array(' ', ', ', '');
4e17e6 78
6a35c8 79 // repalce new lines and strip ending ', '
T 80 $mailto = preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_to']));
4e17e6 81
T 82 // decode address strings
83 $to_address_arr = $IMAP->decode_address_list($mailto);
84 $identity_arr = rcmail_get_identity($_POST['_from']);
85
86
87 $from = $identity_arr['mailto'];
88 $first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto;
89
90
91 // create unique message-id
92 $message_id = sprintf('<%s@%s>', md5(uniqid('rcmail')), $_SESSION['imap_host']);
93
94
95 // compose headers array
96 $headers = array('Date' => date('D, j M Y G:i:s O'),
97                  'From' => $identity_arr['string'],
98                  'To'   => $mailto);
99
100 // additional recipients
101 if ($_POST['_cc'])
6a35c8 102   $headers['Cc'] = preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_cc']));
4e17e6 103
T 104 if ($_POST['_bcc'])
6a35c8 105   $headers['Bcc'] = preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_bcc']));
4e17e6 106   
T 107 if (strlen($identity_arr['bcc']))
108   $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
109
110 // add subject
111 $headers['Subject'] = trim(stripslashes($_POST['_subject']));
112
113 if (strlen($identity_arr['organization']))
114   $headers['Organization'] = $identity_arr['organization'];
115
116 if (strlen($identity_arr['reply-to']))
117   $headers['Reply-To'] = $identity_arr['reply-to'];
118
119 if ($_SESSION['compose']['reply_msgid'])
120   $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
121
122
123 if ($_POST['_priority'])
124   {
125   $priority = (int)$_POST['_priority'];
126   $a_priorities = array(1=>'lowest', 2=>'low', 4=>'high', 5=>'highest');
127   if ($str_priority = $a_priorities[$priority])
128     $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
129   }
130
131
132 // additional headers
133 $headers['Message-ID'] = $message_id;
134 $headers['X-Sender'] = $from;
135
136 if ($CONFIG['useragent'])
137   $headers['User-Agent'] = $CONFIG['useragent'];
138
139
140 // create PEAR::Mail_mime instance
141 $MAIL_MIME = new Mail_mime();
142 $MAIL_MIME->setTXTBody(stripslashes($_POST['_message']), FALSE, TRUE);
143 //$MAIL_MIME->setTXTBody(wordwrap(stripslashes($_POST['_message'])), FALSE, TRUE);
144
145
146 // add stored attachments, if any
147 if (is_array($_SESSION['compose']['attachments']))
148   foreach ($_SESSION['compose']['attachments'] as $attachment)
149     $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE);
150
151   
152 // add submitted attachments
153 if (is_array($_FILES['_attachments']['tmp_name']))
154   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath)
155     $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE);
156
a95e0e 157 // encoding settings for mail composing
T 158 $message_param = array('text_encoding' => '7bit',
159                        'html_encoding' => 'quoted-printable',
160                        'head_encoding' => 'quoted-printable',
161                        'head_charset'  => 'ISO-8859-1',
f3b659 162                        'html_charset'  => 'ISO-8859-1',
T 163                        'text_charset'  => 'ISO-8859-1');
4e17e6 164
T 165 // compose message body and get headers
a95e0e 166 $msg_body = $MAIL_MIME->get($message_param);
4e17e6 167 $msg_subject = $headers['Subject'];
T 168
169
170 // send thru SMTP server using cusotm SMTP library
171 if ($CONFIG['smtp_server'])
172   {
968bdc 173   // generate list of recipients
T 174   $a_recipients = array($mailto);
4e17e6 175
968bdc 176   if (strlen($headers['Cc']))
T 177     $a_recipients[] = $headers['Cc'];
178   if (strlen($headers['Bcc']))
179     $a_recipients[] = $headers['Bcc'];
180
181   // generate message headers
182   $header_str = $MAIL_MIME->txtHeaders($headers);
183
184   // send message
185   $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body);
186
4e17e6 187   // log error
968bdc 188   if (!$sent)
4e17e6 189     {
T 190     raise_error(array('code' => 800,
191                       'type' => 'smtp',
192                       'line' => __LINE__,
193                       'file' => __FILE__,
968bdc 194                       'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE);
4e17e6 195     }
T 196   }
197
198 // send mail using PHP's mail() function
199 else
200   {
201   // unset some headers because they will be added by the mail() function
202   $headers_php = $headers;
203   unset($headers_php['To'], $headers_php['Subject']);
204
205   $header_str = $MAIL_MIME->txtHeaders($headers_php);
fd660a 206   
T 207   if(ini_get('safe_mode'))
208     $sent = mail($mailto, $msg_subject, $msg_body, $header_str);
209   else  
210     $sent = mail($mailto, $msg_subject, $msg_body, $header_str, "-f$from");
4e17e6 211   }
T 212
213
214 // return to compose page if sending failed
215 if (!$sent)
216   {
217   show_message("sendingfailed", 'error'); 
10a699 218   rcmail_overwrite_action('compose');
4e17e6 219   return;
T 220   }
221
222
223 // set repliead flag
224 if ($_SESSION['compose']['reply_uid'])
225   $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED');
226
227
228 // copy message to sent folder
229 if ($CONFIG['sent_mbox'])
230   {
231   // create string of complete message headers
232   $header_str = $MAIL_MIME->txtHeaders($headers);
233
234   // check if mailbox exists
235   if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes()))
520c36 236     $mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
T 237   else
238     $mbox = TRUE;
4e17e6 239
T 240   // append message to sent box
520c36 241   if ($mbox)
T 242     $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
243
244   // raise error if saving failed
245   if (!$saved)
246     raise_error(array('code' => 800,
247                       'type' => 'imap',
248                       'file' => __FILE__,
249                       'message' => "Could not save message in $CONFIG[sent_mbox]"), TRUE, FALSE);
4e17e6 250   }
T 251
252
253 // log mail sending
254 if ($CONFIG['smtp_log'])
255   {
256   $log_entry = sprintf("[%s] User: %d; Message for %s; Subject: %s\n",
257                date("d-M-Y H:i:s O", mktime()),
258                $_SESSION['user_id'],
259                $mailto,
260                $msg_subject);
261
262   if ($fp = fopen($INSTALL_PATH.'logs/sendmail', 'a'))
263     {
264     fwrite($fp, $log_entry);
265     fclose($fp);
266     }
267   }
268
269
270 // show confirmation
271 show_message('messagesent', 'confirmation');
272
273
274 // kill compose entry from session
275 rcmail_compose_cleanup();
276
277 ?>