• ¡Welcome to Square Theme!
  • This news are in header template.
  • Please ignore this message.
مهمان عزیز خوش‌آمدید. ورود عضــویت


امتیاز موضوع:
  • 14 رای - 1.93 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
Title: ارسال فایل با ایمیل
حالت خطی
#2
کد:
Class for sending mail with MIME attachments in multipart format using
external sendmail, mimencode and zip

<?

class Mail {
    var $from; //The sender
    var $to; //The recipient
    var $subject; //The subject line
    var $headers; //A hash of additional headers (headername => headervalue)
    var $zipname; //The name of the file the attachments are zipped into
                  //($zipname == false if attachments are to be sent
                      //individually)
    var $attachments; //An array of files to attach
    var $body; //The body text of the message
    
    //Mail constructor: initializes vars to default values. 'Nuff said.
    function Mail() {
        $this->from = "";
        $this->to = "";
        $this->subject = "";
        $this->headers = array();
        $this->zipname = false;
        $this->attachments = array();
        $this->body = "";
    }
    
    //Auxiliary method, used to guess a file's MIME type
    //based on its extension. Doesn't know about too many
    //extensions right now
    function guessMIMEType($filename) {
        //GUESS MIME TYPE
        $filename = basename($filename);
        if(strrchr($filename,".") == false) {
            return("application/octet-stream");
        }
        
        $ext = strrchr($filename,".");
        switch($ext) {
            case ".gif":
                return "image/gif";
                break;
            case ".gz":
                return "application/x-gzip";
            case ".htm":
            case ".html":
                return "text/html";
                break;
            case ".jpg":
                return "image/jpeg";
                break;
            case ".tar":
                return "application/x-tar";
                break;
            case ".txt":
                return "text/plain";
                break;
            case ".zip":
                return "application/zip";
                break;
            default:
                return "application/octet-stream";
                break;
        }
    }

    //Cute little convenience method. Supply it with a filename to
    //zip attachments to, or supply it with false if attachments are
    //sent individually
    function ZipAttachments($name) {
        $this->zipname = $name;
    }

    //The workhorse method, does the actually sending of the mail.
    //Doesn't check for errors so be careful!
    function Send($sendmail = "sendmail") {
        if($this->from == "")
            $fp = popen($sendmail . " -i " . $this->to, "w");
        else
            $fp = popen($sendmail . " -i -f\"" . $this->from . "\"
" . $this->to, "w");

        $mime_boundary = "-1747901728-1448367683-913849620=:4553";
        
        if($fp == false)
            return false;
        
        //Write subject header    
        fwrite($fp,"Subject: " . $this->subject . "\n");
    
        //Write user-defined headers    
        reset($this->headers);
        while(list($hdrname,$hdrval) = each($this->headers)) {
            fwrite($fp,$hdrname . ": " . $hdrval . "\n");
        }
        
        //If there are attachments, this needs to be a MIME message
        if(count($this->attachments) > 0) {
            //Write MIME headers
            fwrite($fp,"MIME-Version: 1.0\n");
            fwrite($fp,"Content-Type: multipart/mixed; BOUNDARY=\""
. $mime_boundary . "\"\n");
            fwrite($fp,"\n");
            //Write dummy message body
            fwrite($fp,"  This message is in MIME format.  The
first part should be readable text,\n");
            fwrite($fp,"  while the remaining parts are likely
unreadable without MIME-aware tools.\n");
            fwrite($fp,"\n");
            
            //Write message text
            fwrite($fp,"--" . "$mime_boundary" . "\n");
            fwrite($fp,"Content-Type: text/plain; charset=US-
ASCII\n");
            fwrite($fp,"\n");
            fwrite($fp,$this->body);
            fwrite($fp,"\n");
            
            //Handle attachments
            if($this->zipname != false) { //IF we've been told to
                                                      //zip the attachments
                fwrite($fp,"--" . $mime_boundary . "\n");
                fwrite($fp,"Content-Type: application/zip; name=
\"". $this->zipname . "\"\n");
                fwrite($fp,"Content-Transfer-Encoding: base64
\n");
                  //fwrite($fp,"Content-ID: " . $content_ID . "\n");
                fwrite($fp,"Content-Description:\n");
                fwrite($fp,"\n");
                $cmdline = "zip - ";
        while(list($key, $attachment_name) =  each($this->attachments))
                    $cmdline .= "$attachment_name ";
                $cmdline .= "| mimencode -b";
                $pp = popen($cmdline,"r");
                while(!feof($pp)) {
                    $data = fread($pp,4096);
                    fwrite($fp,$data);
                }
                pclose($pp);
            }
            else { //no need to zip the attachments, attach them
                               //separately
        while(list($key, $attachment_name) = each($this->attachments)) {
                fwrite($fp,"--" . $mime_boundary . "\n");
      fwrite($fp,"Content-Type: " . $this->guessMIMEType($attachment_name) . ";
      name=\"". basename($attachment_name) . "\"\n");
            fwrite($fp,"Content-Transfer-Encoding: base64\n");
                    //fwrite($fp,"Content-ID: " .
                                        //$content_ID . "\n");
                    fwrite($fp,"Content-Description:\n");
                    fwrite($fp,"\n");
                     $pp = popen("mimencode -b $attachment_name","r");
                    while(!feof($pp)) {
                        $data = fread($pp,4096);
                        fwrite($fp,$data);
                    }
                    pclose($pp);
                }
            }
            
            fwrite($fp,"--" . $mime_boundary . "--\n");
        }
        //No need for a MIME message, so it's an RFC822 message
        else {
            fwrite($fp,"\n");
            fwrite($fp,$this->body);
        }
        
        
        pclose($fp);
    }
}
?>
گروه دور همی پارسی کدرز
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg
 
پاسخ
  


پیام‌های این موضوع
ارسال فایل با ایمیل - توسط Ghoghnus - 07-10-2011، 04:04 PM
RE: ارسال فایل با ایمیل - توسط Amin_Mansouri - 07-11-2011، 05:08 PM
RE: ارسال فایل با ایمیل - توسط Ghoghnus - 07-13-2011، 10:38 PM
RE: ارسال فایل با ایمیل - توسط Ghoghnus - 07-14-2011، 12:06 PM
RE: ارسال فایل با ایمیل - توسط Ghoghnus - 07-14-2011، 02:13 PM

موضوعات مشابه ...
موضوع نویسنده پاسخ بازدید آخرین ارسال
  سورس کد فرستادن ایمیل به همراه فایل پیوست (پی اچ پی) Amin_Mansouri 2 10,352 11-10-2014، 09:18 PM
آخرین ارسال: farshadm
  اپلود فایل Ghoghnus 7 11,433 01-28-2013، 11:32 PM
آخرین ارسال: MarMar
  اپلود فایل با نام رندم امیر 0 3,022 08-05-2012، 12:34 PM
آخرین ارسال: امیر
  آپلود فایل تکست a.adhami 5 11,119 07-28-2012، 11:18 AM
آخرین ارسال: Amin_Mansouri
  ارسال اس ام اس a.adhami 2 3,942 07-02-2012، 09:34 PM
آخرین ارسال: Oep
Thumbs Down ایمیل سندر a.adhami 1 3,843 06-19-2012، 11:53 AM
آخرین ارسال: Amin_Mansouri
  سورس کد لاگین کردن به اف تی پی و اپلود فایل(پی اچ پی) Amin_Mansouri 0 4,863 06-17-2012، 09:32 AM
آخرین ارسال: Amin_Mansouri
  سورس کد اپلودر فایل (پی اچ پی) Amin_Mansouri 0 3,643 06-16-2012، 08:54 PM
آخرین ارسال: Amin_Mansouri
  سورس کد خواندن فایل جی زیپ ( پی اچ پی) Amin_Mansouri 0 3,186 06-16-2012، 08:39 PM
آخرین ارسال: Amin_Mansouri
  اجرای PHP از درون فایل های HTML Amin_Mansouri 0 3,144 02-03-2012، 05:56 PM
آخرین ارسال: Amin_Mansouri

پرش به انجمن:


Browsing: 1 مهمان