www.webdeveloper.com
Results 1 to 11 of 11

Thread: Upload, Attachment, Email

  1. #1
    Join Date
    Mar 2008
    Posts
    88

    Upload, Attachment, Email

    Hey Guys, Im having a problem with adding an attachment to a form and sending it through email... This is what I get when I press submit

    Server error
    The website encountered an error while retrieving http://webaddress/eddies/form-to-email.php. It may be down for maintenance or configured incorrectly.
    Here are some suggestions:
    Reload this webpage later.
    HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

    heres the code

    Code:
    
    <?php
    include_once('Mail.php');
    include_once('Mail_Mime/mime.php');
    
    if(!isset($_POST['submit']))
    {
    	//This page should not be accessed directly. Need to submit the form.
    	echo "error; you need to submit the form!";
    }
    
    $email = $_POST["email"];
    $firstName = $_POST["firstName"];
    $surname = $_POST ["surname"];
    
    //Get the uploaded file information
    $name_of_uploaded_file =
        basename($_FILES['uploaded_file']['name']);
    //get the file extension of the file
    $type_of_uploaded_file =
        substr($name_of_uploaded_file,
        strrpos($name_of_uploaded_file, '.') + 1);
    $size_of_uploaded_file =
        $_FILES["uploaded_file"]["size"]/1024;//size in KBs
    
    //Settings
    $max_allowed_file_size = 100; // size in KB
    $allowed_extensions = array("doc", "docx", "pdf", "txt");
    //Validations
    if($size_of_uploaded_file > $max_allowed_file_size )
    {
      $errors .= "\n Size of file should be less than $max_allowed_file_size";
    }
    //------ Validate the file extension -----
    $allowed_ext = false;
    for($i=0; $i<sizeof($allowed_extensions); $i++)
    {
      if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
      {
        $allowed_ext = true;
      }
    }
    if(!$allowed_ext)
    {
      $errors .= "\n The uploaded file is not supported file type. ".
      " Only the following file types are supported: ".implode(',',$allowed_extensions);
    }
    
    //copy the temp. uploaded file to uploads folder
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
    if(is_uploaded_file($tmp_path))
    {
      if(!copy($tmp_path,$path_of_uploaded_file))
      {
        $errors .= '\n error while copying the uploaded file';
      }
    }
    	
    	if(empty($email)||empty($firstName)) 
    {
        echo "Name and email are mandatory!";
        exit;
    }
    if(IsInjected($visitor_email))
    {
        echo "Bad email value!";
        exit;
    }
    
    
    
    
    $email_from = $email;//<== update the email address
    $email_subject = "New Applicant";
    $email_body = " details about email".
        
    $to = "email@email.com";//<== update the email address
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    $message->addAttachment($path_of_uploaded_file);
    
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    
    
    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
      $injections = array('(\n+)',
                  '(\r+)',
                  '(\t+)',
                  '(%0A+)',
                  '(%0D+)',
                  '(%08+)',
                  '(%09+)'
                  );
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str))
        {
        return true;
      }
      else
        {
        return false;
      }
    }
       
    ?>


    Once I press submit I can see the file being uploaded but then the server error comes up.
    Anyone see a problem with it?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Anything in the php_error.log file?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2008
    Posts
    88
    Code:
    /home4/webcatc1/public_html/eddies/error_log:
    [02-Sep-2012 14:03:43] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 115
    [02-Sep-2012 14:06:19] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 115
    [02-Sep-2012 16:32:16] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 115
    [02-Sep-2012 16:34:36] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 116
    [02-Sep-2012 17:32:20] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 156
    [02-Sep-2012 17:40:40] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 156
    [02-Sep-2012 17:40:50] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 156
    [02-Sep-2012 17:41:36] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 148
    [02-Sep-2012 17:46:49] PHP Fatal error:  Call to undefined function  addattachment() in /home4/webcatc1/public_html/eddies/form-to-email.php on line 83
    [02-Sep-2012 17:58:13] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 187
    [02-Sep-2012 18:03:11] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 188
    [02-Sep-2012 18:06:11] PHP Fatal error:  Call to undefined function  addattachment() in /home4/webcatc1/public_html/eddies/form-to-email.php on line 41
    [02-Oct-2012 16:42:33] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [02-Oct-2012 16:42:33] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [02-Oct-2012 16:42:41] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [02-Oct-2012 16:42:41] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [02-Oct-2012 16:43:12] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [02-Oct-2012 16:43:12] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [02-Oct-2012 16:44:01] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 22
    [02-Oct-2012 16:44:01] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 22
    [02-Oct-2012 16:46:57] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:46:57] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:49:02] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:49:02] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:49:02] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 100
    [02-Oct-2012 16:50:03] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:03] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:35] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:35] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:35] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 100

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    I would start by figuring out why the include is failing, as that failure may be causing at least some of the other errors. You might want to change those include()s to require()s since they appear to be mandatory.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2008
    Posts
    88
    It sends the email perfectly without the upload code...

    I've looked at the lines in the error log and messed about with them, but it still gives the same error message.

    I also tried your suggestion but no success (same error message again)

    Anything else I can try or do?

    Thanks for replying BTW

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Do you have a new set of errors from the log after correcting the ones above?

  7. #7
    Join Date
    Mar 2008
    Posts
    88
    Code:
    /home4/webcatc1/public_html/eddies/error_log:
    [02-Oct-2012 16:49:02] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 100
    [02-Oct-2012 16:50:03] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:03] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:35] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:35] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [02-Oct-2012 16:50:35] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 100
    [03-Oct-2012 08:32:55] PHP Warning:  include(Mail_Mime/mime.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [03-Oct-2012 08:32:55] PHP Warning:  include() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [03-Oct-2012 08:32:55] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 100
    [03-Oct-2012 08:33:31] PHP Warning:  require(Mail_Mime/mime.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [03-Oct-2012 08:33:31] PHP Fatal error:  require() [<a href='function.require'>function.require</a>]: Failed opening required 'Mail_Mime/mime.php' (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 3
    [03-Oct-2012 08:39:05] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 89
    [03-Oct-2012 08:39:05] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 89
    [03-Oct-2012 08:39:05] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 99
    [03-Oct-2012 08:46:46] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 86
    [03-Oct-2012 08:46:46] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 86
    [03-Oct-2012 08:46:46] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 96
    [03-Oct-2012 08:52:26] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 92
    [03-Oct-2012 08:53:43] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [03-Oct-2012 08:53:43] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [03-Oct-2012 08:53:43] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 96
    [03-Oct-2012 09:09:55] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 190
    [03-Oct-2012 09:11:44] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 191
    [03-Oct-2012 09:12:08] PHP Parse error:  syntax error, unexpected $end in /home4/webcatc1/public_html/eddies/form-to-email.php on line 191
    [03-Oct-2012 09:16:10] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [03-Oct-2012 09:16:10] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [03-Oct-2012 09:16:10] PHP Fatal error:  Call to undefined function  isinjected() in /home4/webcatc1/public_html/eddies/form-to-email.php on line 81
    [03-Oct-2012 09:49:46] PHP Warning:  include_once(Mail_Mime/mime.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [03-Oct-2012 09:49:46] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening 'Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/form-to-email.php on line 4
    [03-Oct-2012 09:49:46] PHP Fatal error:  Call to a member function addAttachment() on a non-object in /home4/webcatc1/public_html/eddies/form-to-email.php on line 96

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Well, your include of Mail_Mime/mime.php is still not working. You may either need to check/fix your include_path settings or else change it to use a full relative or absolute file path. Since it's not being included, that's probably why you get the "Call to a member function addAttachment() on a non-object" error.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Mar 2008
    Posts
    88
    update...

    I took out >

    Code:
      $message->addAttachment($path_of_uploaded_file);
    and it sent the email (and i received it) and the file got uploaded into the uploads folder on the server but I obviously didnt get the attachment

  10. #10
    Join Date
    Mar 2008
    Posts
    88
    Been messing about with it and im fed up lol

    heres what ive done

    I've added installed all the pear files

    but it still cant find them

    Code:
    
    include('/home4/webcatc1/public_html/eddies/Pear/PEAR');
    include('.:/usr/lib64/php:/usr/lib/php:/usr/share/pear');
    include_once('Mail.php');
    include_once('public_html/eddies/Pear/PEAR/Mail/mime.php');
    include_once('public_html/eddies/Pear/PEAR/Mail.php');
    include_once('public_html/eddies/Pear/PEAR/Mail/mimePart.php');
    include_once('pear.php.net/Mail_Mime-1.8.5');
    include_once('.:/usr/lib64/php:/usr/lib/php:/usr/share/pear/Mail_Mime/mime.php');
    
    
    
    $message = new Mail_mime();
    $message->setTXTBody($text);
    $message->addAttachment($path_of_uploaded_file);
    $body = $message->get();
    $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
    $headers = $message->headers($extraheaders);
    $mail = Mail::factory("mail");
    $mail->send($to, $headers, $body);
    ?>
    and this is the error i get

    Code:
    /home4/webcatc1/public_html/eddies/error_log:
    [04-Oct-2012 04:56:18] PHP Warning:  include_once() [<a href='function.include'>function.include</a>]: Failed opening '.:/usr/lib64/php:/usr/lib/php:/usr/share/pear/Mail_Mime/mime.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php:/usr/share/pear') in /home4/webcatc1/public_html/eddies/test.php on line 79
    [04-Oct-2012 04:56:18] PHP Fatal error:  Class 'Mail_mime' not found in /home4/webcatc1/public_html/eddies/test.php on line 83
    Any ideas?

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    You need to locate the files on your disk and point the include appropriately. Isn't much we could help with without knowing the exact location of the files you are wanting to include.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles