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