[imp] A LMTP Driver for Mail class
Damian Fernandez Sosa (DESC)
damlists at cnba.uba.ar
Thu Feb 12 14:42:17 PST 2004
Here is a lmtp driver for PEAR Mail class (lmtp.php )
and an example of use (test_lmtp_driver.php)
Useful to PEAR and HORDE (imp) to - for example - create a "private webmail"
(users can send email ONLY to other local users via lmtp )
please commit it if you find useful to PEAR and/or Horde
-------------------------------------------------
Mail enviado desde el CNBA
http://www.cnba.uba.ar/
-------------- next part --------------
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license at php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Damian Alejandro Fernandez Sosa <damlists at cnba.uba.ar> |
// Chuck Hagenbuch <chuck at horde.org> |
// +----------------------------------------------------------------------+
require_once 'Mail.php';
/**
* LMTP implementation of the PEAR Mail:: interface. Requires the PEAR
* Net_LMTP:: class.
* @access public
* @package Mail
* @version $Revision: 1.0 $
*/
class Mail_lmtp extends Mail {
/**
* The LMTP host to connect to.
* @var string
*/
var $host = 'localhost';
/**
* The port the LMTP server is on.
* @var integer
*/
var $port = 2003;
/**
* Whether or not to attempt to authenticate to the LMTP server.
* @var boolean
*/
var $auth = false;
/**
* The username to use if the LMTP server requires authentication.
* @var string
*/
var $username = '';
/**
* The password to use if the LMTP server requires authentication.
* @var string
*/
var $password = '';
/**
* The password to use if the LMTP server requires authentication.
* @var boolean
*/
var $_debug = false;
/**
* Constructor.
*
* Instantiates a new Mail_lmtp:: 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 Whether or not to use LMTP auth. Defaults to false.
* username The username to use for LMTP auth. No default.
* password The password to use for LMTP auth. No default.
* debug Sets the debug feature true or false. Defaults to false.
*
* If a parameter is present in the $params array, it replaces the
* default.
*
* @param array Hash containing any parameters different from the
* defaults.
* @access public
*/
function Mail_lmtp($params)
{
if (isset($params['host'])){
$this->host = $params['host'];
}
if (isset($params['port'])){
$this->port = $params['port'];
}
if (isset($params['auth'])){
$this->auth = $params['auth'];
}
if (isset($params['username'])){
$this->username = $params['username'];
}
if (isset($params['password'])){
$this->password = $params['password'];
}
if (isset($params['debug'])){
$this->_debug = $params['debug'];
}
}
/**
* Implements Mail::send() function using LMTP.
*
* @param mixed $recipients Either 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.
*
* @param array $headers The array of headers to send with the mail, in an
* associative array, where the array key is the
* header name (ie, 'Subject'), and the array value
* is the header value (ie, 'test'). The header
* produced from those values would be 'Subject:
* test'.
*
* @param string $body The full text of the message body, including any
* Mime parts, etc.
*
* @return mixed Returns true on success, or a PEAR_Error
* containing a descriptive error message on
* failure.
* @access public
*/
function send($recipients, $headers, $body)
{
include_once 'Net/LMTP.php';
if (!($lmtp = new Net_LMTP($this->host, $this->port))){
return new PEAR_Error('unable to instantiate Net_LMTP object');
}
if (PEAR::isError(($ret=$lmtp->setDebug($this->_debug)))){
return new PEAR_Error('unable to set debug: ' . $ret->getMessage() );
}
if (PEAR::isError($lmtp->connect())){
return new PEAR_Error('unable to connect to lmtp server ' . $this->host . ':' . $this->port);
}
if ($this->auth) {
if(!is_string($this->auth)){
if (PEAR::isError(($ret=$lmtp->auth($this->username, $this->password)))){
return new PEAR_Error('unable to authenticate to lmtp server: ' . $ret->getMessage() );
}
}else{
if (PEAR::isError(($ret=$lmtp->auth($this->username, $this->password, $this->auth)))){
return new PEAR_Error('unable to authenticate to lmtp server: ' . $ret->getMessage() );
}
}
}
list($from, $text_headers) = $this->prepareHeaders($headers);
// Since few MTAs are going to allow this header to be forged
// unless it's in the MAIL FROM: exchange, we'll use Return-Path
// instead of From: if it's set
if (!empty($headers['Return-Path'])) {
$from = $headers['Return-Path'];
}
if (!isset($from)) {
return new PEAR_Error('No from address given');
}
if (PEAR::isError($lmtp->mailFrom($from))) {
return new PEAR_Error('unable to set sender to [' . $from . ']');
}
$recipients = $this->parseRecipients($recipients);
foreach($recipients as $recipient) {
if (PEAR::isError($res = $lmtp->rcptTo($recipient))){
return new PEAR_Error('unable to add recipient [' . $recipient . ']: ' . $res->getMessage());
}
}
if (PEAR::isError($lmtp->data($text_headers . "\r\n" . $body))) {
return new PEAR_Error('unable to send data');
}
$lmtp->disconnect();
return true;
}
}
?>
-------------- next part --------------
<?
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license at php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Damian Alejandro Fernandez Sosa <damlists at cnba.uba.ar> |
// Chuck Hagenbuch <chuck at horde.org> |
// +----------------------------------------------------------------------+
require_once 'Mail.php';
// The email as we send the email in the LMTP dialog
$to="damian at cnba.uba.ar";
// The email text (RFC822 format)
$email_headers['From']='damian at cnba.uba.ar';
$email_headers['To']='damian at cnba.uba.ar';
$email_headers['Date']='Wed, 21 Jan 2003 21:07:35 -300';
$email_headers['Subject']='testing LMTP';
$email_body="this is a test email\r\nto the lmtp server" ;
// The LMTP server
$param['host']='192.168.1.1';
// The default LMTP port
//$param['port']=2003;
$param['auth']=true;
//$param['auth']='PLAIN';
// The username to authenticate to the LMTP server
$param['username']='cyrus';
// The password to authenticate to the LMTP server
$param['password']='password';
// Should debug the LMTP connection?
//$param['debug']=true;
// Instantiate a LMTP class
if (PEAR::isError( ($lmtp= &Mail::factory('lmtp', $param ) ) ) ){
echo 'ERROR!!!' . $lmtp->getMessage() . "\n";
exit();
}
// Send the email
if (PEAR::isError( ($ret=$lmtp->send($to, $email_headers, $email_body) ) ) ){
echo 'ERROR!!!' . $ret->getMessage() . "\n";
}else{
echo "Email was sent successfuly!!!\n";
}
?>
More information about the imp
mailing list