2014-01-28 192 views
0

我面臨一個問題,即發送帶有Excel附件的PHP電子郵件。我可以看到郵件發送的消息,郵件即將到來,但沒有附件。我知道真正的問題。這是我的代碼。我已將User.xlsx文件放入根目錄中。請幫我解決這個問題。謝謝帶附件的PHP電子郵件

$to = "[email protected]"; 
    $subject="attachement"; 
    $mail_msg="message with attach"; 
    $filename="User.xlsx"; // Attachement file in root directory 
    $contentType="application/zip"; // Not sure about what to put here 
    $pathToFilename="./"; 
    $random_hash = md5(date('r', time())); 
    $headers = "From: [email protected]\r\nReply-To: ".$to; 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    $attachment = chunk_split(base64_encode(file_get_contents($pathToFilename))); 
    ob_start(); 
    echo " 
       --PHP-mixed-$random_hash 
       Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\" 

       --PHP-alt-$random_hash 
       Content-Type: text/plain; charset=\"utf-8\" 
       Content-Transfer-Encoding: 7bit 

       $mail_msg 

       --PHP-alt-$random_hash-- 

       --PHP-mixed-$random_hash 
       Content-Type: $contentType; name=\"$filename\" 
       Content-Transfer-Encoding: base64 
       Content-Disposition: attachment 

       $attachment 
       --PHP-mixed-$random_hash-- 
       "; 

    $message = ob_get_clean(); 
    // $fh=fopen('log.txt','w'); 
    // fwrite($fh,$message); 
    $mail_sent = @mail($to, $subject, $message, $headers); 
    echo $mail_sent ? "Mail sent" : "Mail failed"; 
+0

如果你想找出你的手動啞劇構建的問題,那麼做。在這裏研究數百個其他重複項。但不要求其他人進行調試。 PHPMailer和SwiftMailer都允許使用附件發送的簡單郵件,而無需擺弄從某處複製的代碼。 – mario

回答

0

我設法調試我的代碼,抱歉重複發佈。它可以幫助某人,謝謝

// Email to Client with attachement 
       //define the receiver of the email 
       $to = '[email protected]'; 
       //define the subject of the email 
       $subject = 'Bayern3 Notification'; 
       $filename = "User.xlsx"; 
       //create a boundary string. It must be unique 
       //so we use the MD5 algorithm to generate a random hash 
       $random_hash = md5(date('r', time())); 
       //define the headers we want passed. Note that they are separated with \r\n 
       $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
       //add boundary string and mime type specification 
       $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
       //read the atachment file contents into a string, 
       //encode it with MIME base64, 
       //and split it into smaller chunks 
       $attachment = chunk_split(base64_encode(file_get_contents($filename))); 
       //define the body of the message. 
       ob_start(); //Turn on output buffering 
       echo "--PHP-mixed-$random_hash 
         Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\" 

         --PHP-alt-$random_hash 
         Content-Type: text/plain; charset=\"iso-8859-1\" 
         Content-Transfer-Encoding: 7bit 

         --PHP-alt-$random_hash 
         Content-Type: text/html; charset=\"iso-8859-1\" 
         Content-Transfer-Encoding: 7bit 

          <h4>Hi,</h4> 
         <p><b>$userfullname</b> and his 10 friends qualified for <b>$venuename</b>. Please find the Excel sheet attached. You can download the detailed report form Bayern3 Admin Panel</p> 

         <h4>Regards,</h4> 
         <h4>Bayern3 Team</h4> 
         --PHP-alt-$random_hash-- 

         --PHP-mixed-$random_hash 
         Content-Type: application/zip; name=$filename 
         Content-Transfer-Encoding: base64 
         Content-Disposition: attachment 

         $attachment 
         --PHP-mixed-$random_hash--"; 

       //copy current buffer contents into $message variable and delete current output buffer 
       $message = ob_get_clean(); 
       //send the email 
       $mail_sent = @mail($to, $subject, $message, $headers); 
       //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
       //echo $mail_sent ? "Mail sent" : "Mail failed"; 
       if($mail_sent === True) 
       { 
        //echo "Mail Sent"; 
       } 
       else{ 
        //echo "Mail Failed"; 
       }