3.139.104.214 |    

Navigation

Google Advertisement

Class to send a mail with the unix command sendmail.

sendmail.class.php
  1. <?php
  2. /***************************************************************
  3.  * http://wiki.tuxnet24.de/
  4.  * Copyright (c) 2009 Marko Schulz <info@tuxnet24.de>
  5.  * All Rights Reserved.
  6.  *
  7.  * --LICENSE NOTICE--
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  * --LICENSE NOTICE--
  22.  ***************************************************************
  23.  * $File: sendmail.class.php $
  24.  * $Author: mschulz $ - $Date: 2009/01/11 00:46:20 $
  25.  * $Source: /var/lib/cvs/php-001/classes/sendmail.class.php,v $ - $Revision: 1.0 $
  26.  * $Description: Class to send a mail with the unix command sendmail. $
  27.  * $Copyright: (c) 2009 Marko Schulz - <info@tuxnet24.de> $
  28.  ***************************************************************/
  29.  
  30. class Sendmail {
  31.  
  32. 	// Default path to sendmail.
  33. 	private $_mailer = "/usr/sbin/sendmail";
  34.  
  35. 	// Class name and version.
  36. 	private $VERSION = "1.0";
  37. 	private $NAME = "Sendmail";
  38.  
  39. 	// @public: Sendmail()
  40. 	/***************************************************************
  41. 	* This is the Constructor of Class Sendmail.
  42. 	*
  43. 	* (object) $mail = new Sendmail ( array(  'recipient_mail' => (string) $recipient_mail,
  44. 	*					[ 'recipient_name' => (string) $recipient_name,
  45. 	*					  'sendmail' => (string) $sendmail ] ) );
  46. 	*
  47. 	***************************************************************/
  48. 	public function Sendmail ( $args = array() ) {
  49.  
  50. 	// Get the argument sendmail, the path to the sendmail binary.
  51. 	(string) $mailer = $args['sendmail'] ? $args['sendmail'] : $this->_mailer;
  52. 	(string) $name = $args['recipient_name'] ? $args['recipient_name'] : NULL;
  53. 	(string) $mail = $args['recipient_mail'] ? $args['recipient_mail'] : NULL;
  54.  
  55. 	// Check the format of the email address.
  56. 	$this->checkmail( $mail ) or die( "No valid email address as recipient defined in line ". __LINE__ );
  57.  
  58. 	// Return a error if no vailid path to sendmail was defined.
  59. 	is_executable( $mailer ) or
  60. 		die( "No valid path [".$mailer."] to sendmail defined in line ".__LINE__ );
  61.  
  62. 	// Create the mail object and open a pipe to sendmail.
  63. 	(object) $this->mail = popen( $mailer . " -t ", 'w' ) or
  64. 		die( "Can't open file handler to ".$mailer." in line ".__LINE__ );
  65.  
  66. 	// Set the mail header for recipient.
  67. 	if ( $name ) $this->header = "To: \"".$name."\" <".$mail.">\n";
  68. 	else $this->header = "To: \"".$mail."\" <".$mail.">\n";
  69.  
  70. 	return $this->mail;
  71.  
  72. 	}
  73.  
  74.  
  75. 	// @public: from()
  76. 	/***************************************************************
  77. 	* This method set the From mail address. Optional, you can
  78. 	* define a From name.
  79. 	*
  80. 	* (void) $mail->from ( array( 'mail' => (string) $email [, 'name' => (string) $name = NULL ] ) );
  81. 	*
  82. 	***************************************************************/
  83. 	public function from ( $args ) {
  84.  
  85. 	(string) $name = $args['name'] ? $args['name'] : NULL;
  86. 	(string) $mail = $args['mail'] ? $args['mail'] : NULL;
  87.  
  88. 	// Check the format of the email address.
  89. 	$this->checkmail( $mail ) or die( "No valid email address as from defined in line ". __LINE__ );
  90.  
  91. 	if ( $name ) $this->header = $this->header . "From: \"".$name."\" <".$mail.">\n";
  92. 	else $this->header = $this->header . "From: \"".$mail."\" <".$mail.">\n";
  93.  
  94. 	}
  95.  
  96.  
  97. 	// @public: replyto()
  98. 	/***************************************************************
  99. 	* This method set the Reply-to mail address. Optional, you can
  100. 	* define a Reply-to name.
  101. 	*
  102. 	* (void) $mail->replyto ( array( 'mail' => (string) $email [, 'name' => (string) $name = NULL ] ) );
  103. 	*
  104. 	***************************************************************/
  105. 	public function replyto ( $args ) {
  106.  
  107. 	(string) $name = $args['name'] ? $args['name'] : NULL;
  108. 	(string) $mail = $args['mail'] ? $args['mail'] : NULL;
  109.  
  110. 	// Check the format of the email address.
  111. 	$this->checkmail( $mail ) or die( "No valid email address as reply-to defined in line ". __LINE__ );
  112.  
  113. 	if ( $name ) $this->header = $this->header . "Reply-to: \"".$name."\" <".$mail.">\n";
  114. 	else $this->header = $this->header . "Reply-to: \"".$mail."\" <".$mail.">\n";
  115.  
  116. 	}
  117.  
  118.  
  119. 	// @public: subject()
  120. 	/***************************************************************
  121. 	* This method set the Subject of the mail.
  122. 	*
  123. 	* (void) $mail->subject ( (string) $sub );
  124. 	*
  125. 	***************************************************************/
  126. 	public function subject ( $sub = NULL ) {
  127.  
  128. 	$this->header = $this->header . "Subject: ".$sub."\n";
  129.  
  130. 	}
  131.  
  132.  
  133. 	// @public: header()
  134. 	/***************************************************************
  135. 	* This method set a mail header ( e.g. 'Content-Transfer-Encoding' => '8bit' ).
  136. 	*
  137. 	* (void) $mail->header ( (array) $args );
  138. 	*
  139. 	***************************************************************/
  140. 	public function header ( $args ) {
  141.  
  142. 	$this->header = $this->header . key($args) . ": " . $args[key($args)] . "\n";
  143.  
  144. 	}
  145.  
  146.  
  147. 	// @public: body()
  148. 	/***************************************************************
  149. 	* This method set the Body of the mail.
  150. 	*
  151. 	* (void) $mail->body ( [ (string) $args = NULL ] );
  152. 	*
  153. 	***************************************************************/
  154. 	public function body ( $args = NULL ) {
  155.  
  156. 	$this->body = $args . "\n";
  157.  
  158. 	}
  159.  
  160.  
  161. 	// @public: send()
  162. 	/***************************************************************
  163. 	* This method send the mail and return true/false.
  164. 	*
  165. 	* (bool) $mail->send ();
  166. 	*
  167. 	***************************************************************/
  168. 	public function send () {
  169.  
  170. 	// Set the X-Mailer header.
  171. 	(string) $match = "/(x-mailer:)([^\\n]+)/im";
  172. 	(string) $replace = "\\1 PHP ".$this->NAME." ".$this->VERSION;
  173.  
  174. 	if ( !preg_match( '/x-mailer:/i', $this->header ) )
  175. 		$this->header = $this->header . "X-Mailer: PHP ".$this->NAME." ".$this->VERSION."\n";
  176. 	else $this->header = preg_replace( $match, $replace, $this->header );
  177.  
  178. 	// Write the mail to data file hande (sendmail) and close it.
  179. 	if ( !@fputs( $this->mail, $this->header . "\n" . $this->body ) ) {
  180. 		return False;
  181. 	} else {
  182. 		if ( $this->close() ) return True;
  183. 		else return False;
  184. 	}
  185.  
  186. 	}
  187.  
  188.  
  189. 	// @private: close()
  190. 	/***************************************************************
  191. 	* This method close the data file handle.
  192. 	*
  193. 	* (bool) $mail->close ();
  194. 	*
  195. 	***************************************************************/
  196. 	private function close () {
  197. 	// popen return the exit status of the runnig process. If is all fine, popen return 0.
  198. 	if ( @pclose( $this->mail ) ) return False; 
  199. 	else return True;
  200. 	}
  201.  
  202.  
  203. 	// @private: checkmail()
  204. 	/***************************************************************
  205. 	* This method check the format of a email address.
  206. 	*
  207. 	* (bool) $mail->checkmail ( (string) &$mail );
  208. 	*
  209. 	***************************************************************/
  210. 	private function checkmail ( &$mail ) {
  211.  
  212. 	if ( !eregi ( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $mail ) ) return False;
  213. 	else return True;
  214.  
  215. 	}
  216.  
  217.  
  218. };
  219.  
  220. //**************************************************************
  221. // EOF
  222.  
  223. ?>
Parsed in 0.015 seconds at 504.15 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 4 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)