2016-07-09 10 views
0

PHPを使用して電子メールで添付ファイルを送信しようとしています。私はいくつかのオンラインチュートリアルのページに従っており、私のmail()関数が真を返すようになっています。ただし、電子メールは送信していません。私はヘッダーとボディーを調べていて、私が間違っていることを特定することはできません。添付ファイル付きのPHPメールが返送されていない、真偽返信

$from_email = '[email protected]'; //sender email 
$recipient_email = '[email protected]'; //recipient email 
$subject = 'Test mail'; //subject of email 
$message = 'This is body of the message'; //message body 

//get file details we need 
$file_tmp_name = $_FILES['data']['tmp_name']; 
$file_name  = $_FILES['data']['name']; 
$file_size  = $_FILES['data']['size']; 
$file_type  = $_FILES['data']['type']; 
$file_error  = $_FILES['data']['error']; 

$user_email = filter_var("[email protected]", FILTER_SANITIZE_EMAIL); 

if($file_error>0) 
{ 
    echo 'upload error'; 
} 
//read from the uploaded file & base64_encode content for the mail 
$handle = fopen($file_tmp_name, "r"); 
$content = fread($handle, $file_size); 
fclose($handle); 
$encoded_content = chunk_split(base64_encode($content)); 


    $boundary = md5("sanwebe"); 
    //header 
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "From: ".$from_email."\r\n"; 
    $headers .= "Reply-To: ".$user_email."" . "\r\n"; 
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

    echo $headers; 
    //plain text 
    $body = "--$boundary\r\n"; 
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $body .= chunk_split(base64_encode($message)); 

    //attachment 
    $body .= "--$boundary\r\n"; 
    $body .="Content-Type: ".$file_type."; name=".$file_name."\r\n"; 
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
    $body .="Content-Transfer-Encoding: base64\r\n"; 
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 

    echo $body; 
    $body .= $encoded_content; 

$sentMail = @mail($recipient_email, $subject, $body, $headers); 
if($sentMail) //output success or failure messages 
{  
    echo 'Thank you for your email'; 
}else{ 
    echo 'Could not send mail! Please check your PHP mail configuration.'; 
} 

私は$headers$bodyを印刷するとき、これはプリントアウトするものである:

MIME-Version: 1.0 
From: [email protected] 
Reply-To: [email protected] 
Content-Type: multipart/mixed; boundary = 8de2a431c506316063ec3a4044192e46 

--8de2a431c506316063ec3a4044192e46 
Content-Type: text/plain; charset=ISO-8859-1 
Content-Transfer-Encoding: base64 

VGhpcyBpcyBib2R5IG9mIHRoZSBtZXNzYWdl 
--8de2a431c506316063ec3a4044192e46 
Content-Type: application/pdf; name=blob 
Content-Disposition: attachment; filename=blob 
Content-Transfer-Encoding: base64 
X-Attachment-Id: 32091 

Thank you for your email 

答えて

1

@ @mailコールがエラーを抑制する前に(thisを参照してください)。 (エラーを0に設定します)。 @を取り外して、もう一度やり直してください。

+0

ありがとうございました!エラーを確認するのは簡単な修正でした –

関連する問題