...

View Full Version : Sending Attachments through PHP Script



chellert
11-08-2012, 12:54 AM
I am trying to send an email using PHP script but it seems that the attachments wants to convert to text and embed into the email.

this is the code, not sure why its embedding.

$fileatt = $_FILES['attach']['tmp_name'];
$fileatt_name = basename($_FILES['attach']['name']);
$fileatt_type = substr($fileatt_name, strrpos($fileatt_name, '.') + 1);


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

$body .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .

$body .= "\n\n";

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

$body .= "-{$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" .
$body .= "\n\n" .
"-{$mime_boundary}-\n";

mail($email_to, $subject, $body, $headers);

tangoforce
11-08-2012, 01:00 AM
Use the phpmailer class instead (find it via google). It takes all the hassle out of doing stuff like this the hard way.

chellert
11-08-2012, 07:11 PM
OK, I got the PHPMailer working. Since I pull all of the names from a Database I had to use the AddBCC line.

Is there a way to clear the AddAddress, every time I go through the script? So their names show up in the To line and ONLY their email address shows up there? This way there is no distribution of an entire list

tangoforce
11-08-2012, 08:10 PM
Yes - ClearAddresses()

If you use notepad++ it has an option in the 'view' menu called 'Fold all'.

Click that and then click the little + symbol by the line numbers. It will expand the class file so you can see the function names only without the function code. It makes it a lot easier to see what functions are available to you.

minder
11-09-2012, 03:18 AM
This is a pretty good and popular tutorial on how you can send plain text emails, html emails and emails with attachments from webcheatsheets.com.

http://webcheatsheet.com/php/send_email_text_html_attachment.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum