ultramoo
asked on
PHP form with upload file feature attach to email
Hi,
I have some code already that uploads file types to a folder named 'files'. What I would like to do is change my code so that files can be attached to an email instead. I have two parts to the code the first part is the upload code and second part is the email code. (I'd also like to keep the form code on a separate html page and then submits to the php page.) Please see my code below:
I have some code already that uploads file types to a folder named 'files'. What I would like to do is change my code so that files can be attached to an email instead. I have two parts to the code the first part is the upload code and second part is the email code. (I'd also like to keep the form code on a separate html page and then submits to the php page.) Please see my code below:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>
<label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
<input type="submit" value="submit" />
<p>
</form>
</body>
</html>
PHP:
<?php
/*
Code to UPLOAD files to 'files' folder
*/
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.zip');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = 'files/';
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.
/*
Code to SEND form details to email address
*/
$sendto = "youremail@email.com.au";
$subject = "";
$name = $_POST['name'];
$headers = "From: $email\n";
$headers . "MIME-Version: 1.0\n"
. "Content-Transfer-Encoding: 7bit\n"
. "Content-type: text/html; charset = \"iso-8859-1\";\n\n";
// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
CONTACT ENQUIRY
-----------------------------------------------------------------------------
Name: $name
_______________________________________
End of Email
";
if (!trim($name)) {
echo "<p>Please go back and enter your Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}
// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headers)) {
echo "<br><br><p><b>Thank You $name</b></p><p>We will be in touch as soon as possible.</p>";
}
?>
ASKER
Thanks, I think its partly working. The file didn't actually attach it came through as code. When I received the email it came out something like this:
Content-Disposition: attachment;
filename="folio-abs.jpg"
/9j/4AAQSkZJRgABAQEAYABgAA D/4QBKRXhp ZgAASUkqAA gAAAADABoB BQABAAAAMg AAABsBBQAB
AAAAOgAAACgBAwABAAAAAgAAAA AAAAAAAEgA AAABAAAASA AAAAEA/9sA QwABAQEBAQ EBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQ EBAQEBAQEB AQEBAQEBAQ EBAQEBAQEB AQEBAQEBAQ EB/9sAQwEB
AQEBAQEBAQEBAQEBAQEBAQEBAQ EBAQEBAQEB AQEBAQEBAQ EBAQEBAQEB AQEBAQEBAQ EBAQEBAQEB
etc....
Also, it doesn't have to be an image file it could be any file extension.
Content-Disposition: attachment;
filename="folio-abs.jpg"
/9j/4AAQSkZJRgABAQEAYABgAA
AAAAOgAAACgBAwABAAAAAgAAAA
AQEBAQEBAQEBAQEBAQEBAQEBAQ
AQEBAQEBAQEBAQEBAQEBAQEBAQ
etc....
Also, it doesn't have to be an image file it could be any file extension.
see if it works now, it can be any file
<?php
/*
Code to UPLOAD files to 'files' folder
*/
$allowed_filetypes = array('.jpg','.gif','.bmp','.png','.zip');
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = 'files/';
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
$handle = fopen($_FILES['userfile']['tmp_name'], "rb");
$filecontents = fread($handle, filesize($_FILES['userfile']['tmp_name']));
fclose($handle);
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.
/*
Code to SEND form details to email address
*/
$sendto = "youremail@email.com.au";
$subject = "";
$name = $_POST['name'];
// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
CONTACT ENQUIRY
-----------------------------------------------------------------------------
Name: $name
_______________________________________
End of Email
";
$headers = "From: $email\n";
$headers .= "\nMIME-Version: 1.0\n"
. "Content-Type: multipart/alternative;\n"
. "charset=\"iso-8859-1\"\n"
. "Content-Transfer-Encoding: 7bit\n\n";
$headers .="Content-Type: ".$_FILES['userfile']['type'].";
name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=\"$filename\"
".chunk_split(base64_encode($filecontents));
if (!trim($name)) {
echo "<p>Please go back and enter your Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}
// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headers)) {
echo "<br><br><p><b>Thank You $name</b></p><p>We will be in touch as soon as possible.</p>";
}
?>
ASKER
No still not working. This time is doesn't send any email at all. The files are uploading fine into the servers though just not on the email.
if you do print $email; it must be there so that email has from address, check this please
ASKER
No the email didn't print. So is it my form or the php code?
let me do some testing to see if mail comes, i'll post back in few minutes
You might want to use php mailer. Its really easy to use.
http://phpmailer.codeworxtech.com/index.php?pg=tutorial
http://phpmailer.codeworxtech.com/index.php?pg=tutorial
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Just check it out and its works! Thanks for all your help!
Open in new window