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=? |
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; |
|
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 |
|
5bc8cb
|
76 |
// set default charset |
T |
77 |
if (empty($CHARSET)) |
|
78 |
$CHARSET = 'ISO-8859-1'; |
|
79 |
|
c03095
|
80 |
$input_charset = $CHARSET; |
T |
81 |
$message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset; |
|
82 |
|
1cded8
|
83 |
$mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m'); |
T |
84 |
$mailto_replace = array(', ', ', ', ''); |
4e17e6
|
85 |
|
6a35c8
|
86 |
// repalce new lines and strip ending ', ' |
T |
87 |
$mailto = preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_to'])); |
4e17e6
|
88 |
|
T |
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'], |
c03095
|
105 |
'To' => rcube_charset_convert($mailto, $input_charset, $message_charset)); |
4e17e6
|
106 |
|
T |
107 |
// additional recipients |
|
108 |
if ($_POST['_cc']) |
c03095
|
109 |
$headers['Cc'] = rcube_charset_convert(preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_cc'])), $input_charset, $message_charset); |
4e17e6
|
110 |
|
T |
111 |
if ($_POST['_bcc']) |
c03095
|
112 |
$headers['Bcc'] = rcube_charset_convert(preg_replace($mailto_regexp, $mailto_replace, stripslashes($_POST['_bcc'])), $input_charset, $message_charset); |
4e17e6
|
113 |
|
T |
114 |
if (strlen($identity_arr['bcc'])) |
|
115 |
$headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc']; |
|
116 |
|
|
117 |
// add subject |
58e360
|
118 |
$headers['Subject'] = rcube_charset_convert(trim($_POST['_subject']), $input_charset, $message_charset); |
4e17e6
|
119 |
|
T |
120 |
if (strlen($identity_arr['organization'])) |
|
121 |
$headers['Organization'] = $identity_arr['organization']; |
|
122 |
|
|
123 |
if (strlen($identity_arr['reply-to'])) |
|
124 |
$headers['Reply-To'] = $identity_arr['reply-to']; |
|
125 |
|
|
126 |
if ($_SESSION['compose']['reply_msgid']) |
|
127 |
$headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid']; |
|
128 |
|
|
129 |
|
|
130 |
if ($_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 |
|
|
143 |
if ($CONFIG['useragent']) |
|
144 |
$headers['User-Agent'] = $CONFIG['useragent']; |
|
145 |
|
c03095
|
146 |
// fetch message body |
58e360
|
147 |
$message_body = rcube_charset_convert($_POST['_message'], $input_charset, $message_charset); |
4e17e6
|
148 |
|
4b0f65
|
149 |
// append generic footer to all messages |
T |
150 |
if (!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); |
c03095
|
157 |
$message_body .= "\r\n" . rcube_charset_convert($content, 'UTF-8', $message_charset); |
4b0f65
|
158 |
} |
T |
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 |
|
4e17e6
|
165 |
// create PEAR::Mail_mime instance |
4b0f65
|
166 |
$MAIL_MIME = new Mail_mime($header_delm); |
c03095
|
167 |
$MAIL_MIME->setTXTBody($message_body, FALSE, TRUE); |
T |
168 |
//$MAIL_MIME->setTXTBody(wordwrap($message_body), FALSE, TRUE); |
4e17e6
|
169 |
|
T |
170 |
|
|
171 |
// add stored attachments, if any |
|
172 |
if (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 |
|
178 |
if (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 |
|
1cded8
|
182 |
|
a95e0e
|
183 |
// encoding settings for mail composing |
bde645
|
184 |
$message_param = array('text_encoding' => '8bit', |
a95e0e
|
185 |
'html_encoding' => 'quoted-printable', |
T |
186 |
'head_encoding' => 'quoted-printable', |
1cded8
|
187 |
'head_charset' => $message_charset, |
T |
188 |
'html_charset' => $message_charset, |
|
189 |
'text_charset' => $message_charset); |
4e17e6
|
190 |
|
T |
191 |
// compose message body and get headers |
a95e0e
|
192 |
$msg_body = $MAIL_MIME->get($message_param); |
4e17e6
|
193 |
$msg_subject = $headers['Subject']; |
T |
194 |
|
|
195 |
|
|
196 |
// send thru SMTP server using cusotm SMTP library |
|
197 |
if ($CONFIG['smtp_server']) |
|
198 |
{ |
968bdc
|
199 |
// generate list of recipients |
T |
200 |
$a_recipients = array($mailto); |
4e17e6
|
201 |
|
968bdc
|
202 |
if (strlen($headers['Cc'])) |
T |
203 |
$a_recipients[] = $headers['Cc']; |
|
204 |
if (strlen($headers['Bcc'])) |
|
205 |
$a_recipients[] = $headers['Bcc']; |
|
206 |
|
bde645
|
207 |
// clean Bcc from header for recipients |
T |
208 |
$send_headers = $headers; |
|
209 |
unset($send_headers['Bcc']); |
|
210 |
|
968bdc
|
211 |
// generate message headers |
bde645
|
212 |
$header_str = $MAIL_MIME->txtHeaders($send_headers); |
968bdc
|
213 |
|
T |
214 |
// send message |
|
215 |
$sent = smtp_mail($from, $a_recipients, $header_str, $msg_body); |
|
216 |
|
4e17e6
|
217 |
// log error |
968bdc
|
218 |
if (!$sent) |
4e17e6
|
219 |
{ |
T |
220 |
raise_error(array('code' => 800, |
|
221 |
'type' => 'smtp', |
|
222 |
'line' => __LINE__, |
|
223 |
'file' => __FILE__, |
968bdc
|
224 |
'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE); |
4e17e6
|
225 |
} |
T |
226 |
} |
|
227 |
|
|
228 |
// send mail using PHP's mail() function |
|
229 |
else |
|
230 |
{ |
|
231 |
// unset some headers because they will be added by the mail() function |
58e360
|
232 |
$headers_php = $MAIL_MIME->_headers; |
15a9d1
|
233 |
$headers_enc = $MAIL_MIME->headers($headers); |
4e17e6
|
234 |
unset($headers_php['To'], $headers_php['Subject']); |
T |
235 |
|
15a9d1
|
236 |
// reset stored headers and overwrite |
T |
237 |
$MAIL_MIME->_headers = array(); |
4e17e6
|
238 |
$header_str = $MAIL_MIME->txtHeaders($headers_php); |
c03095
|
239 |
|
fd660a
|
240 |
if(ini_get('safe_mode')) |
15a9d1
|
241 |
$sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str); |
T |
242 |
else |
|
243 |
$sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from"); |
4e17e6
|
244 |
} |
T |
245 |
|
c03095
|
246 |
|
4e17e6
|
247 |
// return to compose page if sending failed |
T |
248 |
if (!$sent) |
|
249 |
{ |
|
250 |
show_message("sendingfailed", 'error'); |
10a699
|
251 |
rcmail_overwrite_action('compose'); |
4e17e6
|
252 |
return; |
T |
253 |
} |
|
254 |
|
|
255 |
|
|
256 |
// set repliead flag |
|
257 |
if ($_SESSION['compose']['reply_uid']) |
|
258 |
$IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED'); |
|
259 |
|
|
260 |
|
|
261 |
// copy message to sent folder |
|
262 |
if ($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())) |
520c36
|
269 |
$mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE); |
T |
270 |
else |
|
271 |
$mbox = TRUE; |
4e17e6
|
272 |
|
T |
273 |
// append message to sent box |
520c36
|
274 |
if ($mbox) |
T |
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); |
4e17e6
|
283 |
} |
T |
284 |
|
|
285 |
|
|
286 |
// log mail sending |
|
287 |
if ($CONFIG['smtp_log']) |
|
288 |
{ |
749b07
|
289 |
$log_entry = sprintf("[%s] User: %d on %s; Message for %s; Subject: %s\n", |
4e17e6
|
290 |
date("d-M-Y H:i:s O", mktime()), |
T |
291 |
$_SESSION['user_id'], |
749b07
|
292 |
$_SERVER['REMOTE_ADDR'], |
4e17e6
|
293 |
$mailto, |
T |
294 |
$msg_subject); |
|
295 |
|
9fc381
|
296 |
if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a')) |
4e17e6
|
297 |
{ |
T |
298 |
fwrite($fp, $log_entry); |
|
299 |
fclose($fp); |
|
300 |
} |
|
301 |
} |
|
302 |
|
|
303 |
|
|
304 |
// show confirmation |
|
305 |
show_message('messagesent', 'confirmation'); |
|
306 |
|
|
307 |
|
|
308 |
// kill compose entry from session |
|
309 |
rcmail_compose_cleanup(); |
|
310 |
|
|
311 |
?> |