...

View Full Version : Sending Email Attachment with PHP Mail



dprichard
08-25-2011, 09:18 PM
I am trying to send an email attachment using PHP mail and found the script below. I have also found several other scripts. First time trying this and any help would be greatly appreciated. I get the same result no matter what I try which is an email with something similar to this in it:

This is a multi-part message in MIME format.

-==Multipart_Boundary_xfdda778b8216c5c8a793740f6a858fcbx
Content-Type: text/plain; charset="iso-8859-1
Content-Transfer-Encoding: 7bit



==Multipart_Boundary_xfdda778b8216c5c8a793740f6a858fcbx
Content-Type: application/key;
name="test.key"
Content-Disposition: attachment;
filename="test.key"
Content-Transfer-Encoding: base64

Q29weXJpZ2h0IER5bmFtaWMgU29sdXRpb25zIEdyb3VwLCBJbmMuIEFsbCBSaWdodHMgUmVzZXJ2
ZWQNCg0KDQotLS0tLS0gTElDRU5TRSBGSUxFIERBVEEgLS0tLS0tLQ0KMDkxOTcyMVVWM05UcmZq
MldIcUVHVlN1Z2o5QTdEUWgNCnFpQ0Y2anNMNi8ydlZNV0FNWGltVlVUZzV4aWdXN203DQpKaFNI
Q0R3MlhJdW5lkjKcVNQWk96TVRwa1RxcDIya3hkcQ0KUEFaS0diWVFxb21GZ0F2ellaYytiL0huQmZD
VzkxMG0NCnFTZW4xRjZQblZCWUZvSnBjNDNNU21YU0E0cGF0ZmxrDQpJZDJabERDSnpYKzNSV1NE
YUc4L2VSMnBwUzJoeXM0Qw0KbTFqZ21OSVRuc2kFDeC82SGZVZjFxUUlSd29jTXNqTHoNCmVKOWpz
TmlyNTZCTU5kQVRJcXMrZXFEM1FzVHlrdDhkDQo3dm5XUnlUaTUwMVBHUHAvcGx0Z0J4R2VqNHgx
RGxSYg0KdHdPdzRrSU9HVHNsckFHN3dPcXIyaVpxenRraEw2UG8NCnpjcSt6WXpWdzlOcGRRaHFo
SlllZm9SbUROOHluK2VmDQpWZ0VUVlkjElOdXpZalhBbUVXbm4xcGorQUlnd3VwbjRpNw0KZmZwWE1u
RHk3aktRVGdYazBaY0xrMmdvVHhNM2RNUk0NClZlQTBtY1I5bHFSc2NlMXJLdXRZTXhoVmZjMzZv
d2tsDQpHa3hOczZ1RnNLdHUvRFk1bDVjaVBhbmdxSWNEZjUzNA0KY3JFU2VtMG10eWNDd2lNbHFa
K21MRXkzQWNPWWlEZ2wNCnNlYVhzV2tSQ0pIV1B3ekFYTUR3bnhVWG50a0gwODZWDQp1RkVLdjlK
YkRHV0xvQ0Z2SnVESjdrWmIreGE3bHBwQQ0KRmhVZ0JkQkVuakNFRGZQSDd6Ti9WQjlnM242ZHE5
enkNCnBnRGRCOWZtYzlISDM2RUd1ZFFvUEV3T0hkNlBSdnhBDQpIZTkvSW5NNFJCOHo0dW9QbFM0
Uy9SZXl4SHc3bmlVLw0KK0Z2M0FMRGxPOVN5UlEyam9PUXBtKzNLV3BtdjNRRlENCkJnTDFoL3Zr
dEczQVVSVHFwMWlhUGx4V21IcFd1ODZ3DQpZVlFjZDVXN2UrcE93ej09DQotLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLQ0K


-==Multipart_Boundary_xfdda778b8216c5c8a793740f6a858fcbx-



<?php
$name = "Name goes here";
$email = "david@dprichard.com";

$to = "$name <$email>";

$from = "John-Smith ";

$subject = "Here is your attachment";

$fileatt = "test.key";

$fileatttype = "application/key";

$fileattname = "test.key";

$headers = "From: $from";

$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: {$fileatttype};\n" .

" name=\"{$fileattname}\"\n" .

"Content-Disposition: attachment;\n" .

" filename=\"{$fileattname}\"\n" .

"Content-Transfer-Encoding: base64\n\n" .

$data . "\n\n" .

"-{$mime_boundary}-\n";

if(mail($to, $subject, $message, $headers)) {

echo "

The email was sent.

";

}
else {

echo "

There was an error sending the mail.

";

}
?>

MichigamJim
09-01-2011, 03:53 PM
I'm also getting all the text instead of the file. Obviously we're both failing somewhere in the encoding. Here's mine:


<?php
$to = 'Joe@Joe.com';
$from = 'Jim@Jim.com';
$sub = 'Test';
$filelist = '/Volumes/server/sites/thesite/some.jpg,/Volumes/server/sites/thesite/some.pdf';
$files = explode(",",$filelist);
$msg = lvMessage;
$headers = "From: $from";

$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
$msg = "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" . $msg . "\n\n";

// attachments code starts
for($x=0;$x<count($files);$x++)
{
$msg .= "{$mime_boundary}\n";
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$msg .= "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";
}
$msg .= "{$mime_boundary}\n";
$res = @mail($to, $sub, $msg, $headers);

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum