source: github/program/steps/mail/sendmail.inc @ 4e17e6c

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 4e17e6c was 4e17e6c, checked in by thomascube <thomas@…>, 8 years ago

Initial revision

  • Property mode set to 100644
File size: 7.1 KB
Line 
1<?php
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                      |
9 | All rights reserved.                                                  |
10 |                                                                       |
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
24require_once('lib/smtp.inc');
25require_once('Mail/mime.php');
26
27
28if (!isset($_SESSION['compose']['id']))
29  {
30  $_action = 'list';
31  return;
32  }
33
34
35/****** message sending functions ********/
36
37
38
39function rcmail_get_identity($id)
40  {
41  global $DB;
42 
43  // get identity record
44  $sql_result = $DB->query(sprintf("SELECT *, email AS mailto
45                                    FROM   %s
46                                    WHERE  identity_id=%d
47                                    AND    user_id=%d
48                                    AND    del!='1'",
49                                   get_table_name('identities'),
50                                   $id,
51                                   $_SESSION['user_id']));
52                                   
53  if ($DB->num_rows($sql_result))
54    {
55    $sql_arr = $DB->fetch_assoc($sql_result);
56    $out = $sql_arr;
57    $out['string'] = sprintf('%s <%s>', $sql_arr['name'], $sql_arr['mailto']);
58    return $out;
59    }
60
61  return FALSE; 
62  }
63
64
65
66/****** check submission and compose message ********/
67
68
69$mailto_regexp = '/,\s*$/';
70
71// trip ending ', ' from
72$mailto = preg_replace($mailto_regexp, '', $_POST['_to']);
73
74// decode address strings
75$to_address_arr = $IMAP->decode_address_list($mailto);
76$identity_arr = rcmail_get_identity($_POST['_from']);
77
78
79$from = $identity_arr['mailto'];
80$first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto;
81
82
83// create unique message-id
84$message_id = sprintf('<%s@%s>', md5(uniqid('rcmail')), $_SESSION['imap_host']);
85
86
87// compose headers array
88$headers = array('Date' => date('D, j M Y G:i:s O'),
89                 'From' => $identity_arr['string'],
90                 'To'   => $mailto);
91
92// additional recipients
93if ($_POST['_cc'])
94  $headers['Cc'] = preg_replace($mailto_regexp, '', $_POST['_cc']);
95
96if ($_POST['_bcc'])
97  $headers['Bcc'] = preg_replace($mailto_regexp, '', $_POST['_bcc']);
98 
99if (strlen($identity_arr['bcc']))
100  $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
101
102// add subject
103$headers['Subject'] = trim(stripslashes($_POST['_subject']));
104
105if (strlen($identity_arr['organization']))
106  $headers['Organization'] = $identity_arr['organization'];
107
108if (strlen($identity_arr['reply-to']))
109  $headers['Reply-To'] = $identity_arr['reply-to'];
110
111if ($_SESSION['compose']['reply_msgid'])
112  $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
113
114
115if ($_POST['_priority'])
116  {
117  $priority = (int)$_POST['_priority'];
118  $a_priorities = array(1=>'lowest', 2=>'low', 4=>'high', 5=>'highest');
119  if ($str_priority = $a_priorities[$priority])
120    $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
121  }
122
123
124// additional headers
125$headers['Message-ID'] = $message_id;
126$headers['X-Sender'] = $from;
127
128if ($CONFIG['useragent'])
129  $headers['User-Agent'] = $CONFIG['useragent'];
130
131
132// create PEAR::Mail_mime instance
133$MAIL_MIME = new Mail_mime();
134$MAIL_MIME->setTXTBody(stripslashes($_POST['_message']), FALSE, TRUE);
135//$MAIL_MIME->setTXTBody(wordwrap(stripslashes($_POST['_message'])), FALSE, TRUE);
136
137
138// add stored attachments, if any
139if (is_array($_SESSION['compose']['attachments']))
140  foreach ($_SESSION['compose']['attachments'] as $attachment)
141    $MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE);
142
143 
144// add submitted attachments
145if (is_array($_FILES['_attachments']['tmp_name']))
146  foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath)
147    $MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE);
148
149
150// compose message body and get headers
151$msg_body = $MAIL_MIME->get();
152$msg_subject = $headers['Subject'];
153
154
155// send thru SMTP server using cusotm SMTP library
156if ($CONFIG['smtp_server'])
157  {
158  // connect to SMTP server
159  $smtp_conn = smtp_connect($CONFIG['smtp_server'], '25', $CONFIG['smtp_user'], $CONFIG['smtp_pass']);
160       
161  if ($smtp_conn)
162    {
163    // generate list of recipients
164    $recipients = $mailto.', '.$headers['Cc'].', '.$headers['Bcc'];
165    $a_recipients = smtp_expand($recipients);
166
167    // generate message headers
168    $header_str = $MAIL_MIME->txtHeaders($headers);
169       
170    // send message
171    $sent = smtp_mail($smtp_conn, $from, $a_recipients, $header_str."\r\n".$msg_body, FALSE);
172        }
173       
174  // log error
175  if (!$smtp_conn || !$sent)
176    {
177    raise_error(array('code' => 800,
178                      'type' => 'smtp',
179                      'line' => __LINE__,
180                      'file' => __FILE__,
181                      'message' => "Connection failed: $smtp_error"), TRUE, FALSE);
182    }
183  }
184
185// send mail using PHP's mail() function
186else
187  {
188  // unset some headers because they will be added by the mail() function
189  $headers_php = $headers;
190  unset($headers_php['To'], $headers_php['Subject']);
191
192  $header_str = $MAIL_MIME->txtHeaders($headers_php);
193  $sent = mail($mailto, $msg_subject, $msg_body, $header_str, "-f$from");
194  }
195
196
197// return to compose page if sending failed
198if (!$sent)
199  {
200  $_action = 'compose';
201  $OUTPUT->add_script(sprintf("\n%s.set_env('action', '%s');", $JS_OBJECT_NAME, $_action));
202  show_message("sendingfailed", 'error');
203  return;
204  }
205
206
207// set repliead flag
208if ($_SESSION['compose']['reply_uid'])
209  $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED');
210
211
212// copy message to sent folder
213if ($CONFIG['sent_mbox'])
214  {
215  // create string of complete message headers
216  $header_str = $MAIL_MIME->txtHeaders($headers);
217
218  // check if mailbox exists
219  if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes()))
220    $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
221
222  // append message to sent box
223  $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
224  }
225
226
227// log mail sending
228if ($CONFIG['smtp_log'])
229  {
230  $log_entry = sprintf("[%s] User: %d; Message for %s; Subject: %s\n",
231               date("d-M-Y H:i:s O", mktime()),
232               $_SESSION['user_id'],
233               $mailto,
234               $msg_subject);
235
236  if ($fp = fopen($INSTALL_PATH.'logs/sendmail', 'a'))
237    {
238    fwrite($fp, $log_entry);
239    fclose($fp);
240    }
241  }
242
243
244// show confirmation
245show_message('messagesent', 'confirmation');
246
247
248// kill compose entry from session
249rcmail_compose_cleanup();
250
251?>
Note: See TracBrowser for help on using the repository browser.