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
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Tue Sep 07, 2004 2:16 pm
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">
</font>
</td>
<td width="350" align="left" valign="center">
<input type="submit" value="Send" name="submit">
</td>
</tr>
</table>
</center>
</form>
</body></html>
<?
}
}
}
?>
Last edited by
ol4pr0 on Tue Sep 07, 2004 6:43 pm, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 07, 2004 2:24 pm
the code posted only handles uploaded files..
Code: Select all
<?$_SERVERї'PHP_SELF']?>will do nothing.
Code: Select all
$query ='SELECT * FROM vacante WHERE unique_id="'.$_GETї'ref'].'" AND vac_id="'.$_GETї'vac_id'].'"';this is dangerous. Those params need sanitizing.
Code: Select all
$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)) {this should probably be 1 query-loop set.
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Tue Sep 07, 2004 2:30 pm
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 ?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 07, 2004 2:34 pm
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..
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Tue Sep 07, 2004 2:36 pm
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..
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 07, 2004 2:44 pm
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.
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Tue Sep 07, 2004 2:56 pm
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.
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Tue Sep 07, 2004 3:59 pm
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>";
}
?>
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Tue Sep 07, 2004 6:44 pm
Seemed that it worked on server..
on localhost .. it doesnt but.. doesnt need to