 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
slypig
Joined: 19 Apr 2008 Posts: 2
|
Posted: Sat Apr 19, 2008 3:35 pm Post subject: Emailing file attachment with PHP and PEAR |
|
|
Can anyone steer me in the right direction here. I've created php forms that allow the user to send an attachment before, but now the server I'm on uses PEAR for email sending.
Here is my coding for the send...
require_once("Mail.php");
require_once("Mail/mime.php");
$attach_name = $_FILES["attach"]["name"];
$attach_type = $_FILES["attach"]["type"];
$attach_size = ($_FILES["attach"]["size"] / 1024);
$attach_temp = $_FILES["attach"]["tmp_name"];
if (is_uploaded_file($attach)) {
$fp = fopen($attach, "rb");
$data = fread($fp, filesize($attach));
$data = chunk_split(base64_encode($data));
fclose($fp);
}
$uploaddir = "upload/";
if (move_uploaded_file($_FILES["attach"]["tmp_name"], $uploaddir .
$_FILES["attach"]["name"])) {
print "File is valid, and was successfully uploaded.";
} else {
print "There were some errors!";
}
$file = "(don't know what to put here!! needs to be the attachment!!)
$mime = new Mail_mime();
$mime->setTXTBody($text);
$mime->addAttachment($file,"application/octet-stream");
$body = $mime->get();
$headers = $mime->headers(array("From" => "from email", "Subject" => "subject"));
$params["host"] = 'smtp server';
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = 'username';
$params["password"] = "password";
$mail =& Mail::factory("smtp", $params);
if(!$mail->send("email address",$headers,$body)) {
echo "Could not send email";
}
?>
I have it working to the point where I can upload the file to the server. I don't really WANT to upload the file to the server, I want to email it. I just added the uploading part to make sure I was getting the file info correct.
My main problem is, prior to running into PEAR, I was able to add header and message information regarding MIME content, etc. But as far as I can tell I can't do that with PEAR...
Don't know if I've explained this well....but any help would be appreciated! Thanks |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Sat Apr 19, 2008 6:41 pm Post subject: |
|
|
I'm not sure whether I got your question right, but if you're asking about the uploaded file handling and the attachment of this file, then there is a rather simple solution:
| Code: |
$mime->addAttachment($_FILES["attach"]["tmp_name"]);
|
'application/octet-stream' is already the default value for the second parameter, and therefore, not really needed in this case.
The line with | Code: | | if (is_uploaded_file($attach)) { | doesn't make sense, as there is now variable with this name in your code. However, it would make sense with $_FILES["attach"]["tmp_name"].
The other variables $attach_* aren't really needed, the same is true for the fopen/fread/chunk_split/fclose calls. move_uploaded_file() also isn't needed, Mail_mime can directly read from the temporary file that PHP creates.
Does that answer your questions? |
|
| Back to top |
|
 |
slypig
Joined: 19 Apr 2008 Posts: 2
|
Posted: Sun Apr 20, 2008 5:23 pm Post subject: |
|
|
It appears I was just trying to make things harder for myself.
I've gotten this to work, but not without move_uploaded_file(). Removing this and just using $mime->addAttachment($_FILES["attach"]["tmp_name"]); resulted in the tmp file being sent as the attachment.
I ended up using this ...
$mime->addAttachment($uploaddir . $_FILES["attach"]["name"]);
But overall, you pointed me in the right direction, and I thank you!
 |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Sun Apr 20, 2008 7:24 pm Post subject: |
|
|
| slypig wrote: | | I've gotten this to work, but not without move_uploaded_file(). Removing this and just using $mime->addAttachment($_FILES["attach"]["tmp_name"]); resulted in the tmp file being sent as the attachment. |
The temp file isn't wrong, but I guess that you don't like the filename of the temp file to be shown in the email? There is a workaround that still avoids move_uploaded_file() (not tested, but should work):
| Code: |
$mime->addAttachment(file_get_contents($_FILES["attach"]["tmp_name"]), 'application/octet-stream', $_FILES["attach"]["name"]);
|
The parameters are described on the following page:
http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|