Page 1 of 1

Sending attachment with email

Posted: Sun Oct 31, 2004 9:45 am
by patch2112
Hello all,

I'm new, so excuse me and all that, but I did research this problem and didn't find a solution. Please help! But before you ask why I'm doing it this way, I'm following a book, and I don't know anyother way to do it! Thanks so much!

I'm trying to make a simple form that can send a email with an attachment in it. I'm using a FreeBSD server. Here is the error I get...

Warning: fopen(test.txt): failed to open stream: No such file or directory in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 5

Warning: fread(): supplied argument is not a valid stream resource in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 8

Warning: fclose(): supplied argument is not a valid stream resource in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 42
Thanks for submitting!

Here is the html code for the form...
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="feedback.php" method="post" enctype="multipart/form-data">
<p>To:
<input name="to" type="text" id="to" size="30"/>
<br>
<br>
Email:
<input type="text" name="useraddr" size="30"/>
<br>
<br>
Subject
<input name="subject" type="text" id="subject" size="30">
<br>
<br>
<textarea name="comments" cols="30" rows="5">
</textarea>
<br/>
<br>
<input name="attach" type="file">
<br>
<br>
<input type="submit" value="Send Form">
</p>
</form>

</body>
</html>

Here is the code from the second page...

<?
#varibles passed are $username $useraddr $subject $comments $attach

#open the file
$fp = fopen ($attach_name, "r");

#read the file into a variable
$file = fread($fp, $attach_size);

#encode the data for safe transit and insert /r/n for every 76 characters
$file = chunk_split(base64_encode($file));

#get a random 32-character hexadecimal number
$num = md5(time());

#define the main headers
$hdr="From: $from\r\n";
$hdr.="MIME-Version: 1.0\r\n";
$hdr.="Content-Type: multipart/mixed;";
$hdr.="boundary=$num\r\n";
$hdr.="--$num\r\n"; #start boundary here

#define the message section
$hdr.="Content-Type: text/plain\r\n";
$hdr.="Content-Transfer_Encoding: 8bit\r\n\n";
$hdr.="$comment\r\n";
$hdr.="--$num\n"; #start boundary here

#define the attachment section
$hdr.="Content-Type: $attach_type;";
$hdr.="name=\"$attach_name\"\r\n";
$hdr.="Content-Transfer-Encoding: base64\r\n";
$hdr.="Content-Disposition: attachment; ";
$hdr.="filename=\"$attach_name\"\r\n\n";
$hdr.="$file\r\n";
$hdr.="--$num--"; #final boundary here

#send the email now
mail($to, $subject, "", $hdr);

#close the file
fclose ($file);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Thanks for submitting!
</body>
</html>

Posted: Sun Oct 31, 2004 9:57 am
by mudkicker
please use [ code ] and [ php ] tags to color your codes.

Sorry, new to forums too!

Posted: Sun Oct 31, 2004 10:13 am
by patch2112
Sorry, I'm new to forums too! Here is the post with the tags.

Hello all,

I'm new, so excuse me and all that, but I did research this problem and didn't find a solution. Please help! But before you ask why I'm doing it this way, I'm following a book, and I don't know anyother way to do it! Thanks so much!

I'm trying to make a simple form that can send a email with an attachment in it. I'm using a FreeBSD server. Here is the error I get...

Warning: fopen(test.txt): failed to open stream: No such file or directory in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 5

Warning: fread(): supplied argument is not a valid stream resource in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 8

Warning: fclose(): supplied argument is not a valid stream resource in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 42
Thanks for submitting!

Here is the html code for the form...

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form action="feedback.php" method="post" enctype="multipart/form-data"&gt;
  &lt;p&gt;To: 
    &lt;input name="to" type="text" id="to" size="30"/&gt;
    &lt;br&gt;
    &lt;br&gt;
    Email: 
    &lt;input type="text" name="useraddr" size="30"/&gt;
    &lt;br&gt;
    &lt;br&gt;
    Subject 
    &lt;input name="subject" type="text" id="subject" size="30"&gt;
    &lt;br&gt;
    &lt;br&gt;
    &lt;textarea name="comments" cols="30" rows="5"&gt;
&lt;/textarea&gt;
    &lt;br/&gt;
    &lt;br&gt;
    &lt;input name="attach" type="file"&gt;
    &lt;br&gt;
    &lt;br&gt;
    &lt;input type="submit" value="Send Form"&gt;
  &lt;/p&gt;
  &lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
Here is the code from the second page...

Code: Select all

<?php
<?
#varibles passed are $username $useraddr $subject $comments $attach

#open the file
$fp = fopen ($attach_name, "r");

#read the file into a variable
$file = fread($fp, $attach_size);

#encode the data for safe transit and insert /r/n for every 76 characters
$file = chunk_split(base64_encode($file));

#get a random 32-character hexadecimal number
$num = md5(time());

#define the main headers
$hdr="From: $from\r\n";
$hdr.="MIME-Version: 1.0\r\n";
$hdr.="Content-Type: multipart/mixed;";
$hdr.="boundary=$num\r\n";
$hdr.="--$num\r\n"; #start boundary here

#define the message section
$hdr.="Content-Type: text/plain\r\n";
$hdr.="Content-Transfer_Encoding: 8bit\r\n\n";
$hdr.="$comment\r\n";
$hdr.="--$num\n"; #start boundary here

#define the attachment section
$hdr.="Content-Type: $attach_type;";
$hdr.="name="$attach_name"\r\n";
$hdr.="Content-Transfer-Encoding: base64\r\n";
$hdr.="Content-Disposition: attachment; ";
$hdr.="filename="$attach_name"\r\n\n";
$hdr.="$file\r\n";
$hdr.="--$num--"; #final boundary here

#send the email now
mail($to, $subject, "", $hdr);

#close the file
fclose ($file);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Thanks for submitting!
</body>
</html>
?>

Posted: Sun Oct 31, 2004 10:27 am
by swdev
Your first error message
Warning: fopen(test.txt): failed to open stream: No such file or directory in /home/demystify/www/de-mystify.co.uk/scripting/feedback.php on line 5
tells you exactly what is wrong.

Your code is looking for a file called test.txt - probably in the current directory, and it can't find it. Make sure it exists.


on a stylistic point, line 18 says

Code: Select all

$hdr="From: $from\r\n";
I this this would be better written as

Code: Select all

$hdr = 'From: ' . $from . '\r\n';
This way it is much easier to see where yo have used variable names, especially if you have a syntax highlighting editor.

Hope this helps.