thomascube
2005-11-18 fbf77b4493f1b77c99751d8a86365c712ae3fb1b
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
4b0f65 140 // append generic footer to all messages
T 141 if (!empty($CONFIG['generic_message_footer']))
142   {
143   $file = realpath($CONFIG['generic_message_footer']);
144   if($fp = fopen($file, 'r'))
145     {
146     $content = fread($fp, filesize($file));
147     fclose($fp);
148     $_POST['_message'] .= "\r\n" . $content;
149     }
150   }
151
152
153 // use the configured delimiter for headers
154 $header_delm = $rcmail_config['mail_header_delimiter'] ? $rcmail_config['mail_header_delimiter'] : "\r\n";
155
4e17e6 156 // create PEAR::Mail_mime instance
4b0f65 157 $MAIL_MIME = new Mail_mime($header_delm);
4e17e6 158 $MAIL_MIME->setTXTBody(stripslashes($_POST['_message']), FALSE, TRUE);
T 159 //$MAIL_MIME->setTXTBody(wordwrap(stripslashes($_POST['_message'])), FALSE, TRUE);
160
161
162 // add stored attachments, if any
163 if (is_array($_SESSION['compose']['attachments']))
164   foreach ($_SESSION['compose']['attachments'] as $attachment)
165     $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE);
166
167   
168 // add submitted attachments
169 if (is_array($_FILES['_attachments']['tmp_name']))
170   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath)
171     $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE);
172
a95e0e 173 // encoding settings for mail composing
T 174 $message_param = array('text_encoding' => '7bit',
175                        'html_encoding' => 'quoted-printable',
176                        'head_encoding' => 'quoted-printable',
4b0f65 177                        'head_charset'  => $CHARSET,
T 178                        'html_charset'  => $CHARSET,
179                        'text_charset'  => $CHARSET);
4e17e6 180
T 181 // compose message body and get headers
a95e0e 182 $msg_body = $MAIL_MIME->get($message_param);
4e17e6 183 $msg_subject = $headers['Subject'];
T 184
185
186 // send thru SMTP server using cusotm SMTP library
187 if ($CONFIG['smtp_server'])
188   {
968bdc 189   // generate list of recipients
T 190   $a_recipients = array($mailto);
4e17e6 191
968bdc 192   if (strlen($headers['Cc']))
T 193     $a_recipients[] = $headers['Cc'];
194   if (strlen($headers['Bcc']))
195     $a_recipients[] = $headers['Bcc'];
196
197   // generate message headers
198   $header_str = $MAIL_MIME->txtHeaders($headers);
199
200   // send message
201   $sent = smtp_mail($from, $a_recipients, $header_str, $msg_body);
202
4e17e6 203   // log error
968bdc 204   if (!$sent)
4e17e6 205     {
T 206     raise_error(array('code' => 800,
207                       'type' => 'smtp',
208                       'line' => __LINE__,
209                       'file' => __FILE__,
968bdc 210                       'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE);
4e17e6 211     }
T 212   }
213
214 // send mail using PHP's mail() function
215 else
216   {
217   // unset some headers because they will be added by the mail() function
218   $headers_php = $headers;
219   unset($headers_php['To'], $headers_php['Subject']);
220
221   $header_str = $MAIL_MIME->txtHeaders($headers_php);
fd660a 222   
T 223   if(ini_get('safe_mode'))
224     $sent = mail($mailto, $msg_subject, $msg_body, $header_str);
225   else  
226     $sent = mail($mailto, $msg_subject, $msg_body, $header_str, "-f$from");
4e17e6 227   }
T 228
229
230 // return to compose page if sending failed
231 if (!$sent)
232   {
233   show_message("sendingfailed", 'error'); 
10a699 234   rcmail_overwrite_action('compose');
4e17e6 235   return;
T 236   }
237
238
239 // set repliead flag
240 if ($_SESSION['compose']['reply_uid'])
241   $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED');
242
243
244 // copy message to sent folder
245 if ($CONFIG['sent_mbox'])
246   {
247   // create string of complete message headers
248   $header_str = $MAIL_MIME->txtHeaders($headers);
249
250   // check if mailbox exists
251   if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes()))
520c36 252     $mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
T 253   else
254     $mbox = TRUE;
4e17e6 255
T 256   // append message to sent box
520c36 257   if ($mbox)
T 258     $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
259
260   // raise error if saving failed
261   if (!$saved)
262     raise_error(array('code' => 800,
263                       'type' => 'imap',
264                       'file' => __FILE__,
265                       'message' => "Could not save message in $CONFIG[sent_mbox]"), TRUE, FALSE);
4e17e6 266   }
T 267
268
269 // log mail sending
270 if ($CONFIG['smtp_log'])
271   {
272   $log_entry = sprintf("[%s] User: %d; Message for %s; Subject: %s\n",
273                date("d-M-Y H:i:s O", mktime()),
274                $_SESSION['user_id'],
275                $mailto,
276                $msg_subject);
277
fd8c50 278   if ($fp = fopen($CONFIG['log_dir'].'/sendmail', 'a'))
4e17e6 279     {
T 280     fwrite($fp, $log_entry);
281     fclose($fp);
282     }
283   }
284
285
286 // show confirmation
287 show_message('messagesent', 'confirmation');
288
289
290 // kill compose entry from session
291 rcmail_compose_cleanup();
292
293 ?>