ILIAS  release_4-4 Revision
Mail_smtp Class Reference
+ Inheritance diagram for Mail_smtp:
+ Collaboration diagram for Mail_smtp:

Public Member Functions

 Mail_smtp ($params)
 Constructor. More...
 
 _Mail_smtp ()
 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. More...
 
getSMTPObject ()
 Connect to the SMTP server by instantiating a Net_SMTP object. More...
 
 addServiceExtensionParameter ($keyword, $value=null)
 Add parameter associated with a SMTP service extension. More...
 
 disconnect ()
 Disconnect and destroy the current SMTP connection. More...
 
 _error ($text, &$error)
 Build a standardized string describing the current SMTP 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
 
 $_extparams = array()
 
 $host = 'localhost'
 
 $port = 25
 
 $auth = false
 
 $username = ''
 
 $password = ''
 
 $localhost = 'localhost'
 
 $timeout = null
 
 $debug = false
 Turn on Net_SMTP debugging? More...
 
 $persist = false
 
 $pipelining
 
- Data Fields inherited from Mail
 $sep = "\r\n"
 

Detailed Description

Definition at line 74 of file smtp.php.

Member Function Documentation

◆ _error()

Mail_smtp::_error (   $text,
$error 
)

Build a standardized string describing the current SMTP error.

Parameters
string$textCustom string describing the error context.
object$errorReference to the current PEAR_Error object.
Returns
string A string describing the current SMTP error.
Since
1.1.7 private

Definition at line 433 of file smtp.php.

Referenced by getSMTPObject(), and send().

434  {
435  /* Split the SMTP response into a code and a response string. */
436  list($code, $response) = $this->_smtp->getResponse();
437 
438  /* Build our standardized error string. */
439  return $text
440  . ' [SMTP: ' . $error->getMessage()
441  . " (code: $code, response: $response)]";
442  }
+ Here is the caller graph for this function:

◆ _Mail_smtp()

Mail_smtp::_Mail_smtp ( )

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

Definition at line 216 of file smtp.php.

References disconnect().

217  {
218  $this->disconnect();
219  }
disconnect()
Disconnect and destroy the current SMTP connection.
Definition: smtp.php:411
+ Here is the call graph for this function:

◆ addServiceExtensionParameter()

Mail_smtp::addServiceExtensionParameter (   $keyword,
  $value = null 
)

Add parameter associated with a SMTP service extension.

Parameters
stringExtension keyword.
stringAny value the keyword needs.
Since
1.2.0 public

Definition at line 398 of file smtp.php.

Referenced by Mail_smtp().

399  {
400  $this->_extparams[$keyword] = $value;
401  }
+ Here is the caller graph for this function:

◆ disconnect()

Mail_smtp::disconnect ( )

Disconnect and destroy the current SMTP connection.

Returns
boolean True if the SMTP connection no longer exists.
Since
1.1.9 public

Definition at line 411 of file smtp.php.

Referenced by _Mail_smtp(), and send().

412  {
413  /* If we have an SMTP object, disconnect and destroy it. */
414  if (is_object($this->_smtp) && $this->_smtp->disconnect()) {
415  $this->_smtp = null;
416  }
417 
418  /* We are disconnected if we no longer have an SMTP object. */
419  return ($this->_smtp === null);
420  }
+ Here is the caller graph for this function:

◆ getSMTPObject()

& Mail_smtp::getSMTPObject ( )

Connect to the SMTP server by instantiating a Net_SMTP object.

Returns
mixed Returns a reference to the Net_SMTP object on success, or a PEAR_Error containing a descriptive error message on failure.
Since
1.2.0 public

Definition at line 342 of file smtp.php.

References $_smtp, $res, _error(), PEAR\isError(), PEAR_MAIL_SMTP_ERROR_AUTH, PEAR_MAIL_SMTP_ERROR_CONNECT, PEAR_MAIL_SMTP_ERROR_CREATE, and PEAR\raiseError().

Referenced by send().

343  {
344  if (is_object($this->_smtp) !== false) {
345  return $this->_smtp;
346  }
347 
348  include_once 'Net/SMTP.php';
349  $this->_smtp = &new Net_SMTP($this->host,
350  $this->port,
351  $this->localhost);
352 
353  /* If we still don't have an SMTP object at this point, fail. */
354  if (is_object($this->_smtp) === false) {
355  return PEAR::raiseError('Failed to create a Net_SMTP object',
357  }
358 
359  /* Configure the SMTP connection. */
360  if ($this->debug) {
361  $this->_smtp->setDebug(true);
362  }
363 
364  /* Attempt to connect to the configured SMTP server. */
365  if (PEAR::isError($res = $this->_smtp->connect($this->timeout))) {
366  $error = $this->_error('Failed to connect to ' .
367  $this->host . ':' . $this->port,
368  $res);
370  }
371 
372  /* Attempt to authenticate if authentication has been enabled. */
373  if ($this->auth) {
374  $method = is_string($this->auth) ? $this->auth : '';
375 
376  if (PEAR::isError($res = $this->_smtp->auth($this->username,
377  $this->password,
378  $method))) {
379  $error = $this->_error("$method authentication failure",
380  $res);
381  $this->_smtp->rset();
383  }
384  }
385 
386  return $this->_smtp;
387  }
const PEAR_MAIL_SMTP_ERROR_CREATE
Error: Failed to create a Net_SMTP object.
Definition: smtp.php:48
_error($text, &$error)
Build a standardized string describing the current SMTP error.
Definition: smtp.php:433
$_smtp
Definition: smtp.php:82
& 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's de...
Definition: PEAR.php:524
const PEAR_MAIL_SMTP_ERROR_AUTH
Error: SMTP authentication failure.
Definition: smtp.php:54
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
const PEAR_MAIL_SMTP_ERROR_CONNECT
Error: Failed to connect to SMTP server.
Definition: smtp.php:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Mail_smtp()

Mail_smtp::Mail_smtp (   $params)

Constructor.

Instantiates a new Mail_smtp:: object based on the parameters passed in. It looks for the following parameters: host The server to connect to. Defaults to localhost. port The port to connect to. Defaults to 25. auth SMTP authentication. Defaults to none. username The username to use for SMTP auth. No default. password The password to use for SMTP auth. No default. localhost The local hostname / domain. Defaults to localhost. timeout The SMTP connection timeout. Defaults to none. verp Whether to use VERP or not. Defaults to false. DEPRECATED as of 1.2.0 (use setMailParams()). debug Activate SMTP debug mode? Defaults to false. persist Should the SMTP connection persist? pipelining Use SMTP command pipelining

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

Parameters
arrayHash containing any parameters different from the defaults. public

Definition at line 191 of file smtp.php.

References addServiceExtensionParameter().

192  {
193  if (isset($params['host'])) $this->host = $params['host'];
194  if (isset($params['port'])) $this->port = $params['port'];
195  if (isset($params['auth'])) $this->auth = $params['auth'];
196  if (isset($params['username'])) $this->username = $params['username'];
197  if (isset($params['password'])) $this->password = $params['password'];
198  if (isset($params['localhost'])) $this->localhost = $params['localhost'];
199  if (isset($params['timeout'])) $this->timeout = $params['timeout'];
200  if (isset($params['debug'])) $this->debug = (bool)$params['debug'];
201  if (isset($params['persist'])) $this->persist = (bool)$params['persist'];
202  if (isset($params['pipelining'])) $this->pipelining = (bool)$params['pipelining'];
203 
204  // Deprecated options
205  if (isset($params['verp'])) {
206  $this->addServiceExtensionParameter('XVERP', is_bool($params['verp']) ? null : $params['verp']);
207  }
208 
209  register_shutdown_function(array(&$this, '_Mail_smtp'));
210  }
addServiceExtensionParameter($keyword, $value=null)
Add parameter associated with a SMTP service extension.
Definition: smtp.php:398
+ Here is the call graph for this function:

◆ send()

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

Implements Mail::send() function using SMTP.

Parameters
mixed$recipientsEither a comma-seperated list of recipients (RFC822 compliant), or an array of recipients, each RFC822 valid. This may contain recipients not specified in the headers, for Bcc:, resending messages, etc.
array$headersThe array of headers to send with the mail, in an associative array, where the array key is the header name (e.g., 'Subject'), and the array value is the header value (e.g., 'test'). The header produced from those values would be 'Subject: test'.
string$bodyThe full text of the message body, including any MIME parts, etc.
Returns
mixed Returns true on success, or a PEAR_Error containing a descriptive error message on failure. public

Definition at line 245 of file smtp.php.

References $res, $result, _error(), Mail\_sanitizeHeaders(), disconnect(), getSMTPObject(), PEAR\isError(), Mail\parseRecipients(), PEAR_MAIL_SMTP_ERROR_DATA, PEAR_MAIL_SMTP_ERROR_FROM, PEAR_MAIL_SMTP_ERROR_RECIPIENT, PEAR_MAIL_SMTP_ERROR_SENDER, Mail\prepareHeaders(), and PEAR\raiseError().

246  {
247  /* If we don't already have an SMTP object, create one. */
248  $result = &$this->getSMTPObject();
249  if (PEAR::isError($result)) {
250  return $result;
251  }
252 
253  if (!is_array($headers)) {
254  return PEAR::raiseError('$headers must be an array');
255  }
256 
257  $this->_sanitizeHeaders($headers);
258 
259  $headerElements = $this->prepareHeaders($headers);
260  if (is_a($headerElements, 'PEAR_Error')) {
261  $this->_smtp->rset();
262  return $headerElements;
263  }
264  list($from, $textHeaders) = $headerElements;
265 
266  /* Since few MTAs are going to allow this header to be forged
267  * unless it's in the MAIL FROM: exchange, we'll use
268  * Return-Path instead of From: if it's set. */
269  if (!empty($headers['Return-Path'])) {
270  $from = $headers['Return-Path'];
271  }
272 
273  if (!isset($from)) {
274  $this->_smtp->rset();
275  return PEAR::raiseError('No From: address has been provided',
277  }
278 
279  $params = null;
280  if (!empty($this->_extparams)) {
281  foreach ($this->_extparams as $key => $val) {
282  $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
283  }
284  }
285  if (PEAR::isError($res = $this->_smtp->mailFrom($from, ltrim($params)))) {
286  $error = $this->_error("Failed to set sender: $from", $res);
287  $this->_smtp->rset();
289  }
290 
291  $recipients = $this->parseRecipients($recipients);
292  if (is_a($recipients, 'PEAR_Error')) {
293  $this->_smtp->rset();
294  return $recipients;
295  }
296 
297  foreach ($recipients as $recipient) {
298  $res = $this->_smtp->rcptTo($recipient);
299  if (is_a($res, 'PEAR_Error')) {
300  $error = $this->_error("Failed to add recipient: $recipient", $res);
301  $this->_smtp->rset();
303  }
304  }
305 
306  /* Send the message's headers and the body as SMTP data. */
307  $res = $this->_smtp->data($textHeaders . "\r\n\r\n" . $body);
308  list(,$args) = $this->_smtp->getResponse();
309 
310  if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
311  $this->queued_as = $queued[1];
312  }
313 
314  /* we need the greeting; from it we can extract the authorative name of the mail server we've really connected to.
315  * ideal if we're connecting to a round-robin of relay servers and need to track which exact one took the email */
316  $this->greeting = $this->_smtp->getGreeting();
317 
318  if (is_a($res, 'PEAR_Error')) {
319  $error = $this->_error('Failed to send data', $res);
320  $this->_smtp->rset();
322  }
323 
324  /* If persistent connections are disabled, destroy our SMTP object. */
325  if ($this->persist === false) {
326  $this->disconnect();
327  }
328 
329  return true;
330  }
$result
_error($text, &$error)
Build a standardized string describing the current SMTP error.
Definition: smtp.php:433
& getSMTPObject()
Connect to the SMTP server by instantiating a Net_SMTP object.
Definition: smtp.php:342
disconnect()
Disconnect and destroy the current SMTP connection.
Definition: smtp.php:411
const PEAR_MAIL_SMTP_ERROR_DATA
Error: Failed to send data.
Definition: smtp.php:66
parseRecipients($recipients)
Take a set of recipients and parse them, returning an array of bare addresses (forward paths) that ca...
Definition: Mail.php:240
const PEAR_MAIL_SMTP_ERROR_FROM
Error: No From: address has been provided.
Definition: smtp.php:57
_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
const PEAR_MAIL_SMTP_ERROR_RECIPIENT
Error: Failed to add recipient.
Definition: smtp.php:63
& 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's de...
Definition: PEAR.php:524
const PEAR_MAIL_SMTP_ERROR_SENDER
Error: Failed to set sender.
Definition: smtp.php:60
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
+ Here is the call graph for this function:

Field Documentation

◆ $_extparams

Mail_smtp::$_extparams = array()

Definition at line 89 of file smtp.php.

◆ $_smtp

Mail_smtp::$_smtp = null

Definition at line 82 of file smtp.php.

Referenced by getSMTPObject().

◆ $auth

Mail_smtp::$auth = false

Definition at line 114 of file smtp.php.

◆ $debug

boolean Mail_smtp::$debug = false

Turn on Net_SMTP debugging?

Definition at line 148 of file smtp.php.

◆ $host

Mail_smtp::$host = 'localhost'

Definition at line 95 of file smtp.php.

◆ $localhost

Mail_smtp::$localhost = 'localhost'

Definition at line 134 of file smtp.php.

◆ $password

Mail_smtp::$password = ''

Definition at line 126 of file smtp.php.

◆ $persist

Mail_smtp::$persist = false

Definition at line 156 of file smtp.php.

◆ $pipelining

Mail_smtp::$pipelining

Definition at line 164 of file smtp.php.

◆ $port

Mail_smtp::$port = 25

Definition at line 101 of file smtp.php.

◆ $timeout

Mail_smtp::$timeout = null

Definition at line 141 of file smtp.php.

◆ $username

Mail_smtp::$username = ''

Definition at line 120 of file smtp.php.


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