Upload email attachment with php

design

#1

I am trying to upload files for attachment to emails using html form and php.

Here is my php code and errors I get.

// array with filenames to be sent as attachment
//$files = array(“file_1.ext”,“file_2.ext”,“file_3.ext”,…);

// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com";
$subject =“My subject”;
$message = $my_message;
$headers = “From: $from”;

// boundary
$semi_rand = md5(time());
$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;

// headers for attachment
$headers .= “\nMIME-Version: 1.0\n” . “Content-Type: multipart/mixed;\n” . " boundary="{$mime_boundary}"";

// multipart boundary
$message = “This is a multi-part message in MIME format.\n\n” . “–{$mime_boundary}\n” . “Content-Type: text/plain; charset=“iso-8859-1”\n” . “Content-Transfer-Encoding: 7bit\n\n” . $message . “\n\n”;
$message .= “–{$mime_boundary}\n”;

// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],“rb”);
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= “Content-Type: {“application/octet-stream”};\n” . " name="$files[$x]"\n" .
“Content-Disposition: attachment;\n” . " filename="$files[$x]"\n" .
“Content-Transfer-Encoding: base64\n\n” . $data . “\n\n”;
$message .= “–{$mime_boundary}\n”;
}

// send

$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo “

mail sent to $to!

”;
} else {
echo “

mail could not be sent!

”;
}

Warning: fopen(C:\New Folder\mail attach more done\rfq.html) [function.fopen]: failed to open stream: No such file or directory in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 562

Warning: filesize() [function.filesize]: stat failed for C:\New Folder\mail attach more done\rfq.html in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 563

Warning: fread(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 563

Warning: fclose(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 564

Warning: fopen(C:\New Folder\mail attach more done\rfq.php) [function.fopen]: failed to open stream: No such file or directory in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 562

Warning: filesize() [function.filesize]: stat failed for C:\New Folder\mail attach more done\rfq.php in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 563

Warning: fread(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 563

Warning: fclose(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/verify.php on line 564


#2

Where is the “C:…” path coming from? You’ve left out the part where you construct that array, but it’s definitely not right (as such a path will never exist on the server).


#3

The user selects files to attach to their email to be sent. This is basically a Request for Quote email page where user enters their contact info and optional files such as blue prints to be added as attachmnets to the email.
It creates the message part of the email from other fields in form.

The files come from the form on previous web pag that calls this page

<input type=file size=50 name=“rfq_file1” class=“rfq_input”>
<input type=file size=50 name=“rfq_file2” class=“rfq_input”>
<input type=file size=50 name=“rfq_file3” class=“rfq_input”>
<input type=file size=50 name=“rfq_file4” class=“rfq_input”>
<input type=file size=50 name=“rfq_file5” class=“rfq_input”>

this page (verify.php)

$rfq_file1 = trim($_POST[‘rfq_file1’]);
$rfq_file2 = trim($_POST[‘rfq_file2’]);
$rfq_file3 = trim($_POST[‘rfq_file3’]);
$rfq_file4 = trim($_POST[‘rfq_file4’]);
$rfq_file5 = trim($_POST[‘rfq_file5’]);

if (! $rfq_file1 == “”)
{
$files[0] = $rfq_file1;
}

if (! $rfq_file2 == “”)
{
$files[1] = $rfq_file2;
}

if (! $rfq_file3 == “”)
{
$files[2] = $rfq_file3;
}

if (! $rfq_file4 == “”)
{
$files[3] = $rfq_file4;
}

if (! $rfq_file5 == “”)
{
$files[4] = $rfq_file5;
}


#4

I am putting simpler versions of what I need to accomplish.

Here is the code for the two pages. First is the form page where user enters info and files to upload. Second is PHP Code to create email with attachments and send. Third is the ouput I am getting.

**** FORM CODE ****

<form name="filepost" method="post" action="file_email.php" >
<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr valign="bottom">
    <td height="20">Your Name:</td>
  </tr>
  <tr>
    <td><input name="uname" type="text" id="uname" size="30"></td>
  </tr>
  <tr valign="bottom">
    <td height="20">Your Email Address:</td>
  </tr>
  <tr>
    <td class="frmtxt2"><input name="emaila" type="text" id="emaila" size="30"></td>
  </tr>
  <tr>
    <td height="20" valign="bottom">RFQ File:</td>
  </tr>
  <tr valign="bottom">
    <td valign="bottom"><input name="myfile1" type="file" id="myfile1" size="16"></td>
  </tr>
  <tr valign="bottom">
    <td valign="bottom"><input name="myfile2" type="file" id="myfile2" size="16"></td>
  </tr>
  <tr>
    <td height="40" valign="middle"><input name="Reset2" type="reset" id="Reset2" value="Reset">
        <input name="Submit2" type="submit" value="Submit" ></td>
  </tr>
</table>
</form>

**** PHP Code ****

<?php session_start(); $uname = trim($_POST['uname']); $emaila = trim($_POST['emaila']); $myfile1 = trim($_POST['myfile1']); $myfile2 = trim($_POST['myfile2']); echo "uname = $uname
\n"; echo "emaila = $emaila
\n"; echo "myfile1 = $myfile1
\n"; echo "myfile2 = $myfile2
\n"; if (! $myfile1 == "") { $files[0] = $myfile1; } if (! $myfile2 == "") { $files[1] = $myfile2; } for($x=0;$x<count($files);$x++) { echo "\$files[$x] = $files[$x]
\n"; } $my_message="Request for quote\n\n"; $my_message=$my_message . "Name: $uname\n"; $my_message=$my_message . "Email: $emaila\n"; // email fields: to, from, subject, and so on //$to = "mail@mail.com"; $to = "cvanbeek7b@aol.com"; $from = $emaila; $subject ="Request for quote"; $message = $my_message; $headers = "From: $from"; // boundary $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // headers for attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // multipart boundary $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $message .= "--{$mime_boundary}\n"; // preparing attachments for($x=0;$x<count($files);$x++){ $file = fopen($files[$x],"rb"); $data = fread($file,filesize($files[$x])); fclose($file); $data = chunk_split(base64_encode($data)); $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; $message .= "--{$mime_boundary}\n"; } // send $ok = @mail($to, $subject, $message, $headers); if ($ok) { echo "

mail sent to $to!

"; } else { echo "

mail could not be sent!

"; } ?>

**** OUTPUT ****

uname = Joe Cool
emaila = jcool@aol.com
myfile1 = C:\New Folder\mail attach more done\rfq.html
myfile2 = C:\New Folder\mail attach more done\rfq.php
$files[0] = C:\New Folder\mail attach more done\rfq.html
$files[1] = C:\New Folder\mail attach more done\rfq.php

Warning: fopen(C:\New Folder\mail attach more done\rfq.html) [function.fopen]: failed to open stream: No such file or directory in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 58

Warning: filesize() [function.filesize]: stat failed for C:\New Folder\mail attach more done\rfq.html in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 59

Warning: fread(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 59

Warning: fclose(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 60

Warning: fopen(C:\New Folder\mail attach more done\rfq.php) [function.fopen]: failed to open stream: No such file or directory in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 58

Warning: filesize() [function.filesize]: stat failed for C:\New Folder\mail attach more done\rfq.php in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 59

Warning: fread(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 59

Warning: fclose(): supplied argument is not a valid stream resource in /home/cvanbeek7b/ctvbwebcreations.com/OPC_New01/file_email.php on line 60

mail sent to cvanbeek7b@aol.com!


#5

That’s not how file uploads in PHP work. Read this for details: http://php.net/features.file-upload.multiple


#6

I have made the following changes but it still does not upload the files.

Is there some PHP setting that I have to make to allow it to upload files and tell where to load them?


#7