Help with MIME Attachments

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

Post Reply
JonOfAllTrades
Forum Newbie
Posts: 6
Joined: Mon Nov 07, 2005 3:47 pm
Location: Clarksville, TN

Help with MIME Attachments

Post 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!
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Or you could just use a prewritten class like PHPMailer
JonOfAllTrades
Forum Newbie
Posts: 6
Joined: Mon Nov 07, 2005 3:47 pm
Location: Clarksville, TN

Post by JonOfAllTrades »

Got it! I needed to take out addslashes(), and add filename=. NOT optional.
Thanks for your posts.
Post Reply