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