net.manuellemos.mimemessage qmail_message_class @(#) $Id: qmail_message.php,v 1.11 2009/07/27 22:07:23 mlemos Exp $ Copyright � (C) Manuel Lemos 2001-2004 MIME E-mail message composing and sending using Qmail Manuel Lemos mlemos-at-acm.org en Implement an alternative message delivery method using Qmail http://www.qmail.org/ MTA (Mail Transfer Agent). This class should be used exactly the same way as the base class for composing and sending messages. Just create a new object of this class as follows and set only the necessary variables to configure details of delivery using Qmail. require('email_message.php');
require('qmail_message.php');

$message_object = new qmail_message_class;
{/metadocument} */ class qmail_message_class extends email_message_class { /* Private variables */ var $line_break="\n"; /* Public variables */ /* {metadocument} qmail_path STRING /var/qmail/bin Specifying the path of the Qmail programs. Usually the default is correct. {/metadocument} */ var $qmail_path="/var/qmail/bin"; /* {metadocument} mailer_delivery qmail $Revision: 1.11 $ Specify the text that is used to identify the mail delivery class or sub-class. This text is appended to the X-Mailer header text defined by the mailer variable. Do not change this variable. {/metadocument} */ var $mailer_delivery='qmail $Revision: 1.11 $'; Function SendMail($to,$subject,$body,$headers,$return_path) { $command=$this->qmail_path."/qmail-inject"; if(strcmp($return_path,"")) $command.=" '".preg_replace("/'/", "'\\''","-f$return_path")."'"; if(!($pipe=@popen($command,"w"))) return($this->OutputPHPError("it was not possible to open qmail-inject input pipe", error_get_last()['message'])); if(strlen($headers)) $headers.="\n"; if(!@fputs($pipe,"To: ".$to."\nSubject: ".$subject."\n".$headers."\n") || !@fputs($pipe,$body) || !@fflush($pipe)) return($this->OutputPHPError("it was not possible to write qmail-inject input pipe", error_get_last()['message'])); return(($rc=(pclose($pipe)>>8)) ? "qmail-inject error ".$rc : ""); } Function SendMailing($mailing) { if(strlen($this->error)) return($this->error); if(!IsSet($this->mailings[$mailing])) return($this->OutputError("it was not specified a valid mailing")); if(IsSet($this->mailings[$mailing]["Envelope"])) return($this->OutputError("the mailing was not yet ended")); $this->ResetMessage(); $base_path=$this->mailings[$mailing]["BasePath"]; if(GetType($header_lines=@File($base_path.".h"))!="array") return($this->OutputPHPError("could not read the mailing headers file ".$base_path.".h", error_get_last()['message'])); if(!($envelope_file=@fopen($base_path.".e","rb"))) return($this->OutputPHPError("could not open the mailing envelope file ".$base_path.".e", error_get_last()['message'])); for($return_path=$data="";!feof($envelope_file) || strlen($data);) { if(GetType($break=strpos($data,chr(0)))!="integer") { if(GetType($chunk=@fread($envelope_file,$this->file_buffer_length))!="string") { fclose($envelope_file); return($this->OutputPHPError("could not read the mailing envelop file ".$base_path.".e", error_get_last()['message'])); } $data.=$chunk; continue; } if($break==0) break; switch($data[0]) { case "F": $return_path=substr($data,1,$break-1); break; default: return($this->OutputError("invalid mailing envelope file ".$base_path.".e")); } break; } fclose($envelope_file); if(strlen($return_path)==0) return($this->OutputError("envelope file ".$base_path.".e does not contain a return path")); $headers=$this->FormatHeader("Date",gmdate("D, d M Y H:i:s -0000"))."\n"; for($has=array(),$line=0;$lineTokenize($header_lines[$line],":"); switch(strtolower($header)) { case "return-path": case "bcc": case "date": case "content-length": break; case "from": case "to": case "cc": case "message-id": $has[strtolower($header)]=1; default: $headers.=$this->FormatHeader($header,trim($this->Tokenize("\n")))."\n"; break; } } if(!IsSet($has["from"])) $headers.=$this->FormatHeader("From","<".$return_path.">")."\n"; if(!IsSet($has["to"]) && !IsSet($has["cc"])) $headers.=$this->FormatHeader("Cc","recipient list not shown: ;")."\n"; if(!IsSet($has["message-id"])) { $micros=$this->Tokenize(microtime()," "); $seconds=$this->Tokenize(""); $this->Tokenize($return_path,"@"); $host=$this->Tokenize("@"); if($host[strlen($host)-1]=="-") $host=substr($host,0,strlen($host)-1); $headers.=$this->FormatHeader("Message-ID","<".strftime("%Y%m%d%H%M%S",$seconds).substr($micros,1,5).".qmail@".$host.">")."\n"; } if(!($body_file=@fopen($base_path.".b","rb"))) return($this->OutputPHPError("could not open the mailing body file ".$base_path.".b", error_get_last()['message'])); for($body="";!feof($body_file);) { if(GetType($chunk=@fread($body_file,$this->file_buffer_length))!="string") { fclose($body_file); return($this->OutputPHPError("could not read the mailing body file ".$base_path.".b", error_get_last()['message'])); } $body.=$chunk; } fclose($body_file); $command=$this->qmail_path."/qmail-queue 1<".$base_path.".e"; if(!($pipe=@popen($command,"w"))) return($this->OutputPHPError("it was not possible to open qmail-queue input pipe", error_get_last()['message'])); if(!@fputs($pipe,$headers."\n".$body) || !@fflush($pipe)) return($this->OutputPHPError("it was not possible to write qmail-queue input pipe", error_get_last()['message'])); return(($rc=(pclose($pipe)>>8)) ? "qmail-queue error ".$rc : ""); } }; /* {metadocument}
{/metadocument} */ ?>