ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBMFTransport_SMTP.php
Go to the documentation of this file.
1 <?php
29 require_once dirname(__FILE__).'/../class.ilBMFBase.php';
30 require_once 'Mail/smtp.php';
31 
45 {
46 
47  var $credentials = '';
48  var $timeout = 4; // connect timeout
49  var $urlparts = NULL;
50  var $url = '';
54  var $host = '127.0.0.1';
55  var $port = 25;
56  var $auth = NULL;
64  function ilBMFTransport_SMTP($URL, $encoding='US-ASCII')
65  {
66  parent::ilBMFBase('SMTP');
67  $this->encoding = $encoding;
68  $this->urlparts = @parse_url($URL);
69  $this->url = $URL;
70  }
71 
82  function send($msg, $options = array())
83  {
84  $this->incoming_payload = '';
85  $this->outgoing_payload = $msg;
86  if (!$this->_validateUrl()) {
87  return $this->fault;
88  }
89  if (!$options || !isset($options['from'])) {
90  return $this->_raiseSoapFault('No From: address to send message with');
91  }
92 
93  if (isset($options['host'])) $this->host = $options['host'];
94  if (isset($options['port'])) $this->port = $options['port'];
95  if (isset($options['auth'])) $this->auth = $options['auth'];
96  if (isset($options['username'])) $this->username = $options['username'];
97  if (isset($options['password'])) $this->password = $options['password'];
98 
99  $headers = array();
100  $headers['From'] = $options['from'];
101  $headers['X-Mailer'] = $this->_userAgent;
102  $headers['MIME-Version'] = '1.0';
103  $headers['Message-ID'] = md5(time()) . '.soap@' . $this->host;
104  $headers['To'] = $this->urlparts['path'];
105  if (isset($options['soapaction'])) {
106  $headers['Soapaction'] = "\"{$options['soapaction']}\"";
107  }
108 
109  if (isset($options['headers']))
110  $headers = array_merge($headers, $options['headers']);
111 
112  // If the content type is already set, we assume that MIME encoding is
113  // already done.
114  if (isset($headers['Content-Type'])) {
115  $out = $msg;
116  } else {
117  // Do a simple inline MIME encoding.
118  $headers['Content-Disposition'] = 'inline';
119  $headers['Content-Type'] = "text/xml; charset=\"$this->encoding\"";
120  if (isset($options['transfer-encoding'])) {
121  if (strcasecmp($options['transfer-encoding'], 'quoted-printable') == 0) {
122  $headers['Content-Transfer-Encoding'] = $options['transfer-encoding'];
123  $out = $msg;
124  } elseif (strcasecmp($options['transfer-encoding'],'base64') == 0) {
125  $headers['Content-Transfer-Encoding'] = 'base64';
126  $out = chunk_split(base64_encode($msg), 76, "\n");
127  } else {
128  return $this->_raiseSoapFault("Invalid Transfer Encoding: {$options['transfer-encoding']}");
129  }
130  } else {
131  // Default to base64.
132  $headers['Content-Transfer-Encoding'] = 'base64';
133  $out = chunk_split(base64_encode($msg));
134  }
135  }
136 
137  $headers['Subject'] = isset($options['subject']) ? $options['subject'] : 'SOAP Message';
138 
139  foreach ($headers as $key => $value) {
140  $header_text .= "$key: $value\n";
141  }
142  $this->outgoing_payload = $header_text . "\r\n" . $this->outgoing_payload;
143 
144  $mailer_params = array(
145  'host' => $this->host,
146  'port' => $this->port,
147  'username' => $this->username,
148  'password' => $this->password,
149  'auth' => $this->auth
150  );
151  $mailer =& new Mail_smtp($mailer_params);
152  $result = $mailer->send($this->urlparts['path'], $headers, $out);
153  if (!PEAR::isError($result)) {
154  $val =& new ilBMFValue('Message-ID', 'string', $headers['Message-ID']);
155  } else {
156  $sval[] =& new ilBMFValue('faultcode', 'QName', 'SOAP-ENV:Client');
157  $sval[] =& new ilBMFValue('faultstring', 'string', "couldn't send SMTP message to {$this->urlparts['path']}");
158  $val =& new ilBMFValue('Fault', 'Struct', $sval);
159  }
160 
161  $mqname =& new QName($method, $namespace);
162  $methodValue =& new ilBMFValue('Response', 'Struct', array($val));
163 
164  $this->incoming_payload = $this->_makeEnvelope($methodValue,
165  $this->headers,
166  $this->encoding);
167 
169  }
170 
179  function setCredentials($username, $password)
180  {
181  $this->username = $username;
182  $this->password = $password;
183  }
184 
191  function _validateUrl()
192  {
193  if (!is_array($this->urlparts)) {
194  $this->_raiseSoapFault("Unable to parse URL $url");
195  return false;
196  }
197  if (!isset($this->urlparts['scheme']) ||
198  strcasecmp($this->urlparts['scheme'], 'mailto') != 0) {
199  $this->_raiseSoapFault("Unable to parse URL $url");
200  return false;
201  }
202  if (!isset($this->urlparts['path'])) {
203  $this->_raiseSoapFault("Unable to parse URL $url");
204  return false;
205  }
206  return true;
207  }
208 
209 }