Page 1 of 1

[SOLVED] sending file located on server

Posted: Tue Sep 07, 2004 2:16 pm
by ol4pr0
Mailing a document which is already on the server.
I wouldnt have trouble sending a attachment if it was to be uploaded, however i am trying to send a file that is already on the server.

this is how i am tring it to do.. but it doesnt work. the attachment is not being included.

Code: Select all

<?
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/db.inc');
if (isset($_POST['submit']))
{
  print_r($_POST);
  $header = "From: " . $from . "\n";
  if ($attachement != "none")
  {
    // Get attachement type
    $type = $_FILES[attachement][type];
    if (($type == "text/plain") || ($type == "text/html"))
      $encoding = "8bit";
    else
      $encoding = "base64";

    // Get attachement content
    $fp = @fopen($_FILES[attachement][tmp_name],"r");
    if (!$fp) {
      print "Could not open attachment for reading...<br>\n";
      print "Exiting script!<br><br>";
      print "No mail has been sent!";
      exit;
    }
    $file_content = fread($fp,filesize($_FILES[attachement][tmp_name]));

    // if encoding is base64 ... encode it
    if ($encoding == "base64")
      $file_content = chunk_split(base64_encode($file_content));

    // create a unqiue boundary
    $boundary = strtoupper(md5(uniqid(time())));

    // create the message header...
    $header .= "MIME-version: 1.0\n";
    $header .= "Content-Type: multipart/mixed;\n";
    $header .= "\tboundary= " . $boundary . "\n\n";

    $header .= "This is a multi-part message in MIME format.\n\n";

    // create the message body in the header
    $header .= "--" . $boundary . "\n";
    $header .= "Content-Type: " . $ct . ";\n";
    $header .= "\tcharset="iso-8859-1"\n";
    $header .= "Content-Transfer-Encoding: quoted-printable\n\n";

    // actual message
    $header .=  $body . "\n\n";

    // now for the attachement
    $header .= "--" . $boundary . "\n";
    $header .= "Content-Type: " . $type . "\n";
    $header .= "Content-Transfer-Encoding: " . $encoding . "\n";
    $header .= "Content-Disposition: attachment; filename="" . $_FILES[attachement][name] . ""\n\n";

    // actual attachement
    $header .= $file_content . "\n\n";
    $header .= "--" . $boundary . "--";
  }
  else // if there is no attachement...
  {
    $header .= "Content-Type: " . $ct . ";\n";
    $header .= "\tcharset="iso-8859-1"\n";
    $header .= "Content-Transfer-Encoding: quoted-printable\n\n";

    // actual message
    $header .=  $body . "\n\n";
  }
  if (@mail($_POST['to'],$subject,"",$header))
    print "Mail sent!";
  else
    print "Error sending email!";
  
}
else
{
session_start();

$query ='SELECT * FROM vacante WHERE unique_id="'.$_GET['ref'].'" AND vac_id="'.$_GET['vac_id'].'"';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {

$query_cur ='SELECT * FROM archivos WHERE unique_id="'.$_SESSION['unique_id'].'"';
$result_cur = mysql_query($query_cur);
while($row_cur = mysql_fetch_assoc($result_cur)) {
?>

<html>

<head>
<title>Mail Attachement</title>
</head>

<body bgcolor="#FFFFFF">

<center>
<form enctype="multipart/form-data" action="<?$_SERVER['PHP_SELF']?>" method="post">
<table width="500" cellspacing="0" cellpadding="0" border="0">
<tr>
  <td width="150" align="left" valign="center">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    To:
    </font>
  </td>
  <td width="350" align="left" valign="center">
    <input type="hidden" size="40" name="to" value="<?echo $row['email_vac'];?>"><? echo $_GET['vac_id'];?>
  </td>
</tr>
<tr>
  <td width="150" align="left" valign="center">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    From:
    </font>
  </td>
  <td width="350" align="left" valign="center">
    <input type="text" size="40" name="from">
  </td>
</tr>
<tr>
  <td width="150" align="left" valign="center">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    Subject:
    </font>
  </td>
  <td width="350" align="left" valign="center">
    <input type="text" size="40" name="subject">
  </td>
</tr>
<tr>
  <td width="150" align="left" valign="center">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    Content type:
    </font>
  </td>
  <td width="350" align="left" valign="center">
    <select name="ct">
      <option value="text/plain">Plaintext</option>
      <option value="text/html">HTML mail</option>
    </select>
  </td>
</tr>
<tr>
  <td width="150" align="left" valign="top">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    Message body:
    </font>
  </td>
  <td width="350" align="left" valign="top">
    <textarea rows="8" cols="38" name="body"></textarea>
  </td>
</tr>
<tr>
  <td width="150" align="left" valign="center">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    Attachement:
    </font>
  </td>
  <td width="350" align="left" valign="center">
  <input type="hidden" name="attachement" value="<? ECHO 'http://'.$_SERVER['HTTP_HOST']."/".$row_cur['archivo'];?>"><? ECHO $row_cur['archivo'];?>
  </td>
</tr>
<tr>
  <td width="150" align="left" valign="center">
    <font face="Verdana,Tahoma,Arial,Helvetica" size="2" color="#000000">
    &nbsp;
    </font>
  </td>
  <td width="350" align="left" valign="center">
    <input type="submit" value="Send" name="submit">
  </td>
</tr>
</table>
</center>
</form>
</body></html>
<?
	}
  }
}
?>

Posted: Tue Sep 07, 2004 2:24 pm
by feyd
the code posted only handles uploaded files..

Code: Select all

&lt;?$_SERVER&#1111;'PHP_SELF']?&gt;
will do nothing.

Code: Select all

$query ='SELECT * FROM vacante WHERE unique_id="'.$_GET&#1111;'ref'].'" AND vac_id="'.$_GET&#1111;'vac_id'].'"';
this is dangerous. Those params need sanitizing.

Code: Select all

$query ='SELECT * FROM vacante WHERE unique_id="'.$_GET&#1111;'ref'].'" AND vac_id="'.$_GET&#1111;'vac_id'].'"';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {

$query_cur ='SELECT * FROM archivos WHERE unique_id="'.$_SESSION&#1111;'unique_id'].'"';
$result_cur = mysql_query($query_cur);
while($row_cur = mysql_fetch_assoc($result_cur)) {
this should probably be 1 query-loop set.

Posted: Tue Sep 07, 2004 2:30 pm
by ol4pr0
i whish i could make this one query, however the data is coming from 2 tables, And this part u see here is within a page that can only be accessed after somebody is logged in. however if you have suggestions. :)

You said the code will only handle uploaded files.

What do i need to change for this to work ?

Posted: Tue Sep 07, 2004 2:34 pm
by feyd
you need to add whatever stuff you want for getting the file to use that's on the file system already. So if that's a file browser of sorts, or an automatic selection, that's up to you. But you need to generate a valid local path to that file for the loading of the data to work.

If you rewrote the file handling into a generic fashion, where it just reads the filename passed to it and encodes it into a mime-header or something, that'd help a lot. Then you can just have some custom code to handle whether there is an uploaded file to deal with or an on server file..

Posted: Tue Sep 07, 2004 2:36 pm
by ol4pr0
Valid local path as in valid local part on client system ?

Code: Select all

<? ECHO 'http://'.$_SERVER['HTTP_HOST']."/".$row_cur['archivo'];?>


Becuase that part is the valid path of the archive on the server.

didnt follow you on this part
feyd wrote: If you rewrote the file handling into a generic fashion, where it just reads the filename passed to it and encodes it into a mime-header or something, that'd help a lot. Then you can just have some custom code to handle whether there is an uploaded file to deal with or an on server file..

Posted: Tue Sep 07, 2004 2:44 pm
by feyd

Code: Select all

<? ECHO 'http://'.$_SERVER['HTTP_HOST']."/".$row_cur['archivo'];?>
is not a local path on the server. That's a URL. A local path would be:

Code: Select all

$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPERATOR . $row_cur['archivo'];
however, you should not echo that into the html, as that tells a user a lot more about your server set up than you probably want.
didnt follow you on this part
Make the file attaching a function that accepts at least 1 parameter: the path-filename to read. Have it return the mime-encoded string to add to the headers. Then you can attach whatever and however many files you wish.

Posted: Tue Sep 07, 2004 2:56 pm
by ol4pr0
Oke basicly what u say is that the code below should work right ?

Code: Select all

$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPERATOR . $row_cur['archivo'];
If so, it didnt.

And whatever u said after that i still do not understand.

What this does, is that somebody can send there curriculum to some company if they see a offer they they would like to respond to.
The mail adress they wont see. the file location if i could hide it i would.

If you have any example of what i should do instead of what i am doing now.. That would be great.

Posted: Tue Sep 07, 2004 3:59 pm
by ol4pr0
I have changed alot.. to test this.. however the file is still not being attached.. i am almost sure this should work. actually its not being mailed at all :(

Code: Select all

<?php
$subject = 'testing..';
$message = 'sending with attachemnt';

$fileatt      = "test.doc";
$fileatt_type = 'application/msword';
$fileatt_name = '23456789';

$headers = "From: myself";

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
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}"";

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

$data = chunk_split(base64_encode($data));

$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@adres.com", $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

Posted: Tue Sep 07, 2004 6:44 pm
by ol4pr0
Seemed that it worked on server..

on localhost .. it doesnt but.. doesnt need to :)