Jump to content

Form, Upload, Email


swjl

Recommended Posts

Hey guys,

 

Im having a problem with adding an attachment to a form and sending it through email...

 

 

This is the line where it all goes wrong.

 

 $message->addAttachment($path_of_uploaded_file); 

 

Without this line the email gets sent but with no attachment

 

Here are the error logs

 

 


home4/webcatc1/public_html/eddies/error_log: [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 [03-Oct-2012 10:10:28] 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 10:10:28] 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 11:52:04] 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 11:52:04] 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 11:52:04] 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 12:03: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 4 [03-Oct-2012 12:03: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 4 [03-Oct-2012 12:57:14] 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 12:57:14] 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 12:57:14] 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

 

and heres my 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"];
$DOB = $_POST ["DOB"];
$updir = "uploads/"; // Path To Images Directory


//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 = $updir . $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 = "stuff".

$to = "email";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$message->addAttachment($updir);

//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;
}
}

?>

 

Any Help would be amazing!

Link to comment
Share on other sites

Next time please don't post all thirty lines of error messages as one, but preserve the newlines between them.

 

These are the relevant errors for your script, as far as I could see:

03-Oct-2012 12:57:14] 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 12:57:14] 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 12:57:14] 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

 

The first two I don't know if are a part of the actual problem, or just something left over from a previous attempt. However, you should find out and fix it.

 

The last error message, however, tells you exactly what the problem is. Namely that $message is not an object, and thus does not have the addAttachment () property. In fact, from your code I can see that $message doesn't even exist before you attempt to use it.

You haven't even made a single object in your code, so I'm a bit mystified as to how you were planning on having this work. (Re-)reading the manual for the mailer class you've planned to use is a very good idea, I'd say.

 

I also see that you're not properly handling all error conditions, nor are you validating input (properly). So your script is open to attacks, and quite brittle should there be any unexpected condition in the input data.

The validation you have done is based upon the blacklisting principle, which is not recommended. Simply because you cannot make sure that you cover all of the basis, and even if you could you'd still need to update it constantly as new attack vectors appear. Change it to a whitelisting approach, where you only accept what you know to be valid input.

 

Also, using an IF-test to wrap a function that returns a boolean, to return a boolean, is rather wasteful. Return the result from the function call directly instead, like this:

    return preg_match ($pattern, $str);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.