Link to home
Start Free TrialLog in
Avatar of rskurane
rskurane

asked on

PHP script problems on browsing files and then sending to email

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>
Avatar of zac_charles
zac_charles
Flag of Australia image

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
ASKER CERTIFIED SOLUTION
Avatar of d_tan
d_tan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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.
Avatar of rskurane
rskurane

ASKER

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
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).