2016-07-15 13 views
1

私は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 

$filename = "file1.pdf"; 
$path = $_SERVER['DOCUMENT_ROOT'] . "/mail-function/upload/"; 
$file = $path.$filename; 
$file_size = filesize($file); 
$handle = fopen($file, "rb"); 
$content = fread($handle, $file_size); 
fclose($handle); 
$content = chunk_split(base64_encode($content)); 


$boundary = md5(uniqid(time())); 
//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"; 

//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: application/pdf; name=" . $filename . "\r\n"; 
$body .="Content-Disposition: attachment; filename=" . $filename . "\r\n"; 
$body .="Content-Transfer-Encoding: base64\r\n"; 
$body .="X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n"; 
$body .= $encoded_content; 

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

..

答えて

0

は、私はそれが役に立つことを願っています

$filename = "file1.pdf"; 
    $file = $path . "/" . $filename; 
    $message ="my message"; 
    $file_size = filesize($file); 
    $handle = fopen($file, "r"); 
    $content = fread($handle, $file_size); 
    fclose($handle); 
    $content = chunk_split(base64_encode($content)); 

    // a random hash will be necessary to send mixed content 
    $separator = md5(time()); 

    // carriage return type (we use a PHP end of line constant) 
    $eol = PHP_EOL; 

    // main header (multipart mandatory) 
    $headers = "From: name <[email protected]>" . $eol; 
    $headers .= "MIME-Version: 1.0" . $eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol; 
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol; 
    $headers .= "This is a MIME encoded message." . $eol; 

    // message 
    $headers .= "--" . $separator . $eol; 
    $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol; 
    $headers .= "Content-Transfer-Encoding: 8bit" . $eol; 
    $headers .= $message . $eol; 

    // attachment 
    $headers .= "--" . $separator . $eol; 
    $headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol; 
    $headers .= "Content-Transfer-Encoding: base64" . $eol; 
    $headers .= "Content-Disposition: attachment" . $eol; 
    $headers .= $content . $eol; 
    $headers .= "--" . $separator . "--"; 

    //SEND Mail 
    if (mail($mailto, $subject, "", $headers)) { 
     echo "mail send ... OK"; // or use booleans here 
     } else { 
     echo "mail send ... ERROR!"; 
     } 

、このコードを試してみてください。

関連する問題