2017-06-30 11 views
0

私は、は、以下のフォームから電子メールID[email protected])を抽出(example.php)をPHPのコードを持っているがアップロードされたファイルを取得するには、受信者に電子メールを送信し @ example.comアップロードファイルを添付ファイルおよび送信者IDとして抽出しました。次にsuccess.htmlページにリダイレクトします。アップロードしたファイルの名前を変更して電子メールの添付ファイル(PHP)として送信するにはどうすればよいですか?

HTMLフォームは以下の通りです:

<form action="http://example.com/example.php" enctype="multipart/form-data" method="POST"> 
 
<label>Your Email <input name="sender_email" type="email" /> </label></p> 
 
\t \t \t \t <label>Attachment <input name="my_file" type="file" /></label> <label><input name="button" type="submit" value="Submit" /></label></p> 
 
\t \t </form> 
 
\t \t <p> 
 
\t \t \t &nbsp;</p>

フォームのPHPは次のとおりです。

<?php 
if($_POST && isset($_FILES['my_file'])) 
{ 
$recipient_email = '[email protected]'; 
//Capture POST data from HTML form and Sanitize them, 
$from_email = filter_var($_POST["sender_email"], 
FILTER_SANITIZE_STRING); //sender email used in "reply-to" header 
$subject  = SUBMISSION_PG; 
$message  = filter_var($_POST["message"], 
FILTER_SANITIZE_STRING); //message 
//Get uploaded file data 
$file_tmp_name = $_FILES['my_file']['tmp_name']; 
$file_name  = $_FILES['my_file']['name']; 
$file_size  = $_FILES['my_file']['size']; 
$file_type  = $_FILES['my_file']['type']; 
$file_error  = $_FILES['my_file']['error']; 
if($file_error > 0) 
{ 
die('Upload error or No files uploaded'); 
} 
//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: ".$from_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: $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"; 
$body .= $encoded_content; 
$sentMail = @mail($recipient_email, $subject, $body, $headers); 
if($sentMail) //output success or failure messages 
{  
header("Location:http://example.com/success.html"); 
}else{ 
die('Could not send mail! Please check your PHP mail 
configuration.'); 
} 
} 
?> 

私はに必要送信者にアップロードされたファイル名の名前を変更@ example.com.extensionアップロードされたf添付ファイルとしてile。

私はこの問題を解決できる人には感謝します。

+0

'$ file_name'をあなたが望むものに変更してください。電子メールのMIMEヘッダーは残りの部分を処理します –

+0

この投稿に重複している可能性があります。 https://stackoverflow.com/a/31428803/2311317 base64でファイルの内容をエンコードしているので( '$ encoded_content')、必要なコンテンツタイプは' octet-stream'です。そのため、あなたはそのメソッドを使うことができます。 – pokeybit

+0

ご協力いただきありがとうございます。私は確かにあなたのソリューションを試し、結果を知らせるでしょう。 –

答えて

0

この回答で見たよう:この行でhttps://stackoverflow.com/a/31428803/2311317

$body .="Content-Type: $file_type; name=".$file_name."\r\n"; 

変更いけないあなたが置いてこれらの行などであなたの二重引用符をエスケープするのを忘れ

$body .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\r\n"; 

へPHP変数。このようなのoccurance

name=\"".$file_name."\"\r\n" 

お知らせ余分な引用符と引用符をエスケープ。

関連する問題