<?php
/***************************************************************
* http://wiki.tuxnet24.de/
* Copyright (c) 2009 Marko Schulz <info@tuxnet24.de>
* All Rights Reserved.
*
* --LICENSE NOTICE--
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* --LICENSE NOTICE--
***************************************************************
* $File: sendmail.class.php $
* $Author: mschulz $ - $Date: 2009/01/11 00:46:20 $
* $Source: /var/lib/cvs/php-001/classes/sendmail.class.php,v $ - $Revision: 1.0 $
* $Description: Class to send a mail with the unix command sendmail. $
* $Copyright: (c) 2009 Marko Schulz - <info@tuxnet24.de> $
***************************************************************/
class Sendmail {
// Default path to sendmail.
private $_mailer = "/usr/sbin/sendmail";
// Class name and version.
private $VERSION = "1.0";
private $NAME = "Sendmail";
// @public: Sendmail()
/***************************************************************
* This is the Constructor of Class Sendmail.
*
* (object) $mail = new Sendmail ( array( 'recipient_mail' => (string) $recipient_mail,
* [ 'recipient_name' => (string) $recipient_name,
* 'sendmail' => (string) $sendmail ] ) );
*
***************************************************************/
public function Sendmail ( $args = array() ) {
// Get the argument sendmail, the path to the sendmail binary.
(string) $mailer = $args['sendmail'] ? $args['sendmail'] : $this->_mailer;
(string) $name = $args['recipient_name'] ? $args['recipient_name'] : NULL;
(string) $mail = $args['recipient_mail'] ? $args['recipient_mail'] : NULL;
// Check the format of the email address.
$this->checkmail( $mail ) or die( "No valid email address as recipient defined in line ". __LINE__ );
// Return a error if no vailid path to sendmail was defined.
is_executable( $mailer ) or
die( "No valid path [".$mailer."] to sendmail defined in line ".__LINE__ );
// Create the mail object and open a pipe to sendmail.
(object) $this->mail = popen( $mailer . " -t ", 'w' ) or
die( "Can't open file handler to ".$mailer." in line ".__LINE__ );
// Set the mail header for recipient.
if ( $name ) $this->header = "To: \"".$name."\" <".$mail.">\n";
else $this->header = "To: \"".$mail."\" <".$mail.">\n";
return $this->mail;
}
// @public: from()
/***************************************************************
* This method set the From mail address. Optional, you can
* define a From name.
*
* (void) $mail->from ( array( 'mail' => (string) $email [, 'name' => (string) $name = NULL ] ) );
*
***************************************************************/
public function from ( $args ) {
(string) $name = $args['name'] ? $args['name'] : NULL;
(string) $mail = $args['mail'] ? $args['mail'] : NULL;
// Check the format of the email address.
$this->checkmail( $mail ) or die( "No valid email address as from defined in line ". __LINE__ );
if ( $name ) $this->header = $this->header . "From: \"".$name."\" <".$mail.">\n";
else $this->header = $this->header . "From: \"".$mail."\" <".$mail.">\n";
}
// @public: replyto()
/***************************************************************
* This method set the Reply-to mail address. Optional, you can
* define a Reply-to name.
*
* (void) $mail->replyto ( array( 'mail' => (string) $email [, 'name' => (string) $name = NULL ] ) );
*
***************************************************************/
public function replyto ( $args ) {
(string) $name = $args['name'] ? $args['name'] : NULL;
(string) $mail = $args['mail'] ? $args['mail'] : NULL;
// Check the format of the email address.
$this->checkmail( $mail ) or die( "No valid email address as reply-to defined in line ". __LINE__ );
if ( $name ) $this->header = $this->header . "Reply-to: \"".$name."\" <".$mail.">\n";
else $this->header = $this->header . "Reply-to: \"".$mail."\" <".$mail.">\n";
}
// @public: subject()
/***************************************************************
* This method set the Subject of the mail.
*
* (void) $mail->subject ( (string) $sub );
*
***************************************************************/
public function subject ( $sub = NULL ) {
$this->header = $this->header . "Subject: ".$sub."\n";
}
// @public: header()
/***************************************************************
* This method set a mail header ( e.g. 'Content-Transfer-Encoding' => '8bit' ).
*
* (void) $mail->header ( (array) $args );
*
***************************************************************/
public function header ( $args ) {
$this->header = $this->header . key($args) . ": " . $args[key($args)] . "\n";
}
// @public: body()
/***************************************************************
* This method set the Body of the mail.
*
* (void) $mail->body ( [ (string) $args = NULL ] );
*
***************************************************************/
public function body ( $args = NULL ) {
$this->body = $args . "\n";
}
// @public: send()
/***************************************************************
* This method send the mail and return true/false.
*
* (bool) $mail->send ();
*
***************************************************************/
public function send () {
// Set the X-Mailer header.
(string) $match = "/(x-mailer:)([^\\n]+)/im";
(string) $replace = "\\1 PHP ".$this->NAME." ".$this->VERSION;
if ( !preg_match( '/x-mailer:/i', $this->header ) )
$this->header = $this->header . "X-Mailer: PHP ".$this->NAME." ".$this->VERSION."\n";
else $this->header = preg_replace( $match, $replace, $this->header );
// Write the mail to data file hande (sendmail) and close it.
if ( !@fputs( $this->mail, $this->header . "\n" . $this->body ) ) {
return False;
} else {
if ( $this->close() ) return True;
else return False;
}
}
// @private: close()
/***************************************************************
* This method close the data file handle.
*
* (bool) $mail->close ();
*
***************************************************************/
private function close () {
// popen return the exit status of the runnig process. If is all fine, popen return 0.
if ( @pclose( $this->mail ) ) return False;
else return True;
}
// @private: checkmail()
/***************************************************************
* This method check the format of a email address.
*
* (bool) $mail->checkmail ( (string) &$mail );
*
***************************************************************/
private function checkmail ( &$mail ) {
if ( !eregi ( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $mail ) ) return False;
else return True;
}
};
//**************************************************************
// EOF
?>