vendredi 30 juin 2017

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

I have a php code(example.php) which extracts the email id(sender@example.com) from the following form, gets the uploaded file and sends an email to a recipient@example.com with the uploaded file as attachment and the extracted email id as the sender id. Then it redirects the user to a success.html page.

The html form is as below:

<form action="http://ift.tt/19WJzYb" 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>

The php for the form is:

<?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://ift.tt/2uslppk");
}else{
die('Could not send mail! Please check your PHP mail 
configuration.');  
}
}
?>

I need to rename the uploaded filename to sender@example.com.extension then send the uploaded file as attachment.

I'd be thankful to anyone who can solve me this issue.




Aucun commentaire:

Enregistrer un commentaire