0

サーバーからファイルを添付して、電子メールで添付ファイルとして送信する方法を誰か教えてもらえますか?? 私は次のコードを持っています:

<?php 
// Read POST request params into global vars 
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject']; 
$message = $_POST['message'];
// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name']; 
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";  
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file);
// Generate a boundary string 
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment  
$headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" . 
        " boundary=\"{$mime_boundary}\""; 
// Add a multipart boundary above the plain message 
 $message = "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" .   
$message . "\n\n"; 
// Base64 encode the file data 
// Add file attachment to the message 
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
 " name=\"{$fileatt_name}\"\n" . 
//"Content-Disposition: attachment;\n" . 
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .              
          $data . "\n\n" .  "--{$mime_boundary}--\n"; 
}
// Send the message 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
echo "<p>Mail sent! Yay PHP!</p>"; 
} else { 
echo "<p>Mail could not be sent. Sorry!</p>"; 
}
?>

ただし、このコードはサーバーからファイルを添付しません。私を助けてください..事前に感謝..!!

4

1 に答える 1

0

1 つの質問: 添付ファイル付きの電子メールを送信しますか、それともこの問題で PHP のスキルを向上させたいですか?

メールを送信するだけの場合は、非常に優れた簡単に処理できるPHPMailerをご覧ください。私自身、これを何年も問題なく使用しています。

あなたのスキルを向上させるために:添付ファイルがbase64でエンコードされているとのことですが、$data = base64_encode($data);のようなことをしている行が見つかりません。添付ファイルの$dataをそのままメールに追加します。

于 2012-04-04T16:17:49.297 に答える