ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
Mail_smtpmx Class Reference
+ Inheritance diagram for Mail_smtpmx:
+ Collaboration diagram for Mail_smtpmx:

Public Member Functions

 __construct ($params)
 Constructor.
 Mail_smtpmx ($params)
 Constructor wrapper for PHP4.
 __destruct ()
 Destructor implementation to ensure that we disconnect from any potentially-alive persistent SMTP connections.
 send ($recipients, $headers, $body)
 Implements Mail::send() function using SMTP direct delivery.
 _getMx ($host)
 Recieve mx rexords for a spciefied host.
 _loadNetDns ()
 initialize PEAR:Net_DNS_Resolver
 _raiseError ($id, $info=array())
 raise standardized error
- Public Member Functions inherited from Mail
factory ($driver, $params=array())
 Provides an interface for generating Mail:: objects of various types.
 _sanitizeHeaders (&$headers)
 Sanitize an array of mail headers by removing any additional header strings present in a legitimate header's value.
 prepareHeaders ($headers)
 Take an array of mail headers and return a string containing text usable in sending a message.
 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.

Data Fields

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

Detailed Description

Definition at line 61 of file smtpmx.php.

Constructor & Destructor Documentation

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().

{
if (isset($params['mailname'])) {
$this->mailname = $params['mailname'];
} else {
// try to find a valid mailname
if (function_exists('posix_uname')) {
$uname = posix_uname();
$this->mailname = $uname['nodename'];
}
}
// port number
if (isset($params['port'])) {
$this->_port = $params['port'];
} else {
$this->_port = getservbyname('smtp', 'tcp');
}
if (isset($params['timeout'])) $this->timeout = $params['timeout'];
if (isset($params['verp'])) $this->verp = $params['verp'];
if (isset($params['test'])) $this->test = $params['test'];
if (isset($params['peardebug'])) $this->test = $params['peardebug'];
if (isset($params['netdns'])) $this->withNetDns = $params['netdns'];
}

+ Here is the caller graph for this function:

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.

{
if (is_object($this->_smtp)) {
$this->_smtp->disconnect();
$this->_smtp = null;
}
}

Member Function Documentation

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().

{
$mx = array();
if ($this->withNetDns) {
$res = $this->_loadNetDns();
if (is_a($res, 'PEAR_Error')) {
return $res;
}
$response = $this->resolver->query($host, 'MX');
if (!$response) {
return false;
}
foreach ($response->answer as $rr) {
if ($rr->type == 'MX') {
$mx[$rr->exchange] = $rr->preference;
}
}
} else {
$mxHost = array();
$mxWeight = array();
if (!getmxrr($host, $mxHost, $mxWeight)) {
return false;
}
for ($i = 0; $i < count($mxHost); ++$i) {
$mx[$mxHost[$i]] = $mxWeight[$i];
}
}
asort($mx);
return $mx;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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().

{
if (is_object($this->resolver)) {
return true;
}
if (!include_once 'Net/DNS.php') {
return $this->_raiseError('no_resolver');
}
$this->resolver = new Net_DNS_Resolver();
if ($this->debug) {
$this->resolver->test = 1;
}
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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().

{
$code = $this->errorCode[$id]['code'];
$msg = $this->errorCode[$id]['msg'];
// include info to messages
if (!empty($info)) {
$search = array();
$replace = array();
foreach ($info as $key => $value) {
array_push($search, '{' . strtoupper($key) . '}');
array_push($replace, $value);
}
$msg = str_replace($search, $replace, $msg);
}
return PEAR::raiseError($msg, $code);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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().

{
$this->__construct($params);
register_shutdown_function(array(&$this, '__destruct'));
}

+ Here is the call graph for this function:

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

Reimplemented from Mail.

Definition at line 274 of file smtpmx.php.

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

{
if (!is_array($headers)) {
return PEAR::raiseError('$headers must be an array');
}
$result = $this->_sanitizeHeaders($headers);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
// Prepare headers
$headerElements = $this->prepareHeaders($headers);
if (is_a($headerElements, 'PEAR_Error')) {
return $headerElements;
}
list($from, $textHeaders) = $headerElements;
// use 'Return-Path' if possible
if (!empty($headers['Return-Path'])) {
$from = $headers['Return-Path'];
}
if (!isset($from)) {
return $this->_raiseError('no_from');
}
// Prepare recipients
$recipients = $this->parseRecipients($recipients);
if (is_a($recipients, 'PEAR_Error')) {
return $recipients;
}
foreach ($recipients as $rcpt) {
list($user, $host) = explode('@', $rcpt);
$mx = $this->_getMx($host);
if (is_a($mx, 'PEAR_Error')) {
return $mx;
}
if (empty($mx)) {
$info = array('rcpt' => $rcpt);
return $this->_raiseError('no_mx', $info);
}
$connected = false;
foreach ($mx as $mserver => $mpriority) {
$this->_smtp = new Net_SMTP($mserver, $this->port, $this->mailname);
// configure the SMTP connection.
if ($this->debug) {
$this->_smtp->setDebug(true);
}
// attempt to connect to the configured SMTP server.
$res = $this->_smtp->connect($this->timeout);
if (is_a($res, 'PEAR_Error')) {
$this->_smtp = null;
continue;
}
// connection established
if ($res) {
$connected = true;
break;
}
}
if (!$connected) {
$info = array(
'host' => implode(', ', array_keys($mx)),
'port' => $this->port,
'rcpt' => $rcpt,
);
return $this->_raiseError('not_connected', $info);
}
// Verify recipient
if ($this->vrfy) {
$res = $this->_smtp->vrfy($rcpt);
if (is_a($res, 'PEAR_Error')) {
$info = array('rcpt' => $rcpt);
return $this->_raiseError('failed_vrfy_rcpt', $info);
}
}
// mail from:
$args['verp'] = $this->verp;
$res = $this->_smtp->mailFrom($from, $args);
if (is_a($res, 'PEAR_Error')) {
$info = array('from' => $from);
return $this->_raiseError('failed_set_from', $info);
}
// rcpt to:
$res = $this->_smtp->rcptTo($rcpt);
if (is_a($res, 'PEAR_Error')) {
$info = array('rcpt' => $rcpt);
return $this->_raiseError('failed_set_rcpt', $info);
}
// Don't send anything in test mode
if ($this->test) {
$result = $this->_smtp->rset();
$res = $this->_smtp->rset();
if (is_a($res, 'PEAR_Error')) {
return $this->_raiseError('failed_rset');
}
$this->_smtp->disconnect();
$this->_smtp = null;
return true;
}
// Send data
$res = $this->_smtp->data("$textHeaders\r\n$body");
if (is_a($res, 'PEAR_Error')) {
$info = array('rcpt' => $rcpt);
return $this->_raiseError('failed_send_data', $info);
}
$this->_smtp->disconnect();
$this->_smtp = null;
}
return true;
}

+ Here is the call graph for this function:

Field Documentation

Mail_smtpmx::$_smtp = null

Definition at line 69 of file smtpmx.php.

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.

Mail_smtpmx::$errorCode

Definition at line 149 of file smtpmx.php.

Mail_smtpmx::$mailname = 'localhost'

Definition at line 85 of file smtpmx.php.

Mail_smtpmx::$port = 25

Definition at line 76 of file smtpmx.php.

Mail_smtpmx::$resolver

Definition at line 106 of file smtpmx.php.

Mail_smtpmx::$test = false

Definition at line 128 of file smtpmx.php.

Mail_smtpmx::$timeout = 10

Definition at line 92 of file smtpmx.php.

Mail_smtpmx::$verp = false

Definition at line 114 of file smtpmx.php.

Referenced by send().

boolean Mail_smtpmx::$vrfy = false

Whether to use VRFY or not.

Definition at line 121 of file smtpmx.php.

Mail_smtpmx::$withNetDns = true

Definition at line 99 of file smtpmx.php.


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