Solved

PHP script problems on browsing  files and then sending to email

Posted on 2004-08-09
8
211 Views
Last Modified: 2008-03-17
Hi Guys
I am having problems creating a PHP script for uploading files and sending it my email. I have created a basic form and have pasted the .htm along with the message. I am new to PHP and have tried but in vain. I am sending my .php and .htm along with this message. If someone can help me in this regard, I will appreciate it.
Thanks
rskurane

.htm code

 <table width="444" border="0">
      <tr>
        <td width="438" height="355">
           <form name="form" onsubmit= "return validateform()"  method="post"  action="send_college2.php">
          <p><span class="style9">Please enter your full name:</span>                
            <INPUT size=30 name="FromName">
                <br>
                <br>
                <span class="style9">Please enter your email address:</span>
                <INPUT size=30 name="FromEmail">
          </p>
              <p><span class="style9">Affiliation</span>:
                  <INPUT size=30 name="FromAffiliation">
              </p>
              <p class="style9">Please select a lab:&nbsp;&nbsp;
                <select name="menu1" size="1">
                  <option >Options: Select a laboratory</option>
                  <option value="ELET 1100">ELET 1100: Electrical Circuits Laboratory I</option valu>
              <option value="ELET 1101">ELET 1101: Electrical Circuits Laboratory II</option>
              <option value="ELET 2103">ELET 2103: Digital Circuits and Systems Laboratory</option>
              <option value="ELET 2105">ELET 2105: Discrete and IC Semiconductor Circuit Laboratory</option>
              <option value="ELET 3105">ELET 3105: Microprocessor Architecture and Systems Laboratory</option>
              <option value="ELET 3303">ELET 3303: Operational Amplifier Applications Laboratory</option>
              <option value="ELET 3102">ELET 3102: Communications Circuits Laboratory</option>
              <option value="ELET 4108">ELET 4108: Senior Project</option>
              <option value="ELET 4121">ELET 4121: Microcomputer Networks Laboratory</option>
               </select>
              </p>
              <p>          
           <span class="style9">Please enter your suggestions</span>
            <span class="style9">here:</span>
            <textarea name="FormText" cols="60" rows="8" id="FormText"></textarea>
                  <input name="filename"  type="file" size="32">

          </p>
          <table width="143" border="0" align="center">
            <tr>
              <td width="133">
      <input name="Formsubmit" type="submit" id="Formsubmit" value="Submit">
              <input name="Formreset" type="reset" id="Formreset" value="Reset">
                    &nbsp;</td>
            </tr>
          </table>
          <p>&nbsp;          </p>
        </form></td>
      </tr>
    </table>    <p>&nbsp;</p>



This is my .php

<html>
<title>Email has been sent</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<table width="280">
            <tr>
                  <td colspan="2">
            <script type="text/javascript" language="JavaScript1.2" src="file:///C|/Documents%20and%20Settings/rskurane/Desktop/js/sm_question_form.js">
      </script>
            </td>
                      </tr>
                        </table>
            
            <?php
                                          
            $bOK = true;
            if (empty($FromName))
            {
            echo "Please enter a valid name in the form <br>";
            $bOK = false;
            }
            if (empty($FromEmail))
            {
            echo "Please enter a valid email in the form <br>";
            $bOK = false;
            }
            if (empty($FormText))
            {
            echo "Please enter a valid question or comment in the form <br>";
            $bOK = false;
            }
            if (empty($FromAffiliation))
            {
            echo "Please enter a valid affiliation in the form <br>";
            $bOK = false;
            }
            if ($bOK == true)
            {
            $mail_to = "";
            $role_to = "";
            if ($menu1 == "1100")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                  else if ($menu1 == "1101")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                             else if ($menu1 == "2103")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                  else if ($menu1 == "2105")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                  else if ($menu1 == "3105")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                  else if ($menu1 == "3303")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                           else if ($menu1 == "3102")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                  else if ($menu1 == "4108")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                  else if ($menu1 == "4121")
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                                                                                                                                                                                          
                  else
                  {
                  $mail_to = "xyz";
                  $role_to = "";
                  }
                                                      
                  //echo "FromName is " . $FromName . "<br>";
            //echo "FromEmail is " . $FromEmail . "<br>
                  //echo "QuestionFor is " . $QuestionFor . "<br>" ;
                  //echo "Question is " . $Question . "<br>";
                                                
            $mail_headers = "From: $FromEmail\r\nReply-to: $FromEmail\r\n";
            $subject = "CLABS Website Comment from " . $FromName . " who is a " .$FromAffiliation. " and is providing ideas for " .$menu1. "( " . $FromEmail . " ) ";
            mail($mail_to, $subject, $FormText, $mail_headers);
                                                      
                        }
                                    
                  ?>

                  <center>
                  <?php
                  if ($bOK == true)
                  {
                  echo "<p>                        <b>Thank you for your comment or question. Your email has been sent to the " . $role_to . ". You should expect a reply within 2 working days. </b></p>";
                  }
                  else
                  {
                  echo "<p>Please click the <b>Back</b> button and fill in the missing fields and try again.</p>";
                  }
                  ?>
                        </center>
                                    
                        <p>&nbsp;
                                          
                        </p>            
</html>
0
Comment
Question by:rskurane
8 Comments
 
LVL 7

Expert Comment

by:zac_charles
ID: 11759088
Hi, im not at home and i dont have the php mail() function enabled here but i will check it out for you when i get home.

Zac Charles
0
 
LVL 2

Accepted Solution

by:
d_tan earned 250 total points
ID: 11759208
Sorry, don't really have time to look through this whole thing. . .

but. . .

did notice a couple of things on first glance.

1.  If you are using the file input, your html form must have the enctype="multipart/form-data"

2.  your form method is post.  You will have to retrieve the values from the post array.
Try $_POST['FromName'] instead of $FromName  for any of the variables you want to retrieve from the previous htm page.

3.  Instead of that massive big and ugly if statement, try using a switch statement

switch ($_POST['menu1']) {

     case "ELET 1100":
            $mail_to = "xyz";
            $role_to = "";
             break;    // Use this to seperate the cases
      case "ELET 2103":
      case "ELET 2105":
            // Do something that will occur in both cases of ELET 2103 and 2105
            break;
      default:
            // Do something for any undefined cases above (in this case all the rest of the ELET 3105, 3303 etc etc. .
            break;
}

Also note that your values for comparison must be the same as in your option values.  In your included code, you compared menu1 to "1100" but in the option it was described as "ELET 1100".

Hth. . .

dtan

}
0
 
LVL 25

Assisted Solution

by:Squinky
Squinky earned 250 total points
ID: 11761140
dtan tells it like it is.

Use PHPMailer (phpmailer.sourceforge.net) for sending your email. It just skips and works around so many problems with using mail() that I can think of no good reason to EVER use mail()!

Zac, you can use it without having access to the mail() function as it has its own SMTP client...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Expert Comment

by:zac_charles
ID: 11761230
>>mail($mail_to, $subject, $FormText, $mail_headers);

This person is using the mail function, i dont know what phpmailer does and i didnt have the time to set it up or anything.
0
 

Author Comment

by:rskurane
ID: 11764135
Thanks for your reply
But I am having a problem with adding the feature of browsing -> uploading files and then sending them via email.

Regards
rskurane
0
 
LVL 25

Expert Comment

by:Squinky
ID: 11766589
Well we're each talking about small parts of the overall question - browse, upload and send are 3 distinct operations. You have already done the browse step - including a file type input tag will give you a browse button. Next you have to deal with the upload, which is described here, with examples: http://www.php.net/manual/en/features.file-upload.php which you can strip down to:

$uploaddir = '/var/www/uploads/'; //Where you want to save your upload to
$uploadfile = $uploaddir . $_FILES['filename']['name']; //What will be the full path to the uploaded file

if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {
  //Now your file is ready for sending, so you'd insert your email code here
} else {
   print "Possible file upload attack!  Here's some debugging info:\n";
   print_r($_FILES);
}

Creating a multipart email with attachments using the mail() function is a total drag, horribly complicated and thus very prone to error, so I'm not in the least bit surprised you're having trouble with it (everybody else does). There are two reasonable alternatives: PEAR's Mail_MIME module, and PHPMailer. I prefer PHPMailer as it does some stuff that Mail_MIME doesn't do (though Mail_MIME does MIME decoding which is great for handling receiving mail). Anyway, PHPMailer makes it really easy to send messages with attachments:

require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->From = 'uploader@example.com';
$mail->AddAddress('me@example.com', 'My Friend');
$mail->Subject = 'Uploaded file';
$mail->Body = "Someone uploaded a file called {$_FILES['filename']['name']}, which is enclosed!";
$mail->AddAttachment($uploadfile);
if(!$mail->Send()) {
      echo "Message was not sent";
      echo "Mailer Error: " . $mail->ErrorInfo;
} else {
      echo "Message has been sent";
}

although the mail command is just a single line, you'd probably have to write 10 times the code to set it up to replicate what PHPMailer is doing for you, and you'd probably make lots of mistakes in the process. Seriously, feel the force, let go of your attachment to the mail() command ;^)

To install phpmailer, download the files from the site phpmailer.sourceforge.net. It's just a PHP class (2 actually), so either drop it into your includes folder, or put it in the same folder as your script (there are no permissions issues when using PHPMailer).
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

706 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

15 Experts available now in Live!

Get 1:1 Help Now