Solved

PHP Form Mail with Attachment Problem

Posted on 2003-11-08
35
504 Views
Last Modified: 2012-05-04
I have a form on my website that I would like users to be able to fill out, attach a file, and email to me.

The problem I am currently having is that even when an email subject is entered the form says that one has not been entered.  Here is the code for my form (as you can tell there is also a config.php and a mail_module.php file- if you need to see the code for them please let me know:

<?
include "config.php";

?>
<form action="mail_module.php" method="post" ENCTYPE="multipart/form-data">
<p><table border=0 cellspacing=0 cellpadding=0>
<tr>
        <td><font size="2" face="Arial, Helvetica, sans-serif"><strong>Email: </strong></font></td>
<td bgcolor="#F0F0F0"><input class=fi type=text size=35 name=email_from value=<? if($mail_mode==0) echo "anonymous@anonymous-server.com";?>></td></tr>
<?
if($mail_mode==0) print("<tr><td bgcolor=\"#F0F0F0\" ><b>TO EMAIL</b></td>
<td bgcolor=\"#F0F0F0\"><input class=fi type=text size=35 name=email_to_anonymous><br><i>only for testing, please type your real email address</i></td></tr>");
?>
<tr>
        <td><b><font size="2" face="Arial, Helvetica, sans-serif">Subject: </font></b></td>
<td><input class=fi type=text size=15 name=email_subject value=<? if($mail_mode==0) echo "Testing of script";?>></td></tr>
</td></tr></table>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td>
<br><p><b><font size="2" face="Arial, Helvetica, sans-serif">Questions, comments, concerns, or general feedback:</font></b>
<br><textarea class=fi name=real_email_message cols=45 rows=8 onclick=createCaret(this); onselect=createCaret(this);>
<? if($mail_mode==0) echo "Test message

With blank lines and <b>HTML</b> tags
<hr>

";?>
</textarea></p>
<?
print("
   <p>Attachment: <input class=fi type=\"file\" size=32 name=\"upfile\"><br>
   <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$max_size\">
   <br><b>Note:</b> We do <b>NOT</b> accept these file types: ");
   print(str_replace("|",",",$banned_ext));
   print(". The maximum file size is <b>$max_size</b> Kb");
?>

<p><input class=fb type="submit" value=">> Send email">  <input class=fb type=reset>
<font size=1 color=gray>[MODE :
<?
if($mail_mode==0) echo "Anonymous HTML form mail with attachment file";
else if($mail_mode==1) echo "HTML form mail with attachment file";
?>
]</font>
</td></tr></table>
</form>

Thanks in advance and please be as specific as possible in your responses as this is my first experience with PHP.

-Scott
0
Comment
Question by:Gobernaster
  • 19
  • 15
35 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9709677
need to see mail_module.php really

loz
0
 

Author Comment

by:Gobernaster
ID: 9710205
mail_module.php:

<?
include "config.php";
?>
<link rel=stylesheet href="../style/formstyle2.css">
<?php  
//----------------------------------------------
function checkfileatt($fname,$fsize){
  global $max_size;
  global $banned_ext;
  $err="No file attached";
  //Checking file type in or out of banned file extensions list
    $pos1=strrchr($fname,".");
    $ftype=str_replace(".","",$pos1);
    $blist=explode("|",$banned_ext);
    for($i=0;$i<sizeof($blist)-1;$i++){
      if($ftype==$blist[$i]) $err="ERROR: Your file extension (<b>*.$ftype</b>) is not be accepted.  Please click your browser's back button and submit an acceptable file.";
    }
  //Check file size
    if(round($fsize/1024)>$max_size) $err="ERROR: Your file size (<b>" .round($fsize/1024) ."</b> Kb) is too large. We only accept <b>$max_size</b> Kb.  Please click your browser's back button and send a smaller file.";
  //Return the value
  return $err;
}

function formathtml($text){
  $text=stripslashes($text);
  $text=str_replace("\r\n","<BR>",$text);
  $text=str_replace("\n","<BR>",$text);
return $text;
}

function checkemail($email){
  if(!eregi("^[A-za-z0-9\_\.-]+@[A-za-z0-9\_\.-]+.[A-za-z0-9\_-]+.*",$email) || empty($email))
  return FALSE;
  else return TRUE;
}
//----------------------------------------------

    if($mail_mode==0) $email_to=$email_to_anonymous;
    else $email_to=$my_email;
   
    if($version=="demo") {
          $email_from="anonymous@anonymous-server.com";
          $real_email_message.="<br>------------<br>Demo email sent from <a href=http://obiewebsite.sourceforge.net target=_blank>http://ObieWebsite.SourceForge.Net</a><br>Get more PHP and Javascripts for FREE!<br>";
    }

    $kt=0;
    //Checking valid email address
    if(!checkemail($email_to)) $kt=1;
    if(!checkemail($email_from)) $kt=1;
    if(empty($email_subject)) $kt=2;
   
    if($kt==1) print("<p>ERROR: Email address (From email or To email) is not correct.  Please click your browser's back button and enter your email address.</p>");
    else if($kt==2) print("<p>ERROR: Your email subject is blank.  Please click your browser's back button and enter a subject.</p>");
   
    if($kt==0){

                //BEGIN //----------------------------------------------
                      $email_message=formathtml($real_email_message);
                      $chked=0;
                      if ($upfile == "none" || $upfile_size==0) $chked=1;
                      if (checkfileatt($upfile_name,$upfile_size)!="No file attached") $chked=1;
                      if($chked==0) {
                            copy($upfile, $upfile_name);
                        $fileatt=$upfile_name;
                        $fileatt_type=$upfile_type;
                        $fileatt_name=$upfile_name;
                        $file = fopen($fileatt,'rb');
                        $data = fread($file,filesize($fileatt));
                        fclose($file);
                      }
                      $headers = "From: ".$email_from;

                      $semi_rand = md5(time());
                      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

                      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
                      $email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n";

                      $data = chunk_split(base64_encode($data));

                      $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n";
                      $sending_ok = @mail($email_to, $email_subject, $email_message, $headers);

                      if($chked==0) unlink($upfile_name);

                //END mode 3 and 0//----------------------------------------------

                //Print the email result
                if($sending_ok){
                  print("<title>Sending successful to $email_to</title><div align=center><p>Your email was sent successfully!!</p>");
                }
                else print("<title>Sending failed to $email_to</title><div align=center><p><b>ERROR in mail server</b>Sorry, your email could not be sent at this time.</p>");

                print("
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>
                     <p><u><b>Sent to email</b></u><br>
                     $email_to
                     <p><u><b>From email</b></u><br>
                     $email_from
                     <p><u><b>Email subject</b></u><br>
                     $email_subject
                    <p><u><b>Message</b></u><p>
                    <table border=0 width=770 cellspacing=1 style=\"border: 1 dotted #AED0F2;\"><tr><td>");
                print(formathtml($real_email_message));
                 print("</td></tr></table><p><br></p>
                    <p align=center><a href=../index.htm title=\"Come back to my site\">Thank you!! Back to my site</a>  or  <a href=index.php>send me another email.</a></p>
                    <p><br></p>
                    </td></tr></table>
                   
                    <p><b>Attachment status</b>
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>");
                    if($mail_mode==0 || $mail_mode==1) {
                      if($chked==0) print("<p><b>$upfile_name</b><br>Filesize: " .round($upfile_size/1024) ." Kb | Filetype : $upfile_type");
                      else print("<p>" .checkfileatt($upfile_name,$upfile_size) ."</p>");
                    }
    }
?>

Thanks!

-Scott
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9710373
hi,

your script appears to be assuming register_globals is on when it is most probably not. you can check by putting <? phpinfo(); ?> in a script and looking for the value of register_globals in it. register_globals puts values from forms, sessions, cookies, urls etc. into direct variable names, so an input field with name="myformfield" would come out as $myformfield on the next page. however if register globals is off, it will be put into the variable $_POST as part of an array, so it would be accessed through $_POST["myformfield"] (assuming the method of the form is POST). the reason for having register_globals off is that it's a security risk if values can come from any source. for example if your login system relied on a session variable called logged_in being present, a user could go to the page.php?logged_in=1 and this would bluff being authenticated

so in a nutshell, what you need to do is replace any instances of variables that come from your form with the register_globals equivalent. values from your POST form becomes $_POST["fieldname"] and your file upload variables become $_FILES["fieldname"]

so email_to becomes $_POST["email_to"], email_to_anonymous becomes $_POST["email_to_anonymous"], $email_subject value becomes $_POST["email_subject value"] etc. info about your upload comes from $_FILES["upfile"]["tmp_name"], $_FILES["upfile"]["name"] etc

if you're using php 4.1.0 or earlier, you need to use the old forms of these variables, which are HTTP_POST_VARS["fieldname"] and $HTTP_POST_FILES["fieldname"]

to read more, go here: http://uk2.php.net/variables.predefined

loz
0
 

Author Comment

by:Gobernaster
ID: 9710491
loz,

You are correct- I checked and register_globals is off.  The PHP version is 4.2.2.

I changed my form to this:

<?
include "config.php";

?>
<form action="mail_module.php" method="post" ENCTYPE="multipart/form-data">
<p><table border=0 cellspacing=0 cellpadding=0>
<tr>
        <td><font size="2" face="Arial, Helvetica, sans-serif"><strong>Email: </strong></font></td>
<td bgcolor="#F0F0F0"><input class=fi type=text size=35 name=$_POST["email_from value"]=<? if($mail_mode==0) echo "anonymous@anonymous-server.com";?>></td></tr>
<?
if($mail_mode==0) print("<tr><td bgcolor=\"#F0F0F0\" ><b>TO EMAIL</b></td>
<td bgcolor=\"#F0F0F0\"><input class=fi type=text size=35 name=$_POST["email_to_anonymous"]><br><i>only for testing, please type your real email address</i></td></tr>");
?>
<tr>
        <td><b><font size="2" face="Arial, Helvetica, sans-serif">Subject: </font></b></td>
<td><input class=fi type=text size=15 name=$_POST["email_subject value"]=<? if($mail_mode==0) echo "Testing of script";?>></td></tr>
</td></tr></table>
<table border=0 cellspacing=0 cellpadding=0>
<tr><td>
<br><p><b><font size="2" face="Arial, Helvetica, sans-serif">Questions, comments, concerns, or general feedback:</font></b>
<br><textarea class=fi name=$_POST["real_email_message"] cols=45 rows=8 onclick=createCaret(this); onselect=createCaret(this);>
<? if($mail_mode==0) echo "Test message

With blank lines and <b>HTML</b> tags
<hr>

";?>
</textarea></p>
<?
print("
   <p>Attachment: <input class=fi type=\"file\" size=32 name=$_FILES["upfile"]["name"]><br>
   <input type=\"hidden\" name=$_FILES["MAX_FILE_SIZE"] value=\"$max_size\">
   <br><b>Note:</b> We do <b>NOT</b> accept these file types: ");
   print(str_replace("|",",",$banned_ext));
   print(". The maximum file size is <b>$max_size</b> Kb");
?>

<p><input class=fb type="submit" value=">> Send email">  <input class=fb type=reset>
<font size=1 color=gray>[MODE :
<?
if($mail_mode==0) echo "Anonymous HTML form mail with attachment file";
else if($mail_mode==1) echo "HTML form mail with attachment file";
?>
]</font>
</td></tr></table>
</form>

but now nothing shows up when I go to the PHP page.  I am sure it is something stupid that I did, but please bear with me as I have never dealt with PHP before.

Do I need to make any changes to the config or mail_module files?

Thanks!!

0
 
LVL 13

Expert Comment

by:lozloz
ID: 9710525
hi,

you don't need to change your form at all - it's only your mail_module page which needs modification so if you put the form back to how it was and replace the instances of variables which come from the form with the ones i gave you, it should work properly

tell me if you have any more problems

cheers,

loz
0
 

Author Comment

by:Gobernaster
ID: 9710690
Thanks loz,

I removed the changes from my form and made the changes to the mail_module file but I am still having some problems.  Now it says the email address is not correct.

Again I apologize for being such a pain, but I really have no idea what I am doing (which I am sure you already figured by now).

<?

include "config.php";
?>
<link rel=stylesheet href="../style/formstyle2.css">
<?php  
//----------------------------------------------
function checkfileatt($fname,$fsize){
  global $max_size;
  global $banned_ext;
  $err="No file attached";
  //Checking file type in or out of banned file extensions list
    $pos1=strrchr($fname,".");
    $ftype=str_replace(".","",$pos1);
    $blist=explode("|",$banned_ext);
    for($i=0;$i<sizeof($blist)-1;$i++){
      if($ftype==$blist[$i]) $err="ERROR: Your file extension (<b>*.$ftype</b>) is not be accepted.  Please click your browser's back button and submit an acceptable file.";
    }
  //Check file size
    if(round($fsize/1024)>$max_size) $err="ERROR: Your file size (<b>" .round($fsize/1024) ."</b> Kb) is too large. We only accept <b>$max_size</b> Kb.  Please click your browser's back button and send a smaller file.";
  //Return the value
  return $err;
}

function formathtml($text){
  $text=stripslashes($text);
  $text=str_replace("\r\n","<BR>",$text);
  $text=str_replace("\n","<BR>",$text);
return $text;
}

function checkemail($email){
  if(!eregi("^[A-za-z0-9\_\.-]+@[A-za-z0-9\_\.-]+.[A-za-z0-9\_-]+.*",$email) || empty($email))
  return FALSE;
  else return TRUE;
}
//----------------------------------------------

    if($mail_mode==0) $_POST["email_to"]=$_POST["email_to_anonymous"];
    else $_POST["email_to"]=$_POST["my_email"];
   
    if($version=="demo") {
          $_POST["email_from"]=$_POST["anonymous@anonymous-server.com"];
          $_POST["real_email_message"].="<br>------------<br>Demo email sent from <a href=http://obiewebsite.sourceforge.net target=_blank>http://ObieWebsite.SourceForge.Net</a><br>Get more PHP and Javascripts for FREE!<br>";
    }

    $kt=0;
    //Checking valid email address
    if(!checkemail($_POST["email_to"])) $kt=1;
    if(!checkemail($_POST["email_from"])) $kt=1;
    if(empty($_POST["email_subject"])) $kt=2;
   
    if($kt==1) print("<p>ERROR: Email address (From email or To email) is not correct.  Please click your browser's back button and enter your email address.</p>");
    else if($kt==2) print("<p>ERROR: Your email subject is blank.  Please click your browser's back button and enter a subject.</p>");
   
    if($kt==0){

                //BEGIN //----------------------------------------------
                      $email_message=formathtml($real_email_message);
                      $chked=0;
                      if ($upfile == "none" || $upfile_size==0) $chked=1;
                      if (checkfileatt($upfile_name,$upfile_size)!="No file attached") $chked=1;
                      if($chked==0) {
                            copy($upfile, $upfile_name);
                        $fileatt=$upfile_name;
                        $fileatt_type=$upfile_type;
                        $fileatt_name=$upfile_name;
                        $file = fopen($fileatt,'rb');
                        $data = fread($file,filesize($fileatt));
                        fclose($file);
                      }
                      $headers = "From: ".$_POST["email_from"];

                      $semi_rand = md5(time());
                      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

                      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
                      $email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n";

                      $data = chunk_split(base64_encode($data));

                      $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n";
                      $sending_ok = @mail($_POST["email_to"], $_POST["email_subject"], $_POST["email_message"], $_POST["headers"]);

                      if($chked==0) unlink($upfile_name);

                //END mode 3 and 0//----------------------------------------------

                //Print the email result
                if($sending_ok){
                  print("<title>Sending successful to $email_to</title><div align=center><p>Your email was sent successfully!!</p>");
                }
                else print("<title>Sending failed to $email_to</title><div align=center><p><b>ERROR in mail server</b>Sorry, your email could not be sent at this time.</p>");

                print("
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>
                     <p><u><b>Sent to email</b></u><br>
                     $email_to
                     <p><u><b>From email</b></u><br>
                     $email_from
                     <p><u><b>Email subject</b></u><br>
                     $email_subject
                    <p><u><b>Message</b></u><p>
                    <table border=0 width=770 cellspacing=1 style=\"border: 1 dotted #AED0F2;\"><tr><td>");
                print(formathtml($real_email_message));
                 print("</td></tr></table><p><br></p>
                    <p align=center><a href=../index.htm title=\"Come back to my site\">Thank you!! Back to my site</a>  or  <a href=index.php>send me another email.</a></p>
                    <p><br></p>
                    </td></tr></table>
                   
                    <p><b>Attachment status</b>
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>");
                    if($mail_mode==0 || $mail_mode==1) {
                      if($chked==0) print("<p><b>$upfile_name</b><br>Filesize: " .round($upfile_size/1024) ." Kb | Filetype : $upfile_type");
                      else print("<p>" .checkfileatt($upfile_name,$upfile_size) ."</p>");
                    }
    }
?>

Thank you for your patience!
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9710931
i assume this is your error message: ERROR: Email address (From email or To email) is not correct.  Please click your browser's back button and enter your email address.

which occurs due to this:

    if(!checkemail($_POST["email_to"])) $kt=1;
    if(!checkemail($_POST["email_from"])) $kt=1;

which leads me back to:

    if($mail_mode==0) $_POST["email_to"]=$_POST["email_to_anonymous"];
    else $_POST["email_to"]=$_POST["my_email"];

this should actually be

    if($mail_mode==0) $email_to=$_POST["email_to_anonymous"];
    else $email_to=$my_email

i'm guessing $mail_mode and $my_email are set in config.php..

there might be some more problems anyway

loz
0
 

Author Comment

by:Gobernaster
ID: 9710943
Thanks!  I replaced this:

 if($mail_mode==0) $_POST["email_to"]=$_POST["email_to_anonymous"];
    else $_POST["email_to"]=$_POST["my_email"];

with this:

    if($mail_mode==0) $email_to=$_POST["email_to_anonymous"];
    else $email_to=$my_email

I no longer receive the error "Email address (From email or To email) is not correct.  Please click your browser's back button and enter your email address," but now the mail_module.php page shows up blank after filling out the form and clicking submit.

And yes, mail_mode and $my_email are set in config.php.
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9710974
                     $sending_ok = @mail($_POST["email_to"], $_POST["email_subject"], $_POST["email_message"], $_POST["headers"]);

this needs to be changed to

$sending_ok = @mail($email_to, $_POST["email_subject"], $email_message, $headers);

i don't imagine this will fix it but it's worth a go, and needs changing anyway.. what would probably be best is to debug by adding print "1"; print "2"; etc. after certain sections of code so you can find the spot where the code stops working

loz
0
 

Author Comment

by:Gobernaster
ID: 9711041
Thanks loz, I replaced:

$sending_ok = @mail($_POST["email_to"], $_POST["email_subject"], $_POST["email_message"], $_POST["headers"]);

with:

$sending_ok = @mail($email_to, $_POST["email_subject"], $email_message, $headers);

but as you had assume it still isn't fixed- I still end up at a blank mail_module page after filling out the form and clicking submit.

after what sections of code would you suggest I add print "1"; print "2"?  (again, sorry, but I don't even possess a basic understanding of PHP and am totally lost- as I am sure you can tell, but really wanted to get this form working)

Thanks so much!
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9711057
adding that where each double line break is would probably work ok, would help to narrow it down, then you can tell me which is the last number it prints out and where the number is added in

cheers,

loz
0
 

Author Comment

by:Gobernaster
ID: 9711095
I tried adding print "1"; print "2"; etc. into the mail_module code and the page still loads blank...

Are there certain areas or ways to add these print commands that I don't know?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9711109
can you paste all the code from mail_module.php here

loz
0
 

Author Comment

by:Gobernaster
ID: 9711217
do you want me to include all the html too?

would it make it easier for you if I sent the file to you?

Thanks,
Scott
0
 

Author Comment

by:Gobernaster
ID: 9711238
Here's mail_module:

<?
include "config.php";
?>
<link rel=stylesheet href="../style/formstyle2.css">
<?php  
//----------------------------------------------
function checkfileatt($fname,$fsize){
  global $max_size;
  global $banned_ext;
  $err="No file attached";
  //Checking file type in or out of banned file extensions list
    $pos1=strrchr($fname,".");
    $ftype=str_replace(".","",$pos1);
    $blist=explode("|",$banned_ext);
    for($i=0;$i<sizeof($blist)-1;$i++){
      if($ftype==$blist[$i]) $err="ERROR: Your file extension (<b>*.$ftype</b>) is not be accepted.  Please click your browser's back button and submit an acceptable file.";
    }
  //Check file size
    if(round($fsize/1024)>$max_size) $err="ERROR: Your file size (<b>" .round($fsize/1024) ."</b> Kb) is too large. We only accept <b>$max_size</b> Kb.  Please click your browser's back button and send a smaller file.";
  //Return the value
  return $err;
}

function formathtml($text){
  $text=stripslashes($text);
  $text=str_replace("\r\n","<BR>",$text);
  $text=str_replace("\n","<BR>",$text);
return $text;
}

function checkemail($email){
  if(!eregi("^[A-za-z0-9\_\.-]+@[A-za-z0-9\_\.-]+.[A-za-z0-9\_-]+.*",$email) || empty($email))
  return FALSE;
  else return TRUE;
}
//----------------------------------------------

    if($mail_mode==0) $email_to=$_POST["email_to_anonymous"];
    else $email_to=$my_email
   
    if($version=="demo") {
          $_POST["email_from"]=$_POST["anonymous@anonymous-server.com"];
          $_POST["real_email_message"].="<br>------------<br>Demo email sent from <a href=http://obiewebsite.sourceforge.net target=_blank>http://ObieWebsite.SourceForge.Net</a><br>Get more PHP and Javascripts for FREE!<br>";
    }
      
    $kt=0;
    //Checking valid email address
    if(!checkemail($_POST["email_to"])) $kt=1;
    if(!checkemail($_POST["email_from"])) $kt=1;
    if(empty($_POST["email_subject"])) $kt=2;
   
    if($kt==1) print("<p>ERROR: Email address (From email or To email) is not correct.  Please click your browser's back button and enter your email address.</p>");
    else if($kt==2) print("<p>ERROR: Your email subject is blank.  Please click your browser's back button and enter a subject.</p>");
   
    if($kt==0){
      
                //BEGIN //----------------------------------------------
                      $email_message=formathtml($real_email_message);
                      $chked=0;
                      if ($upfile == "none" || $upfile_size==0) $chked=1;
                      if (checkfileatt($upfile_name,$upfile_size)!="No file attached") $chked=1;
                      if($chked==0) {
                            copy($upfile, $upfile_name);
                        $fileatt=$upfile_name;
                        $fileatt_type=$upfile_type;
                        $fileatt_name=$upfile_name;
                        $file = fopen($fileatt,'rb');
                        $data = fread($file,filesize($fileatt));
                        fclose($file);
                      }
                                
                      $headers = "From: ".$_POST["email_from"];

                      $semi_rand = md5(time());
                      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

                      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
                      $email_message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message . "\n\n";

                      $data = chunk_split(base64_encode($data));

                      $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n";
                      $sending_ok = @mail($email_to, $_POST["email_subject"], $email_message, $headers);

                      if($chked==0) unlink($upfile_name);

                //END mode 3 and 0//----------------------------------------------

                //Print the email result
                if($sending_ok){
                  print("<title>Sending successful to $email_to</title><div align=center><p>Your email was sent successfully!!</p>");
                }
                        
                else print("<title>Sending failed to $email_to</title><div align=center><p><b>ERROR in mail server</b>Sorry, your email could not be sent at this time.</p>");

                print("
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>
                     <p><u><b>Sent to email</b></u><br>
                     $email_to
                     <p><u><b>From email</b></u><br>
                     $email_from
                     <p><u><b>Email subject</b></u><br>
                     $email_subject
                    <p><u><b>Message</b></u><p>
                    <table border=0 width=770 cellspacing=1 style=\"border: 1 dotted #AED0F2;\"><tr><td>");
                print(formathtml($real_email_message));
                 print("</td></tr></table><p><br></p>
                    <p align=center><a href=../index.htm title=\"Come back to my site\">Thank you!! Back to my site</a>  or  <a href=index.php>send me another email.</a></p>
                    <p><br></p>
                    </td></tr></table>
                   
                    <p><b>Attachment status</b>
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>");
                    if($mail_mode==0 || $mail_mode==1) {
                      if($chked==0) print("<p><b>$upfile_name</b><br>Filesize: " .round($upfile_size/1024) ." Kb | Filetype : $upfile_type");
                      else print("<p>" .checkfileatt($upfile_name,$upfile_size) ."</p>");
                    }
    }
?>
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9711451
   if($mail_mode==0) $email_to=$_POST["email_to_anonymous"];
    else $email_to=$my_email

the 2nd line here is missing a semi colon at the end:

    if($mail_mode==0) $email_to=$_POST["email_to_anonymous"];
    else $email_to=$my_email;

if that doesn't work i'll have to give you some more help tomorrow

cheers,

loz
0
 

Author Comment

by:Gobernaster
ID: 9711484
Now the mail_module pages loads as it should, but I get this error "ERROR: Email address (From email or To email) is not correct. Please click your browser's back button and enter your email address."

Thanks for all of your help today and I'll talk to you tomorrow!
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Gobernaster
ID: 9736501
loz,

hate to bother you any more, but are you available for some additional guidance?

-Scott
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9736537
whoops, sorry must have missed your last message..

change if(!checkemail($_POST["email_to"])) $kt=1; to if(!checkemail($email_to)) $kt=1;

sorry again

loz
0
 

Author Comment

by:Gobernaster
ID: 9736625
No big deal, thanks loz!

I no longer receive the "ERROR: Email address (From email or To email) is not correct. Please click your browser's back button and enter your email address," but now receive this error after clicking Send email, "ERROR in mail serverSorry, your email could not be sent at this time."

Do I need to make similar changes to:

if(!checkemail($_POST["email_from"])) $kt=1;
if(empty($_POST["email_subject"])) $kt=2;

or do these lines remain the same and the problem lies elsewhere?
 
Thanks!!!
0
 

Author Comment

by:Gobernaster
ID: 9744777
Any idea on what is wrong now (please see previous post for details)?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9744859
heh, missed it again.. sorry i was working all last night

                      $email_message=formathtml($real_email_message);
that should be                      

$email_message=formathtml($_POST["real_email_message"]);

if it's coming through from the form. remove the @ from the following line:

$sending_ok = @mail($email_to, $_POST["email_subject"], $email_message, $headers);

then we'll go from there

loz
0
 

Author Comment

by:Gobernaster
ID: 9744995
Thanks loz I changed this:

"$email_message=formathtml($real_email_message);"

to

"$email_message=formathtml($_POST["real_email_message"]);"

but I still get "ERROR in mail server. Sorry, your email could not be sent at this time."  I tried it both with the @ and without it and had the same result.

Also, the "From email," "Email subject," and "Message" are all blank on the mail_module page after submitting the form.  The only field that shows up filled in is "Sent to email."
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9745044
hi,

they're blank in the form because they are the wrong variables - you need to have $_POST in front of email_from and email_subject, same for real_email_message

just before: $sending_ok = mail($email_to, $_POST["email_subject"], $email_message, $headers);

could you add: print "$email_to - " . $_POST["email_subject"] . " - $email_message - $headers";

and give me the output?

thanks,

loz
0
 

Author Comment

by:Gobernaster
ID: 9745194
Thanks loz,

Here's the output,

"gobernaster@hotmail.com - site - This is a multi-part message in MIME format. --==Multipart_Boundary_x681e833a69e0d1565eaa64191ae5c310x Content-Type:text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_x681e833a69e0d1565eaa64191ae5c310x Content-Type: ; name="" Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 --==Multipart_Boundary_x681e833a69e0d1565eaa64191ae5c310x-- - From: gobernaster@hotmail.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x681e833a69e0d1565eaa64191ae5c310x"
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9745276
can you make a seperate script and run it, filling in your email address for the first argument of each function:

<?
mail("myemail@email.com", "My Subject", "Line 1\nLine 2\nLine 3");
mail("myemail@email.com", "the subject", $message,
     "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."X-Mailer: PHP/" . phpversion());
?>

tell me if you get both emails through. be back tomorrow to try and help some more

loz
0
 

Author Comment

by:Gobernaster
ID: 9745285
I added the $_Post in front of email_from, email_subject, and real_email_message, but now the mail_module page does not load (it is completely blank after submitting the form).  Below is the new code I am using...
               
                    print("
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>
                     <p><u><b>Sent to email</b></u><br>
                     $email_to
                     <p><u><b>From email</b></u><br>
                     $_POST["email_from"]
                     <p><u><b>Email subject</b></u><br>
                     $_POST["email_subject"]
                    <p><u><b>Message</b></u><p>
                    <table border=0 width=770 cellspacing=1 style=\"border: 1 dotted #AED0F2;\"><tr><td>");
                print(formathtml($_POST["real_email_message"]));

-Scott
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9745297
the double quotes cause it to break

                    print("
                    <p><table border=0 width=770 cellspacing=1 bgcolor=black><tr><td bgcolor=gray>
                     <p><u><b>Sent to email</b></u><br>
                     $email_to
                     <p><u><b>From email</b></u><br>" .
                     $_POST["email_from"] . "
                     <p><u><b>Email subject</b></u><br>" .
                     $_POST["email_subject"] . "
                    <p><u><b>Message</b></u><p>
                    <table border=0 width=770 cellspacing=1 style=\"border: 1 dotted #AED0F2;\"><tr><td>");
                print(formathtml($_POST["real_email_message"]));

loz
0
 

Author Comment

by:Gobernaster
ID: 9752507
Thanks loz, I applied the code from your last post and now the email from, email subject, and email message fields appear filled in on the mail_module page (I know you knew this would work, but just wanted to update you)!!

Am I placing this:

<?
mail("myemail@email.com", "My Subject", "Line 1\nLine 2\nLine 3");
mail("myemail@email.com", "the subject", $message,
     "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."X-Mailer: PHP/" . phpversion());
?>

in the same mail_module file or in a completely separate file?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9752628
seperate file, run it once, simply to test if the mailing on your server is working (remember to replace the address in there)

loz
0
 

Author Comment

by:Gobernaster
ID: 9757455
loz,

Sorry that I didn't have time to respond yesterday, but I was extremely busy with other stuff and couldn't find the time to get on a computer.

I added this to a new page and uploaded it to my server:

<?
mail("gobernaster@hotmail.com", "Mail Server Test", "Line 1\nLine 2\nLine 3");
mail("myemail@email.com", "the subject", $message,
     "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."X-Mailer: PHP/" . phpversion());
?>

I then visited this page on my server via IE and later checked my email, but never received anything.  I was not clear on where I had to enter my email address so I also tried:

<?
mail("gobernaster@hotmail.com", "Mail Server Test", "Line 1\nLine 2\nLine 3");
mail("gobernaster@hotmail.com", "the subject", $message,
     "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
    ."X-Mailer: PHP/" . phpversion());
?>

Am I doing something wrong or does this mean there is a problem with the mailing on my server?

Thanks!

-Scott
0
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
ID: 9757473
hi,

to me it seems something is wrong with your php, the second code listing you have there is what i intended but they both should work.. where are you being hosted? maybe you should get in contact with your server admin about this? tell me how it goes

loz
0
 

Author Comment

by:Gobernaster
ID: 9759023
Hey loz,

I am using FSIwebs to host this site.  I will contact my admin over there to try to work through this and will let you know how I make out.

Thanks for all of your help so far and I'll keep you posted!!

-Scott
0
 

Author Comment

by:Gobernaster
ID: 12586613
Sorry loz this fell to the backburner and is still an issue.  You did a significant amount of work to help me out and I will reward you the points despite my lack of a solution.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now