reddyfire
asked on
PHP mail attachment form
I got a mail form now that will allow for users filling out the form to submit a message with an attachment. Now I want to it to display an error message if the the name field, the message field, or the email field are left blank.
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$path_info = pathinfo($_FILES['filename']['name']);
if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
die ("can not upload this type of file");
}
// we'll begin by assigning the To address and message subject
$to="reddyfire@gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$nimi = $_REQUEST['nimi'];
$toimi = $_REQUEST['toimi'];
$Puhelinnumero = $_REQUEST['Puhelinnumero'];
$sahkoposti = $_REQUEST['fromemail'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = stripslashes($_POST['message'])."$message";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "OK, sent.";
else
echo "Failed to send";
}
}
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
ASKER
I tried your solution but I got the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/guanajuato/public_ht ml/Guanaju ato/sendma il.php:2) in /home/guanajuato/public_ht ml/Guanaju ato/sendma il.php on line 88
Warning: Cannot modify header information - headers already sent by (output started at /home/guanajuato/public_ht
check the source code of the page containing the header error ... and post it
ASKER
Here is the code.
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$path_info = pathinfo($_FILES['filename']['name']);
if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
die ("can not upload this type of file");
}
// we'll begin by assigning the To address and message subject
$to="reddyfire@gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$nimi = $_REQUEST['nimi'];
$toimi = $_REQUEST['toimi'];
$Puhelinnumero = $_REQUEST['Puhelinnumero'];
$sahkoposti = $_REQUEST['fromemail'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = stripslashes($_POST['message'])."$message";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
header("Location: thankyou.html");
else
header("Location: sorry.html");
}
die;
}
?>
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
i tried youes and babuno5's as weel
babuno5 code worked fine for me ..... try it again ......
babuno5 code worked fine for me ..... try it again ......
ASKER
I tested it again and still didn't work. If you look at the 2 codes carefully you will notice that babuno5's code is different than mine and does not contain the message box that allows for comments.
first f it worked for you and you only missing the comment box we can add it .... we need a working code ... then we can add the extra code we need
ASKER
Ok the working code is located in the original post. To save you some time from going back up and looking at it here it is.
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$path_info = pathinfo($_FILES['filename']['name']);
if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
die ("can not upload this type of file");
}
// we'll begin by assigning the To address and message subject
$to="reddyfire@gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$nimi = $_REQUEST['nimi'];
$toimi = $_REQUEST['toimi'];
$Puhelinnumero = $_REQUEST['Puhelinnumero'];
$sahkoposti = $_REQUEST['fromemail'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = stripslashes($_POST['message'])."$message";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "OK, sent.";
else
echo "Failed to send";
}
}
?>
<p>Send an e-mail with an attachment:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
i added t script to check the form before submit
check and waiting for your updates
check and waiting for your updates
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$path_info = pathinfo($_FILES['filename']['name']);
if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
die ("can not upload this type of file");
}
// we'll begin by assigning the To address and message subject
$to="reddyfire@gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$nimi = $_REQUEST['nimi'];
$toimi = $_REQUEST['toimi'];
$Puhelinnumero = $_REQUEST['Puhelinnumero'];
$sahkoposti = $_REQUEST['fromemail'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = stripslashes($_POST['message'])."$message";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "OK, sent.";
else
echo "Failed to send";
}
}
?>
<script language="JavaScript">
function checkform() {
if (form.fromname.value == "" || form.fromemail.value == "" || form.message.value == "") {
alert("you error message here");
} else {
form.submit();
}
}
</script>
<p>Send an e-mail with an attachment:</p>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
</form>
ASKER
Sorry that didn't work for me. Now when I click on the submit button it wont even do anything.
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$path_info = pathinfo($_FILES['filename']['name']);
if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
die ("can not upload this type of file");
}
// we'll begin by assigning the To address and message subject
$to="reddyfire@gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$nimi = $_REQUEST['nimi'];
$comments = $_REQUEST['comments'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = stripslashes($_POST['comments'])."$comments";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Submission sent.";
else
echo "Failed to send";
}
}
?>
<script language="JavaScript">
function checkform() {
if (form.fromname.value == "" || form.fromemail.value == "" || form.comments.value == "") {
alert("you error message here");
} else
{
form.submit();
}
}
</script>
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>Message:<br> <textarea name="comments" rows="3" cols="40"></textarea></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
</form>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I renamed all the forms to form1 and it submits stuff now but even if I leave any of the forms blank it will still submit without giving any kind of error message.
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
$path_info = pathinfo($_FILES['filename']['name']);
if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
die ("can not upload this type of file");
}
// we'll begin by assigning the To address and message subject
$to="reddyfire@gmail.com";
$subject="E-mail with attachment";
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$nimi = $_REQUEST['nimi'];
$comments = $_REQUEST['comments'];
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
$message = stripslashes($_POST['comments'])."$comments";
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers))
echo "Submission sent.";
else
echo "Failed to send";
}
}
?>
<script language="JavaScript">
function checkform() {
if (form1.fromname.value == "" || form1.fromemail.value == "" || form.1message.value == "") {
alert("you error message here");
} else
{
form1.submit();
}
}
</script>
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1">
<p>From name: <input type="text" name="fromname"></p>
<p>From e-mail: <input type="text" name="fromemail"></p>
<p>Message:<br> <textarea name="message" rows="3" cols="40"></textarea></p>
<p>File: <input type="file" name="filename"></p>
<p><input type="button" onclick=checkform() name="Submit" value="Submit"></p>
</form>
on line 97 your last condition is wrong
if (form1.fromname.value == "" || form1.fromemail.value == "" || form.1message.value == "") {
it is
form.1message.value
but it must be
form1.message.value
if (form1.fromname.value == "" || form1.fromemail.value == "" || form.1message.value == "") {
it is
form.1message.value
but it must be
form1.message.value
ASKER
Thank you this really helped.
ASKER
Thanks you for your help. I was finally able to get it working.
hello, "agamal"
How to separate HTML and PHP code as two files and adding extra fields like phone, address etc.
Thanks
Pandu
How to separate HTML and PHP code as two files and adding extra fields like phone, address etc.
Thanks
Pandu
Open in new window