ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
Mail_smtpmx Class Reference
+ Inheritance diagram for Mail_smtpmx:
+ Collaboration diagram for Mail_smtpmx:

Public Member Functions

 __construct ($params)
 Constructor. More...
 
 Mail_smtpmx ($params)
 Constructor wrapper for PHP4. More...
 
 __destruct ()
 Destructor implementation to ensure that we disconnect from any potentially-alive persistent SMTP connections. More...
 
 send ($recipients, $headers, $body)
 Implements Mail::send() function using SMTP direct delivery. More...
 
 _getMx ($host)
 Recieve mx rexords for a spciefied host. More...
 
 _loadNetDns ()
 initialize PEAR:Net_DNS_Resolver More...
 
 _raiseError ($id, $info=array())
 raise standardized error More...
 
- Public Member Functions inherited from Mail
factory ($driver, $params=array())
 Provides an interface for generating Mail:: objects of various types. More...
 
 send ($recipients, $headers, $body)
 Implements Mail::send() function using php's built-in mail() command. More...
 
 _sanitizeHeaders (&$headers)
 Sanitize an array of mail headers by removing any additional header strings present in a legitimate header's value. More...
 
 prepareHeaders ($headers)
 Take an array of mail headers and return a string containing text usable in sending a message. More...
 
 parseRecipients ($recipients)
 Take a set of recipients and parse them, returning an array of bare addresses (forward paths) that can be passed to sendmail or an smtp server with the rcpt to: command. More...
 

Data Fields

 $_smtp = null
 
 $port = 25
 
 $mailname = 'localhost'
 
 $timeout = 10
 
 $withNetDns = true
 
 $resolver
 
 $verp = false
 
 $vrfy = false
 Whether to use VRFY or not. More...
 
 $test = false
 
 $debug = false
 Switch to test mode - don't send emails for real. More...
 
 $errorCode
 
- Data Fields inherited from Mail
 $sep = "\r\n"
 

Detailed Description

Definition at line 61 of file smtpmx.php.

Constructor & Destructor Documentation

◆ __construct()

Mail_smtpmx::__construct (   $params)

Constructor.

Instantiates a new Mail_smtp:: object based on the parameters passed in. It looks for the following parameters: mailname The name of the local mail system (a valid hostname which matches the reverse lookup) port smtp-port - the default comes from getservicebyname() and should work fine timeout The SMTP connection timeout. Defaults to 30 seconds. vrfy Whether to use VRFY or not. Defaults to false. verp Whether to use VERP or not. Defaults to false. test Activate test mode? Defaults to false. debug Activate SMTP and Net_DNS debug mode? Defaults to false. netdns whether to use PEAR:Net_DNS or the PHP build in function getmxrr, default is true

If a parameter is present in the $params array, it replaces the default.

public

Parameters
arrayHash containing any parameters different from the defaults.
See also
_Mail_smtpmx()

Definition at line 214 of file smtpmx.php.

Referenced by Mail_smtpmx().

215  {
216  if (isset($params['mailname'])) {
217  $this->mailname = $params['mailname'];
218  } else {
219  // try to find a valid mailname
220  if (function_exists('posix_uname')) {
221  $uname = posix_uname();
222  $this->mailname = $uname['nodename'];
223  }
224  }
225 
226  // port number
227  if (isset($params['port'])) {
228  $this->_port = $params['port'];
229  } else {
230  $this->_port = getservbyname('smtp', 'tcp');
231  }
232 
233  if (isset($params['timeout'])) $this->timeout = $params['timeout'];
234  if (isset($params['verp'])) $this->verp = $params['verp'];
235  if (isset($params['test'])) $this->test = $params['test'];
236  if (isset($params['peardebug'])) $this->test = $params['peardebug'];
237  if (isset($params['netdns'])) $this->withNetDns = $params['netdns'];
238  }
+ Here is the caller graph for this function:

◆ __destruct()

Mail_smtpmx::__destruct ( )

Destructor implementation to ensure that we disconnect from any potentially-alive persistent SMTP connections.

Definition at line 257 of file smtpmx.php.

258  {
259  if (is_object($this->_smtp)) {
260  $this->_smtp->disconnect();
261  $this->_smtp = null;
262  }
263  }

Member Function Documentation

◆ _getMx()

Mail_smtpmx::_getMx (   $host)

Recieve mx rexords for a spciefied host.

The MX records

private

Parameters
string$hostmail host
Returns
mixed sorted

Definition at line 411 of file smtpmx.php.

References $res, and _loadNetDns().

Referenced by send().

412  {
413  $mx = array();
414 
415  if ($this->withNetDns) {
416  $res = $this->_loadNetDns();
417  if (is_a($res, 'PEAR_Error')) {
418  return $res;
419  }
420 
421  $response = $this->resolver->query($host, 'MX');
422  if (!$response) {
423  return false;
424  }
425 
426  foreach ($response->answer as $rr) {
427  if ($rr->type == 'MX') {
428  $mx[$rr->exchange] = $rr->preference;
429  }
430  }
431  } else {
432  $mxHost = array();
433  $mxWeight = array();
434 
435  if (!getmxrr($host, $mxHost, $mxWeight)) {
436  return false;
437  }
438  for ($i = 0; $i < count($mxHost); ++$i) {
439  $mx[$mxHost[$i]] = $mxWeight[$i];
440  }
441  }
442 
443  asort($mx);
444  return $mx;
445  }
_loadNetDns()
initialize PEAR:Net_DNS_Resolver
Definition: smtpmx.php:453
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _loadNetDns()

Mail_smtpmx::_loadNetDns ( )

initialize PEAR:Net_DNS_Resolver

private

Returns
boolean true on success

Definition at line 453 of file smtpmx.php.

References _raiseError().

Referenced by _getMx().

454  {
455  if (is_object($this->resolver)) {
456  return true;
457  }
458 
459  if (!include_once 'Net/DNS.php') {
460  return $this->_raiseError('no_resolver');
461  }
462 
463  $this->resolver = new Net_DNS_Resolver();
464  if ($this->debug) {
465  $this->resolver->test = 1;
466  }
467 
468  return true;
469  }
_raiseError($id, $info=array())
raise standardized error
Definition: smtpmx.php:481
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _raiseError()

Mail_smtpmx::_raiseError (   $id,
  $info = array() 
)

raise standardized error

include additional information in error message

private

Parameters
string$idmaps error ids to codes and message
array$infooptional information in associative array
See also
_errorCode

Definition at line 481 of file smtpmx.php.

References PEAR\raiseError().

Referenced by _loadNetDns(), and send().

482  {
483  $code = $this->errorCode[$id]['code'];
484  $msg = $this->errorCode[$id]['msg'];
485 
486  // include info to messages
487  if (!empty($info)) {
488  $search = array();
489  $replace = array();
490 
491  foreach ($info as $key => $value) {
492  array_push($search, '{' . strtoupper($key) . '}');
493  array_push($replace, $value);
494  }
495 
496  $msg = str_replace($search, $replace, $msg);
497  }
498 
499  return PEAR::raiseError($msg, $code);
500  }
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Mail_smtpmx()

Mail_smtpmx::Mail_smtpmx (   $params)

Constructor wrapper for PHP4.

public

Parameters
arrayHash containing any parameters different from the defaults
See also
__construct()

Definition at line 247 of file smtpmx.php.

References __construct().

248  {
249  $this->__construct($params);
250  register_shutdown_function(array(&$this, '__destruct'));
251  }
__construct($params)
Constructor.
Definition: smtpmx.php:214
+ Here is the call graph for this function:

◆ send()

Mail_smtpmx::send (   $recipients,
  $headers,
  $body 
)

Implements Mail::send() function using SMTP direct delivery.

public

Parameters
mixed$recipientsin RFC822 style or array
array$headersThe array of headers to send with the mail.
string$bodyThe full text of the message body,
Returns
mixed Returns true on success, or a PEAR_Error

Definition at line 274 of file smtpmx.php.

References $res, $result, $verp, _getMx(), _raiseError(), Mail\_sanitizeHeaders(), Mail\parseRecipients(), Mail\prepareHeaders(), and PEAR\raiseError().

275  {
276  if (!is_array($headers)) {
277  return PEAR::raiseError('$headers must be an array');
278  }
279 
280  $result = $this->_sanitizeHeaders($headers);
281  if (is_a($result, 'PEAR_Error')) {
282  return $result;
283  }
284 
285  // Prepare headers
286  $headerElements = $this->prepareHeaders($headers);
287  if (is_a($headerElements, 'PEAR_Error')) {
288  return $headerElements;
289  }
290  list($from, $textHeaders) = $headerElements;
291 
292  // use 'Return-Path' if possible
293  if (!empty($headers['Return-Path'])) {
294  $from = $headers['Return-Path'];
295  }
296  if (!isset($from)) {
297  return $this->_raiseError('no_from');
298  }
299 
300  // Prepare recipients
301  $recipients = $this->parseRecipients($recipients);
302  if (is_a($recipients, 'PEAR_Error')) {
303  return $recipients;
304  }
305 
306  foreach ($recipients as $rcpt) {
307  list($user, $host) = explode('@', $rcpt);
308 
309  $mx = $this->_getMx($host);
310  if (is_a($mx, 'PEAR_Error')) {
311  return $mx;
312  }
313 
314  if (empty($mx)) {
315  $info = array('rcpt' => $rcpt);
316  return $this->_raiseError('no_mx', $info);
317  }
318 
319  $connected = false;
320  foreach ($mx as $mserver => $mpriority) {
321  $this->_smtp = new Net_SMTP($mserver, $this->port, $this->mailname);
322 
323  // configure the SMTP connection.
324  if ($this->debug) {
325  $this->_smtp->setDebug(true);
326  }
327 
328  // attempt to connect to the configured SMTP server.
329  $res = $this->_smtp->connect($this->timeout);
330  if (is_a($res, 'PEAR_Error')) {
331  $this->_smtp = null;
332  continue;
333  }
334 
335  // connection established
336  if ($res) {
337  $connected = true;
338  break;
339  }
340  }
341 
342  if (!$connected) {
343  $info = array(
344  'host' => implode(', ', array_keys($mx)),
345  'port' => $this->port,
346  'rcpt' => $rcpt,
347  );
348  return $this->_raiseError('not_connected', $info);
349  }
350 
351  // Verify recipient
352  if ($this->vrfy) {
353  $res = $this->_smtp->vrfy($rcpt);
354  if (is_a($res, 'PEAR_Error')) {
355  $info = array('rcpt' => $rcpt);
356  return $this->_raiseError('failed_vrfy_rcpt', $info);
357  }
358  }
359 
360  // mail from:
361  $args['verp'] = $this->verp;
362  $res = $this->_smtp->mailFrom($from, $args);
363  if (is_a($res, 'PEAR_Error')) {
364  $info = array('from' => $from);
365  return $this->_raiseError('failed_set_from', $info);
366  }
367 
368  // rcpt to:
369  $res = $this->_smtp->rcptTo($rcpt);
370  if (is_a($res, 'PEAR_Error')) {
371  $info = array('rcpt' => $rcpt);
372  return $this->_raiseError('failed_set_rcpt', $info);
373  }
374 
375  // Don't send anything in test mode
376  if ($this->test) {
377  $result = $this->_smtp->rset();
378  $res = $this->_smtp->rset();
379  if (is_a($res, 'PEAR_Error')) {
380  return $this->_raiseError('failed_rset');
381  }
382 
383  $this->_smtp->disconnect();
384  $this->_smtp = null;
385  return true;
386  }
387 
388  // Send data
389  $res = $this->_smtp->data("$textHeaders\r\n$body");
390  if (is_a($res, 'PEAR_Error')) {
391  $info = array('rcpt' => $rcpt);
392  return $this->_raiseError('failed_send_data', $info);
393  }
394 
395  $this->_smtp->disconnect();
396  $this->_smtp = null;
397  }
398 
399  return true;
400  }
$result
_raiseError($id, $info=array())
raise standardized error
Definition: smtpmx.php:481
parseRecipients($recipients)
Take a set of recipients and parse them, returning an array of bare addresses (forward paths) that ca...
Definition: Mail.php:240
_sanitizeHeaders(&$headers)
Sanitize an array of mail headers by removing any additional header strings present in a legitimate h...
Definition: Mail.php:153
prepareHeaders($headers)
Take an array of mail headers and return a string containing text usable in sending a message...
Definition: Mail.php:178
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
_getMx($host)
Recieve mx rexords for a spciefied host.
Definition: smtpmx.php:411
+ Here is the call graph for this function:

Field Documentation

◆ $_smtp

Mail_smtpmx::$_smtp = null

Definition at line 69 of file smtpmx.php.

◆ $debug

boolean Mail_smtpmx::$debug = false

Switch to test mode - don't send emails for real.

internal error codes

translate internal error identifier to PEAR-Error codes and human readable messages.

Todo:
as I need unique error-codes to identify what exactly went wrond I did not use intergers as it should be. Instead I added a "namespace" for each code. This avoids conflicts with error codes from different classes. How can I use unique error codes and stay conform with PEAR?

Definition at line 135 of file smtpmx.php.

◆ $errorCode

Mail_smtpmx::$errorCode

Definition at line 149 of file smtpmx.php.

◆ $mailname

Mail_smtpmx::$mailname = 'localhost'

Definition at line 85 of file smtpmx.php.

◆ $port

Mail_smtpmx::$port = 25

Definition at line 76 of file smtpmx.php.

◆ $resolver

Mail_smtpmx::$resolver

Definition at line 106 of file smtpmx.php.

◆ $test

Mail_smtpmx::$test = false

Definition at line 128 of file smtpmx.php.

◆ $timeout

Mail_smtpmx::$timeout = 10

Definition at line 92 of file smtpmx.php.

◆ $verp

Mail_smtpmx::$verp = false

Definition at line 114 of file smtpmx.php.

Referenced by send().

◆ $vrfy

boolean Mail_smtpmx::$vrfy = false

Whether to use VRFY or not.

Definition at line 121 of file smtpmx.php.

◆ $withNetDns

Mail_smtpmx::$withNetDns = true

Definition at line 99 of file smtpmx.php.


The documentation for this class was generated from the following file: