2014-01-28 4 views
0

첨부 파일이있는 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

수동 mime 구성으로 문제를 파악하려면 그렇게하십시오. 여기에서 수백 가지 다른 복제본을 연구하십시오. 그러나 다른 사람들에게 디버깅을 요청하지 마십시오. 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"; 
       } 
관련 문제