Help with web page form mail with attachment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reallygreen
Forum Newbie
Posts: 4
Joined: Mon Aug 08, 2011 1:31 pm

Help with web page form mail with attachment

Post by reallygreen »

I am attempting to get an online form with attachment working for a website. Upon submitting, I get the following errors:

Warning: filesize() [function.filesize]: stat failed for http://www.zoincihfthrelmdq.com/sendresults.php in /home/xxxxxxx/public_html/sendresults.php on line 15

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/xxxxxxx/public_html/sendresults.php on line 15
The file was successfully sent!

I'd appreciate it very much if anyone can offer assistance/advice with this code:

Code: Select all

<?php  
$fileatt = "http://www.xxxxxxxxxxxxxxxxxxxx.com/sendresults.php";                  
$fileatt_type = "application/octet-stream"; 
$fileatt_name = "word file";

$email_from = "candidate";
$email_subject = "job inquiry";
$email_txt = "all about candidate";

$email_to = "xxxxxxxxxxxxx@xxxxxxxxxxxxxx.com";

$headers = "From: ".$email_from;  

$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));
$size = ( $size > 1024 ? round($size/1024, 2) . "mb" : $size."kb");  
fclose($file);  

$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";  

$ok = @mail($email_to, $email_subject, $email_message, $headers);  

if($ok) {  
echo "<font face=verdana size=2>The file was successfully sent!</font>";  
} else {  
die("Sorry but the email could not be sent. Please go back and try again!");  
}  
?>
Thank you.
Last edited by califdon on Mon Aug 08, 2011 2:02 pm, edited 1 time in total.
Reason: Moderator added syntax=php tags to make code readable. Note to poster, please always do this. Also deleted duplicate post.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help with web page form mail with attachment

Post by califdon »

(For future reference: please use 'PHP Code' button in editor to make your code easier for us to read. I have done that for you, above.)

Do understand that a Warning message is different from an Error message. What these Warning messages are telling you is that your fread() function (line 15) is being given a value of zero for the 'length' parameter, which is not allowed http://php.net/manual/en/function.fread.php. You are using the output from 'filesize($fileatt)" for this value, but $fileatt seems to be the name of a PHP script (possibly this one??), which doesn't make sense to me, unless you intend to attach this, or some other PHP script, to the email. Is that what you are trying to do? Even if it is, you would NOT want to refer to it as a Web URL, it should be a local file system reference. So I think your problem is in the naming of the file in the first line of this code.
reallygreen
Forum Newbie
Posts: 4
Joined: Mon Aug 08, 2011 1:31 pm

Re: Help with web page form mail with attachment

Post by reallygreen »

Thank you for your reply. I am very new to PHP. The form is very basic and is on a web page. The user enters standard information, attaches a file to upload and then submits. Form routes to the server (sendresults.php) and hopefully sends an email to the intended recipient.
reallygreen
Forum Newbie
Posts: 4
Joined: Mon Aug 08, 2011 1:31 pm

Re: Help with web page form mail with attachment

Post by reallygreen »

What I am trying to do is to send form results by mail with an atachment using PHP script I named sendresults.php. The first code I was using was probably closer? Perhaps you could point me in the right direction as to what to add here and where? Thank you.

<?php
$subject = 'From the Some Placee Inc. Careers Page';
$emailadd = 'webmaster@someplacee.com';
$url = 'http://www.someplacee.com/success.html';
$req = '1';
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help with web page form mail with attachment

Post by califdon »

I'm afraid I still don't understand exactly what you are trying to do. Are you trying to send the file that the user uploads as an attachment to an email that is addressed to...whom? The user? Some fixed address? The code you have shown doesn't do anything to upload the file, it only reads what I assume is your script file, from the web. Also it doesn't do anything to recover the other form data, whatever that may be. Perhaps you have only shown a part of your script?
reallygreen
Forum Newbie
Posts: 4
Joined: Mon Aug 08, 2011 1:31 pm

Re: Help with web page form mail with attachment

Post by reallygreen »

Yes, I am trying to send the file to an email which will be a company email address and will be fixed. Yes, uploading the file is the part I need help with. This is the whole script so far. Thank you for your reply.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help with web page form mail with attachment

Post by califdon »

For starters, you need to capture the form data. Read http://www.htmldog.com/guides/htmlbeginner/forms/.

Then you need to upload the file: http://www.tizag.com/phpT/fileupload.php and http://www.w3schools.com/PHP/php_file_upload.asp
Post Reply