簡體   English   中英

如何重命名上傳的文件並將其作為電子郵件附件(PHP)發送?

[英]How do I rename an uploaded file and send it as an email attachment (PHP)?

我有一個PHP代碼( 使用example.php),提取從以下形式的電子郵件ID(sender@example.com), 獲取上傳的文件 ,並發送電子郵件一個 recipient@example.com上傳文件作為附件提取電子郵件ID作為發件人ID 然后,它將用戶重定向success.html頁面。

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> <label>Attachment <input name="my_file" type="file" /></label> <label><input name="button" type="submit" value="Submit" /></label></p> </form> <p> &nbsp;</p> 

表單的php是:

<?php
if($_POST && isset($_FILES['my_file']))
{
$recipient_email    = 'recipient@example.com';
//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.');  
}
}
?>

我需要將上傳的文件名重命名sender@example.com.extension,然后將上傳的文件作為附件發送。

我將感謝能夠解決我這個問題的任何人。

如在此答案中找到的: 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變量的其他行中轉義雙引號。

像這樣的情況:

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

請注意多余的引號和轉義的引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM