Re: Help Sending Mail

From: Date: Tue, 11 Dec 2007 05:05:41 +0000
Subject: Re: Help Sending Mail
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-265662@lists.php.net to get a copy of this message
Robert Cummings wrote:
On Tue, 2007-12-11 at 06:33 +0200, Me2resh Lists wrote:
Dear All, i wrote this class below to send Mail. but when i use it. i get the sender No Body. and from the address of the server, not the From address that i specify. and the MIME Headers as text in the message itself. and the attachment is pasted as binary text also. can anyone help please ?? here is the code : -------------------------------------------------------------------- <?php $Mail = new MainClass(); $Mail->Construct("Ahmed", "me2resh.lists@gmail.com", "Test Mail", "Hello, this is my first message"); $Mail->AddTo("ahmed@cha-international.com", "Ahmed"); $Mail->AddCc("me2resh@mac.com", "Me2resh"); $Mail->AddBcc("ahmed@menatravelawards.com", "hola"); $Mail->AddAttach("favicon.ico"); $Mail->SendMail(); echo "<pre>"; print_r($Mail); /** * */ class MainClass{
    /**
     *
     */
    var $To = "";
    var $Cc = "";
    var $Bcc = "";
    var $From = "";
    var $Name = "";
    var $Subject = "";
    var $Message = "";
    var $Headers = "";
    function Construct($name, $from, $subject, $message){
        //construct message
        if ($name == ""){
            $name = $from;
        }
        $this->Name = $name;
        $this->From = $from;
        $this->Subject = $subject;
        $this->Message = $message;
    }//end of Construct
    function AddTo($email, $name){
    // add reciepnt to To array
        if ($name == ""){
            $name = $email;
        }
        if ($this->To == ""){
            $this->To .= $name.' <'.$email.'>';
        }else{
            $this->To .= ",".$name.' <'.$email.'>';
        }
    }// end of AddTo
    function AddCc($email, $name){
    // add reciepnt to Cc array
    if ($name == ""){
            $name = $email;
        }
        if ($this->Cc == ""){
            $this->Cc .= $name.' <'.$email.'>';
        }else{
            $this->Cc .= ",".$name.' <'.$email.'>';
        }
    }
    function AddBcc($email, $name){
    // add reciepnt to Bcc array
    if ($name == ""){
            $name = $email;
        }
        if ($this->Bcc == ""){
            $this->Bcc .= $name.' <'.$email.'>';
        }else{
            $this->Bcc .= ",".$name.' <'.$email.'>';
        }
    }
    function AddAttach($attach){
    //add attach to array
        //if (is_uploaded_file($attach)) {
             // Read the file to be attached ('rb' = read binary)
             $file = fopen($attach,'rb');
             $data = fread($file,filesize($attach));
             fclose($file);
            // Generate a boundary string
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            // Add the headers for a file attachment
            $this->Headers .= "\nMIME-Version: 1.0\n" .
                        "Content-Type: multipart/mixed;\n" .
                        " boundary=\"{$mime_boundary}\"";
            // Add a multipart boundary above the plain message
            $this->Message = "This is a multi-part message in MIME
format.\n\n" .
                       "--{$mime_boundary}\n" .
                       "Content-Type: text/plain; charset=\"iso-8859-1\"\n"
.
                       "Content-Transfer-Encoding: 7bit\n\n" .
                       $this->Message . "\n\n";
            // Base64 encode the file data
             $data = chunk_split(base64_encode($data));
              // Add file attachment to the message
            $this->Message .= "--{$mime_boundary}\n" .
                        "Content-Type: {filetype($attach)};\n" .
                        " name=\"{$attach}\"\n" .
                        "Content-Disposition: attachment;\n" .
                        " filename=\"{$attach}\"\n" .
                        "Content-Transfer-Encoding: base64\n\n" .
                        $data . "\n\n" .
                        "--{$mime_boundary}--\n";
                     //   }// end of if
    }// end of add attach
    function SendMail(){
        $this->Headers .= "\r\n";
The last line above is your problem. Remove it.
Partly, check the $this->Headers .= ... part in the AddAttach method
Cheers, Rob.


« previous php.general (#265662) next »