这个可不是一句话,就能讲完的,总之首先创建一个分割符,用来区分文本,以及附件,然后就是发送了,具体的楼主可以到网上查一查smtp协议,或者找一个支持多个附件的php smtp信件发送类看看

解决方案 »

  1.   

    <?
    /* Mailer
    ** author: episome <webmaster(at)3ants.org>
    ** website: http://3ants.org
    **/
    class mailer{
    var $mail = array();
    function mailer(){
    $this->mail['boundary'] = '===' . md5(uniqid('')) . '===';
    $this->mail['content_type'] = 'text/plain';
    $this->mail['attaches'] = '';
    $this->mail['sender'] = 'none';
    $this->mail['from'] = '[email protected]';
    }
    function addAttaches($attaches,$attacheContent=null){
    if(is_null($attacheContent)){
    if(is_string($attaches)){
    $attaches = explode(',',$attaches);
    }
    foreach ($attaches as $attache){
    if(!is_file($attache)){
    continue;
    }
    $attacheContent = join('',file($attache));
    $this->_addAttacheContent($attache,$attacheContent);
    }
    }else{
    $this->_addAttacheContent($attaches,$attacheContent);
    }
    }
    function _addAttacheContent($attache,$attacheContent){
    $this->mail['attaches'] .= "--" . $this->mail['boundary'] . "\n";
    $this->mail['attaches'] .= "Content-Type: application/octet-stream; name=\"" . basename($attache) . "\"\nContent-Transfer-Encoding: BASE64\nContent-Disposition: attachment; filename=\"" . basename($attache) . "\"\n";
    $this->mail['attaches'] .= "\n" . chunk_split(base64_encode($attacheContent)) . "\n\n";
    }
    function setFrom($email,$name='none'){
    $this->mail['from'] = $email;
    $this->mail['sender'] = $name;
    }
    function addTo($email){
    if(is_string($email)){
    $email = explode(',',$email);
    }
    foreach ($email as $id=>$mail){
    if(!$this->_isMail($mail)){
    unset($email[$id]);
    }
    }
    $this->mail['to'] = array_merge($this->mail['to'],$email);
    }
    function setContent($content,$content_type='text/plain'){
    $this->mail['content'] = $content;
    $this->mail['content_type'] = $content_type=='text/html'?'text/html':'text/plain';
    }
    function setSubject($subject){
    $this->mail['subject'] = $subject;
    }
    function send(){
    $this->mail['headers'] = "Content-Type: ". $this->mail['content_type']."\n";
    $this->mail['headers'] .= "\n" . $this->mail['content'] . "\n\n";
    if($this->mail['attaches']!=''){
    $this->mail['headers'] = "Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"".$this->mail['boundary']."\"\n\nThis is a multi-part message in MIME format.\n\n"
    . "--" . $this->mail['boundary'] . "\n"
    . $this->mail['headers']
    . $this->mail['attaches']
    . "--" . $this->mail['boundary'] . "--\n";
    }
    $this->mail['headers'] = "From: ".$this->mail['sender']." <".$this->mail['from'].">\n"
    //."Cc: [email protected]\r\n"
    //."Bcc: [email protected]\r\n"
    .$this->mail['headers'];
    return @mail(@join(',',$this->mail['to']), $this->mail['subject'],' ',$this->mail['headers']);
    }
    function _isMail($mail){
    return preg_match("/^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,4}$/", $mail);
    }
    function clear(){
    unset($this->mail);
    }
    }