2017-07-20 4 views
0

私はウェブページ上に簡単なフォームを持っています。私はフォームに記入する人がファイルをアップロードできるようにしたい。それからメッセージと添付ファイルを私に電子メールで送ってほしい。私は何日ものコードをたくさん使っていますが、私にはうってつけのものは見つかりませんでした。PHP電子メールフォームで添付ファイルを取得するにはどうすればよいですか?

私の現在のコード:

<?php 

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 

     $flags = 'style="display:none;"'; 
     $message = $_POST['message']; 
     $email="[email protected]"; 
     $to = "[email protected]"; 
     $subject = "test Message with attachment"; 

     $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['uploaded_file']))); 
     $filename = $_FILES['file']['uploaded_file']; 

     $boundary =md5(date('r', time())); 

$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n"; 
$headers .= "From: ".$email."\r\n"; 
$headers .= "Reply-To: ".$email."\r\n" . 

$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"; 
exit(); 

    } // end of what happens if form submitted 
else{ 
?> 
<!-- end of form processing--> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test e-mail attachment form</title> 
</head> 
<body> 

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
    <input type="hidden" name="status" value="sent"> 
    <table > 
    <tr> 
     <td> 
      Message: 
     </td> 
     <td> 
      <input type="text" name="message" size="20" required> 
     </td> 
    </tr> 


    <tr> 
     <td> 
      Attach a file: 
     </td> 
     <td> 

<input type="file" name="uploaded_file"> 
       </td> 
    </tr> 
     <tr> 
     <td colspan="2"> 
      <input type="submit" value="Send message"> 
     </td> 
    </tr> 
</table> 

</form> 

</body> 
</html> 
<?php 
} 
?> 

メッセージがOK伝わってくるが、ない添付ファイルはありません。私は[Simple PHP form: Attachment to email (code golf)のようにここに見つかったカップルを含む多くの異なる方法を試しましたが、私が望むものを得ることができません。

私はまた、[http://form.guide/email-form/php-email-form-attachment.html][1]で見つけたものを試しましたが、それはPEARのインストールに関係していました。私はそれを試みましたが、それが私のホームページを上書きしたことを発見したので、私はそれをアンインストールしなければなりませんでした。

私はこれを動作させるのに近いところにいなければならないと確信していますが、何が間違っているかはわかりません。

+0

PHPMailerを試しましたか? –

+0

'file_get_contents($ _ FILES ['file'] ['uploaded_file']'ある意味、失敗しました。エラー報告を有効にするのが最適です。http://php.net/manual/en/function.error-reporting.php –

+0

答えは:https://stackoverflow.com/questions/826265/simple-php-form-attachment-to-email-code-golfは結局働いています。それにタイプミス:-) –

答えて

-1

PHPMailerライブラリを使用することをお勧めします。
send_file_uploadの例を使用できます。

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    $flags = 'style="display:none;"'; 
    $message = $_POST['message']; 
    $email="[email protected]"; 
    $to = "[email protected]"; 
    $subject = "test Message with attachment"; 
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['file']['name'])); 
    $msg = ''; 

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
    { 
     // Upload handled successfully 
     // Now create a message 
     // This should be somewhere in your include_path 
     require '../PHPMailerAutoload.php'; 
     $mail = new PHPMailer; 
     $mail->setFrom($email); 
     $mail->addAddress($to); 
     $mail->Subject = $subject; 
     $mail->Body = $message; 

     // Attach the uploaded file 
     $mail->addAttachment($uploadfile, 'My uploaded file'); 
     if (!$mail->send()) { 
      $msg .= "Mailer Error: " . $mail->ErrorInfo; 
     } else { 
      $msg .= "Message sent!"; 
     } 
    } else 
    { 
     $msg .= 'Failed to move file to ' . $uploadfile; 
    } 

    exit($msg); 
} else { ?> 
<!-- end of form processing--> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test e-mail attachment form</title> 
</head> 
<body> 

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
    <input type="hidden" name="status" value="sent"> 
    <table > 
    <tr> 
     <td> 
      Message: 
     </td> 
     <td> 
      <input type="text" name="message" size="20" required> 
     </td> 
    </tr> 


    <tr> 
     <td> 
      Attach a file: 
     </td> 
     <td> 

<input type="file" name="uploaded_file"> 
       </td> 
    </tr> 
     <tr> 
     <td colspan="2"> 
      <input type="submit" value="Send message"> 
     </td> 
    </tr> 
</table> 

</form> 

</body> 
</html> 
<?php }?> 

P.S.:
また、例の多くがそうhere.

を一覧表示され、あなたのコードは次のように書き換えることができます。このようなライブラリを使用すると、より柔軟で実用的なコードが得られます。
これらはすべての必須ヘッダーを構成し、単純なOOPインターフェイスを提供しているからです。

関連する問題