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 |
{ |
13c1af
|
42 |
global $DB, $CHARSET, $OUTPUT; |
4e17e6
|
43 |
|
T |
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=? |
1cded8
|
49 |
AND del<>1", |
d7cb77
|
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; |
13c1af
|
56 |
$out['string'] = sprintf('%s <%s>', |
T |
57 |
rcube_charset_convert($sql_arr['name'], $CHARSET, $OUTPUT->get_charset()), |
|
58 |
$sql_arr['mailto']); |
4e17e6
|
59 |
return $out; |
T |
60 |
} |
|
61 |
|
|
62 |
return FALSE; |
|
63 |
} |
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
/****** check submission and compose message ********/ |
10a699
|
68 |
|
T |
69 |
|
|
70 |
if (empty($_POST['_to']) && empty($_POST['_subject']) && $_POST['_message']) |
|
71 |
{ |
|
72 |
show_message("sendingfailed", 'error'); |
|
73 |
rcmail_overwrite_action('compose'); |
|
74 |
return; |
|
75 |
} |
4e17e6
|
76 |
|
T |
77 |
|
5bc8cb
|
78 |
// set default charset |
13c1af
|
79 |
$input_charset = $OUTPUT->get_charset(); |
c03095
|
80 |
$message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset; |
T |
81 |
|
1cded8
|
82 |
$mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m'); |
T |
83 |
$mailto_replace = array(', ', ', ', ''); |
4e17e6
|
84 |
|
6a35c8
|
85 |
// repalce new lines and strip ending ', ' |
ea7c46
|
86 |
$mailto = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset)); |
4e17e6
|
87 |
|
T |
88 |
// decode address strings |
|
89 |
$to_address_arr = $IMAP->decode_address_list($mailto); |
|
90 |
$identity_arr = rcmail_get_identity($_POST['_from']); |
|
91 |
|
|
92 |
|
|
93 |
$from = $identity_arr['mailto']; |
|
94 |
$first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto; |
|
95 |
|
|
96 |
|
|
97 |
// create unique message-id |
|
98 |
$message_id = sprintf('<%s@%s>', md5(uniqid('rcmail')), $_SESSION['imap_host']); |
|
99 |
|
|
100 |
|
|
101 |
// compose headers array |
|
102 |
$headers = array('Date' => date('D, j M Y G:i:s O'), |
|
103 |
'From' => $identity_arr['string'], |
c03095
|
104 |
'To' => rcube_charset_convert($mailto, $input_charset, $message_charset)); |
4e17e6
|
105 |
|
T |
106 |
// additional recipients |
ea7c46
|
107 |
if (!empty($_POST['_cc'])) |
T |
108 |
$headers['Cc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset)); |
4e17e6
|
109 |
|
ea7c46
|
110 |
if (!empty($_POST['_bcc'])) |
T |
111 |
$headers['Bcc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset)); |
4e17e6
|
112 |
|
ea7c46
|
113 |
if (!empty($identity_arr['bcc'])) |
4e17e6
|
114 |
$headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc']; |
T |
115 |
|
|
116 |
// add subject |
ea7c46
|
117 |
$headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, FALSE, $message_charset)); |
4e17e6
|
118 |
|
ea7c46
|
119 |
if (!empty($identity_arr['organization'])) |
4e17e6
|
120 |
$headers['Organization'] = $identity_arr['organization']; |
T |
121 |
|
ea7c46
|
122 |
if (!empty($identity_arr['reply-to'])) |
4e17e6
|
123 |
$headers['Reply-To'] = $identity_arr['reply-to']; |
T |
124 |
|
f88d41
|
125 |
if (!empty($_SESSION['compose']['reply_msgid'])) |
4e17e6
|
126 |
$headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid']; |
T |
127 |
|
f88d41
|
128 |
if (!empty($_SESSION['compose']['references'])) |
T |
129 |
$headers['References'] = $_SESSION['compose']['references']; |
4e17e6
|
130 |
|
ea7c46
|
131 |
if (!empty($_POST['_priority'])) |
4e17e6
|
132 |
{ |
T |
133 |
$priority = (int)$_POST['_priority']; |
|
134 |
$a_priorities = array(1=>'lowest', 2=>'low', 4=>'high', 5=>'highest'); |
|
135 |
if ($str_priority = $a_priorities[$priority]) |
|
136 |
$headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority)); |
|
137 |
} |
|
138 |
|
|
139 |
|
|
140 |
// additional headers |
|
141 |
$headers['Message-ID'] = $message_id; |
|
142 |
$headers['X-Sender'] = $from; |
|
143 |
|
ea7c46
|
144 |
if (!empty($CONFIG['useragent'])) |
4e17e6
|
145 |
$headers['User-Agent'] = $CONFIG['useragent']; |
T |
146 |
|
c03095
|
147 |
// fetch message body |
ea7c46
|
148 |
$message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset); |
4e17e6
|
149 |
|
4b0f65
|
150 |
// append generic footer to all messages |
T |
151 |
if (!empty($CONFIG['generic_message_footer'])) |
|
152 |
{ |
|
153 |
$file = realpath($CONFIG['generic_message_footer']); |
|
154 |
if($fp = fopen($file, 'r')) |
|
155 |
{ |
|
156 |
$content = fread($fp, filesize($file)); |
|
157 |
fclose($fp); |
c03095
|
158 |
$message_body .= "\r\n" . rcube_charset_convert($content, 'UTF-8', $message_charset); |
4b0f65
|
159 |
} |
T |
160 |
} |
|
161 |
|
4647e1
|
162 |
// try to autodetect operating system and use the correct line endings |
4b0f65
|
163 |
// use the configured delimiter for headers |
4647e1
|
164 |
if (!empty($CONFIG['mail_header_delimiter'])) |
T |
165 |
$header_delm = $CONFIG['mail_header_delimiter']; |
|
166 |
else if (strtolower(substr(PHP_OS, 0, 3)=='win')) |
|
167 |
$header_delm = "\r\n"; |
|
168 |
else if (strtolower(substr(PHP_OS, 0, 3)=='mac')) |
|
169 |
$header_delm = "\r\n"; |
|
170 |
else |
|
171 |
$header_delm = "\n"; |
4b0f65
|
172 |
|
4e17e6
|
173 |
// create PEAR::Mail_mime instance |
4b0f65
|
174 |
$MAIL_MIME = new Mail_mime($header_delm); |
c03095
|
175 |
$MAIL_MIME->setTXTBody($message_body, FALSE, TRUE); |
T |
176 |
//$MAIL_MIME->setTXTBody(wordwrap($message_body), FALSE, TRUE); |
4e17e6
|
177 |
|
T |
178 |
|
|
179 |
// add stored attachments, if any |
|
180 |
if (is_array($_SESSION['compose']['attachments'])) |
|
181 |
foreach ($_SESSION['compose']['attachments'] as $attachment) |
|
182 |
$MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE); |
|
183 |
|
|
184 |
|
|
185 |
// add submitted attachments |
|
186 |
if (is_array($_FILES['_attachments']['tmp_name'])) |
|
187 |
foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) |
|
188 |
$MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE); |
|
189 |
|
1cded8
|
190 |
|
f88d41
|
191 |
// chose transfer encoding |
T |
192 |
$charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15'); |
|
193 |
$transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit'; |
|
194 |
|
a95e0e
|
195 |
// encoding settings for mail composing |
f88d41
|
196 |
$message_param = array('text_encoding' => $transfer_encoding, |
a95e0e
|
197 |
'html_encoding' => 'quoted-printable', |
T |
198 |
'head_encoding' => 'quoted-printable', |
1cded8
|
199 |
'head_charset' => $message_charset, |
T |
200 |
'html_charset' => $message_charset, |
|
201 |
'text_charset' => $message_charset); |
4e17e6
|
202 |
|
T |
203 |
// compose message body and get headers |
a95e0e
|
204 |
$msg_body = $MAIL_MIME->get($message_param); |
4e17e6
|
205 |
|
f88d41
|
206 |
$msg_subject = $headers['Subject']; |
13c1af
|
207 |
|
T |
208 |
if ($MBSTRING && function_exists("mb_encode_mimeheader")) |
|
209 |
$headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset); |
4e17e6
|
210 |
|
T |
211 |
// send thru SMTP server using cusotm SMTP library |
|
212 |
if ($CONFIG['smtp_server']) |
|
213 |
{ |
968bdc
|
214 |
// generate list of recipients |
T |
215 |
$a_recipients = array($mailto); |
4e17e6
|
216 |
|
968bdc
|
217 |
if (strlen($headers['Cc'])) |
T |
218 |
$a_recipients[] = $headers['Cc']; |
|
219 |
if (strlen($headers['Bcc'])) |
|
220 |
$a_recipients[] = $headers['Bcc']; |
|
221 |
|
bde645
|
222 |
// clean Bcc from header for recipients |
T |
223 |
$send_headers = $headers; |
|
224 |
unset($send_headers['Bcc']); |
|
225 |
|
968bdc
|
226 |
// generate message headers |
bde645
|
227 |
$header_str = $MAIL_MIME->txtHeaders($send_headers); |
968bdc
|
228 |
|
T |
229 |
// send message |
|
230 |
$sent = smtp_mail($from, $a_recipients, $header_str, $msg_body); |
|
231 |
|
4e17e6
|
232 |
// log error |
968bdc
|
233 |
if (!$sent) |
4e17e6
|
234 |
{ |
T |
235 |
raise_error(array('code' => 800, |
|
236 |
'type' => 'smtp', |
|
237 |
'line' => __LINE__, |
|
238 |
'file' => __FILE__, |
968bdc
|
239 |
'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE); |
4e17e6
|
240 |
} |
T |
241 |
} |
|
242 |
|
|
243 |
// send mail using PHP's mail() function |
|
244 |
else |
|
245 |
{ |
|
246 |
// unset some headers because they will be added by the mail() function |
15a9d1
|
247 |
$headers_enc = $MAIL_MIME->headers($headers); |
13c1af
|
248 |
$headers_php = $MAIL_MIME->_headers; |
4e17e6
|
249 |
unset($headers_php['To'], $headers_php['Subject']); |
13c1af
|
250 |
|
15a9d1
|
251 |
// reset stored headers and overwrite |
T |
252 |
$MAIL_MIME->_headers = array(); |
4e17e6
|
253 |
$header_str = $MAIL_MIME->txtHeaders($headers_php); |
c03095
|
254 |
|
13c1af
|
255 |
if (ini_get('safe_mode')) |
15a9d1
|
256 |
$sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str); |
T |
257 |
else |
|
258 |
$sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from"); |
4e17e6
|
259 |
} |
T |
260 |
|
c03095
|
261 |
|
4e17e6
|
262 |
// return to compose page if sending failed |
T |
263 |
if (!$sent) |
|
264 |
{ |
|
265 |
show_message("sendingfailed", 'error'); |
10a699
|
266 |
rcmail_overwrite_action('compose'); |
4e17e6
|
267 |
return; |
T |
268 |
} |
|
269 |
|
|
270 |
|
|
271 |
// set repliead flag |
|
272 |
if ($_SESSION['compose']['reply_uid']) |
|
273 |
$IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED'); |
|
274 |
|
|
275 |
|
|
276 |
// copy message to sent folder |
|
277 |
if ($CONFIG['sent_mbox']) |
|
278 |
{ |
|
279 |
// create string of complete message headers |
|
280 |
$header_str = $MAIL_MIME->txtHeaders($headers); |
|
281 |
|
|
282 |
// check if mailbox exists |
|
283 |
if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes())) |
520c36
|
284 |
$mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE); |
T |
285 |
else |
|
286 |
$mbox = TRUE; |
4e17e6
|
287 |
|
T |
288 |
// append message to sent box |
520c36
|
289 |
if ($mbox) |
T |
290 |
$saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body); |
|
291 |
|
|
292 |
// raise error if saving failed |
|
293 |
if (!$saved) |
|
294 |
raise_error(array('code' => 800, |
|
295 |
'type' => 'imap', |
|
296 |
'file' => __FILE__, |
|
297 |
'message' => "Could not save message in $CONFIG[sent_mbox]"), TRUE, FALSE); |
4e17e6
|
298 |
} |
T |
299 |
|
|
300 |
|
|
301 |
// log mail sending |
|
302 |
if ($CONFIG['smtp_log']) |
|
303 |
{ |
749b07
|
304 |
$log_entry = sprintf("[%s] User: %d on %s; Message for %s; Subject: %s\n", |
4e17e6
|
305 |
date("d-M-Y H:i:s O", mktime()), |
T |
306 |
$_SESSION['user_id'], |
749b07
|
307 |
$_SERVER['REMOTE_ADDR'], |
4e17e6
|
308 |
$mailto, |
T |
309 |
$msg_subject); |
|
310 |
|
9fc381
|
311 |
if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a')) |
4e17e6
|
312 |
{ |
T |
313 |
fwrite($fp, $log_entry); |
|
314 |
fclose($fp); |
|
315 |
} |
|
316 |
} |
|
317 |
|
|
318 |
|
|
319 |
// show confirmation |
|
320 |
show_message('messagesent', 'confirmation'); |
|
321 |
|
|
322 |
|
|
323 |
// kill compose entry from session |
|
324 |
rcmail_compose_cleanup(); |
|
325 |
|
|
326 |
?> |