Sending attachment with email
Posted: Sun Oct 31, 2004 9:45 am
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>
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>