Page 1 of 1

Help with MIME Attachments

Posted: Mon Nov 07, 2005 4:14 pm
by JonOfAllTrades
Good afternoon, everyone. I have written some code to create an e-mail with an attachment, but I must be missing something somewhere. Hopefully some wise person here will immediately see my mistake!
The prog successfully creates and sends the e-mail, including the attachment, but the contents of the attachment are hacked up. Attaching a very short text file, only the last few characters survive. Attaching a larger (5K) text file, I receive nothing. Attaching a DOC or PDF, the contents are corrupted but the file size is about right.
I believe my MIME is correct. Outlook understands which application to open the attachment with. Unfortunately Outlook does not show all the headers, but if it would be useful I can redirect my output to a file and post it.

Code: Select all

$Headers = "MIME-Version: 1.0\r\nContent-type: multipart/mixed; boundary=\"NEXTMIMEPART\"\r\n";

	$Message = "This is a MIME multi-part e-mail.  You need to upgrade your e-mail client.\r\n";
	$Message .= "--NEXTMIMEPART\r\nContent-Type: text/html\r\nContent-Transfer-Encoding: 7bit\r\n";
	... Adds HTML part of message...

	... Checks if an attachment was included, verifies the type, etc...
	$Message .= "--NEXTMIMEPART\r\nContent-Type: {$_FILES['Resume']['type']}; name=\"{$_FILES['Resume']['name']}\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment\r\n";
	$ResumeFile = fopen($_FILES['Resume']['tmp_name'], "rb");
	...Confirms file opened OK...
	$Message .= "\"" . chunk_split(base64_encode(addslashes(fread($ResumeFile, 1024000))), 76, "\r\n") . "\"\r\n";

	$Message .= "--NEXTMIMEPART--\r\n";
	
	return mail("Admin@TMSContracting.com", $Title, $Message, $Headers);
One oddity that could possibly relate: file_size won't run on $_FILES['Resume']['tmp_name']. I've been trying variations on this theme and browsing docs and Google, no luck so far.
Any ideas? Thanks!

Posted: Mon Nov 07, 2005 5:07 pm
by yum-jelly
Try something like this....

Code: Select all

<?

set_magic_quotes_runtime ( 0 );

$Headers = "MIME-Version: 1.0\r\n";
$Headers = "Content-type: multipart/mixed; boundary='" . NEXTMIMEPART . "'\r\n";

$Message  = "This is a MIME multi-part e-mail. You need to upgrade your e-mail client.\r\n\r\n";
$Message .= "--" . NEXTMIMEPART . "\r\n";
$Message .= "Content-Type: text/html\r\n";
$Message .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$Message .= "+++++++++++++ message here +++++++++++++\r\n";

if ( isset ( $_FILES ) )
{
$Message .= "--" . NEXTMIMEPART . "\r\n";
$Message .= "Content-Type: " . $_FILES['Resume']['type'] . ";\r\n";
$Message .= "\tname='" .  $_FILES['Resume']['name'] . "'\r\n";
$Message .= "Content-Transfer-Encoding: base64\r\n";
$Message .= "Content-Disposition: attachment;\r\n";
$Message .= "\tfilename='" .  $_FILES['Resume']['name'] . "'\r\n\r\n";
$Message .= preg_replace ( "/\r?\n/", "\r\n", chunk_split ( base64_encode ( fread ( fopen ( $_FILES['Resume']['tmp_name'], 'rb' ), filesize ( $_FILES['Resume']['tmp_name'] ) ) ), 72 ) ) . "\r\n";
}

$Message .= "--" . NEXTMIMEPART . "--\r\n";

mail ( "Admin@TMSContracting.com", $Title, $Message, $Headers );

set_magic_quotes_runtime ( get_magic_quotes_gpc () );

?>

yj

Posted: Mon Nov 07, 2005 8:00 pm
by Ambush Commander
Or you could just use a prewritten class like PHPMailer

Posted: Tue Nov 08, 2005 8:52 am
by JonOfAllTrades
Got it! I needed to take out addslashes(), and add filename=. NOT optional.
Thanks for your posts.