私はphpに比較的慣れていません。
PDF が添付された電子メールをユーザーに送信し、確認を同僚に送信するスクリプトを作成しました。gmail などの Web ベースの電子メール クライアントではすべて正常に動作しますが、PDF 添付ファイルを Outlook で受信すると開きません。また、完全に空白の「ATT0159.txt」という別の添付ファイルも追加されます。
コードは次のとおりです。
<?php
$pdf = $_GET['pdf'];
$product = $_GET['product'];
$username = $_POST['Name']; #get name
$useraddress = $_POST['Email']; #get user email address
$subjectUser = $product . " PDF brochure from Specifile on-line";
$to = "specifile@gmail.com"; # recipient
$subject = "Confirmation: PDF Request - " . $product; #subject
$message = "The following person has requested the brochure - " . $product . "\n<br/>" . $username . " [ " . $useraddress . " ] "; #message
$attachment = $_SERVER['DOCUMENT_ROOT'] . "\\" . $pdf;
$attachment_type = "application/pdf";
$attachment_name = "brochure.pdf";
#open, read, then close the file
$fp = fopen( $attachment, 'rb');
$file = fread($fp, filesize($attachment));
fclose($fp);
#create boundary string
$num = md5(time());
$str = "==Multipart_Boundary_x{$num}x";
#encode data for safe transit
$file = chunk_split(base64_encode($file));
#define user header
$headers = "MIME-version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"{$str}\"\r\n";
$headers .= "From: Specifile on-line<specifile@infixion.co.za> \r\n";
#define confirmation header
$Confirmheaders = "MIME-version: 1.0\r\n";
$Confirmheaders .= "Content-type: text/html;";
$Confirmheaders .= " charset=\"UTF-8\"\r\n";
$Confirmheaders .= "From: Specifile on-line<specifile@infixion.co.za> \r\n";
#create message for user
$messageUser = "This is a multi-part message in MIME format\r\n";
$messageUser .= "--{$str}\r\n";
$messageUser .= "Content-type: text/html; charset=\"UTF-8\"\r\n";
$messageUser .= "Content-Transfer-Encoding: 8bit\r\n";
$messageUser .= "Hi " . $username . ", <p>The brochure you requested is attatched.</p> \r\n\n";
$messageUser .= "--{$str}\r\n";
#define non text attachment
$messageUser .= "Content-Type: {$attachment_type}; ";
$messageUser .= "name=\"{$attachment_name}\"\r\n";
$messageUser .= "Content-Disposition: attachment; ";
$messageUser .= "filename=\"{$attachment_name}\"\r\n";
$messageUser .= "Content-Transfer-Encoding: base64\r\n";
$messageUser .= "$file\r\n\n";
$messageUser .= "--{$str}";
mail($useraddress,$subjectUser,$messageUser,$headers);
mail($to,$subject,$message,$Confirmheaders);
?>