<?php
if(isset($_POST['submit'])){
$to = "recipient@example.com";
$subject = "File Attachment";
// Get the uploaded file information
$file_tmp_name = $_FILES['uploaded_file']['tmp_name'];
$file_name = $_FILES['uploaded_file']['name'];
$file_size = $_FILES['uploaded_file']['size'];
$file_type = $_FILES['uploaded_file']['type'];
// Read the file content
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
// Set the email headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary')."\r\n\r\n";
// Set the email message
$message = "--".md5('boundary')."\r\n";
$message .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode("Attached file: ".$file_name."\r\n\r\n"));
$message .= "--".md5('boundary')."\r\n";
$message .= "Content-Type: {$file_type}; name={$file_name}\r\n";
$message .= "Content-Disposition: attachment; filename={$file_name}\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($content));
$message .= "--".md5('boundary')."--";
// Send the email
if(mail($to, $subject, $message, $headers)){
echo "The file was successfully sent.";
} else {
echo "There was an error sending the file.";
}
} else {
// Display the form
echo '<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" name="submit" value="Upload">
</form>';
}
?>
specific only png and jpg and pdf file type
<?php
if(isset($_POST['submit'])){
$to = "recipient@example.com";
$subject = "File Attachment";
// Get the uploaded file information
$file_tmp_name = $_FILES['uploaded_file']['tmp_name'];
$file_name = $_FILES['uploaded_file']['name'];
$file_size = $_FILES['uploaded_file']['size'];
$file_type = $_FILES['uploaded_file']['type'];
// Check if the file is a PNG, JPG, or PDF
if($file_type != "image/png" && $file_type != "image/jpeg" && $file_type != "application/pdf"){
echo "Only PNG, JPG, and PDF files are allowed.";
exit();
}
// Read the file content
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
// Set the email headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary')."\r\n\r\n";
// Set the email message
$message = "--".md5('boundary')."\r\n";
$message .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode("Attached file: ".$file_name."\r\n\r\n"));
$message .= "--".md5('boundary')."\r\n";
$message .= "Content-Type: {$file_type}; name={$file_name}\r\n";
$message .= "Content-Disposition: attachment; filename={$file_name}\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($content));
$message .= "--".md5('boundary')."--";
// Send the email
if(mail($to, $subject, $message, $headers)){
echo "The file was successfully sent.";
} else {
echo "There was an error sending the file.";
}
} else {
// Display the form
echo '<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" name="submit" value="Send File">
</form>';
}
?>
short version
<?php
if (isset($_POST['submit'])) {
$to = "recipient@example.com";
$subject = "File Attachment";
$file_tmp_name = $_FILES['uploaded_file']['tmp_name'];
$file_name = $_FILES['uploaded_file']['name'];
$file_type = $_FILES['uploaded_file']['type'];
if ($file_type != "image/png" && $file_type != "image/jpeg" && $file_type != "application/pdf") {
echo "Only PNG, JPG, and PDF files are allowed.";
exit();
}
$content = file_get_contents($file_tmp_name);
$boundary = md5("boundary");
$headers = "MIME-Version: 1.0\r\n"
. "From: sender@example.com\r\n"
. "Reply-To: sender@example.com\r\n"
. "Content-Type: multipart/mixed; boundary=" . $boundary . "\r\n\r\n";
$message = "--" . $boundary . "\r\n"
. "Content-Type: text/plain; charset=ISO-8859-1\r\n"
. "Content-Transfer-Encoding: base64\r\n\r\n"
. chunk_split(base64_encode("Attached file: " . $file_name . "\r\n\r\n"))
. "--" . $boundary . "\r\n"
. "Content-Type: " . $file_type . "; name=" . $file_name . "\r\n"
. "Content-Disposition: attachment; filename=" . $file_name . "\r\n"
. "Content-Transfer-Encoding: base64\r\n\r\n"
. chunk_split(base64_encode($content))
. "--" . $boundary . "--";
if (mail($to, $subject, $message, $headers)) {
echo "The file was successfully sent.";
} else {
echo "There was an error sending the file.";
}
} else {
echo '<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="submit" name="submit" value="Send File">
</form>';
}
?>
add sender
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--<div style="display: flex; align-items: center; height: 100vh; justify-content: center;">-->
<style>
form {
width: 400px;
margin: 0 auto;
text-align: center;
}
input[type="file"] {
padding: 10px;
margin-bottom: 20px;
width: 100%;
box-sizing: border-box;
border: 2px solid gray;
border-radius: 5px;
}
input[type="submit"] {
padding: 10px 20px;
background-color: lightblue;
border: none;
border-radius: 5px;
color: white;
cursor: pointer;
}
</style>
<div style="display: flex; align-items: center; height: 100vh; justify-content: center; flex-direction: column;">
<?php
if(isset($_POST['submit'])){
$sender = $_POST['sender'];
$to = "yourmail@gmail.com";
$subject = "TOPIC";
// Get the uploaded file information
$file_tmp_name = $_FILES['uploaded_file']['tmp_name'];
$file_name = $_FILES['uploaded_file']['name'];
$file_size = $_FILES['uploaded_file']['size'];
$file_type = $_FILES['uploaded_file']['type'];
// Check if the file is a PNG, JPG, or PDF
if($file_type != "image/png" && $file_type != "image/jpeg" && $file_type != "application/pdf"){
echo "Only!!! PNG, JPG, PDF";
exit();
}
// Read the file content
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
// Set the email headers
$headers = "From: " . $sender . "\r\n";
$headers .= "Reply-To: " . $sender . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=XYZ\r\n";
// Define the message
$message = "--XYZ\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= "Sender: " . $sender . "\r\n\r\n";
$message .= "--XYZ\r\n";
$message .= "Content-Type: " . $file_type . "; name=" . $file_name . "\r\n";
$message .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
$message .= chunk_split(base64_encode($content));
// Send the email
if(mail($to, $subject, $message, $headers)){
echo "The file was successfully sent.";
include_once $_SERVER['DOCUMENT_ROOT'] . '/request/thanks.php';
}
else {
echo "There was an error sending the file.";
}
} else {
// Display the form
echo '<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="sender" placeholder="Your Email">
<input type="file" name="uploaded_file">
<input type="submit" name="submit" value="Send File">
</form>';
}
?>
</div>