Jump to content

Email Attachment Problem


js_280

Recommended Posts

I've been trying to figure out what's wrong with this code for several days now...

 

Running PHP 5.8 on Windows Server 2003

 

The pdf files are being created correctly, I can go open them with Adobe without a problem.  The problem comes when the script attempts to add the pdf file as an attachment.  Something is apparently messing with the headers because the pdf file is simply dumped into the email as plain text.  Can anyone see something that I've missed?  I found the code on devshed and thought it would port over without problem...

 

	try
	{
		$pdf->end_document("");

	    $buf = $pdf->get_buffer();
	    $len = strlen($buf);

	    $filename = rand(0, getrandmax()).".pdf";
	    file_put_contents($filename, $buf);
	    
	$to = $branches[$i]['email'];
	//define the subject of the email
	$subject = 'Automated Collection Letters for Branch '.$branches[$i]['num'];
	//create a boundary string. It must be unique
	//so we use the MD5 algorithm to generate a random hash
	$random_hash = md5(date('r', time()));
	//define the headers we want passed. Note that they are separated with \r\n
	$headers = "From: admin@somwhere.com\r\nReply-To: admin@somewhere.com";
	//add boundary string and mime type specification
	$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
	//read the atachment file contents into a string,
	//encode it with MIME base64,
	//and split it into smaller chunks
	$attachment = chunk_split(base64_encode(file_get_contents($filename)));
	//define the body of the message.
	ob_start(); //Turn on output buffering
	?>
	--PHP-mixed-<?php echo $random_hash; ?> 
	Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

	--PHP-alt-<?php echo $random_hash; ?> 
	Content-Type: text/html; charset="iso-8859-1"
	Content-Transfer-Encoding: 7bit

	--PHP-alt-<?php echo $random_hash; ?>--

	--PHP-mixed-<?php echo $random_hash; ?> 
	Content-Type: application/pdf; name="<?php echo($filename); ?>" 
	Content-Transfer-Encoding: base64 
	Content-Disposition: attachment 

	<?php echo $attachment; ?>
	--PHP-mixed-<?php echo $random_hash; ?>--

	<?php
	//copy current buffer contents into $message variable and delete current output buffer
	$message = ob_get_clean();
	//send the email
	mail('someone@somewhere.com', $subject, $message, $headers);
	}
	catch(Exception $e)
	{

	}

 

 

This is what I'm getting in the body of the email...

 

--PHP-mixed-fa9e60a19ce2cbbd18d93de8577f7d27

Content-Type: multipart/alternative; boundary="PHP-alt-fa9e60a19ce2cbbd18d93de8577f7d27"

 

--PHP-alt-fa9e60a19ce2cbbd18d93de8577f7d27

Content-Type: text/html; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

 

--PHP-alt-fa9e60a19ce2cbbd18d93de8577f7d27--

 

--PHP-mixed-fa9e60a19ce2cbbd18d93de8577f7d27

Content-Type: application/pdf; name="31536.pdf"

Content-Transfer-Encoding: base64

Content-Disposition: attachment

 

JVBERi0xLjUKJeTjz9IKMSAwIG9iagpbL1BERi9JbWFnZUIvSW1hZ2VDL0ltYWdlSS9UZXh0XQpl

bmRvYmoKNSAwIG9iago8PC9MZW5ndGggNiAwIFIKL0ZpbHRlci9GbGF0ZURlY29kZQo+Pgpz

bmRvYmoKNSAwIG9iago8PC9MZW5ndGggNiAwIFIKL0ZpbHRlci9GbGF0ZURlY29kZQo+dHJl

YW0KeJyNVO9Po0AQ/c5fMcldLppUrlCg6Leq1fNStVdJLiYmly0MlnNhcXdp5b+/2W21P6zxSlsI

O/vemzcPnh0PunR44Ps9tx/G0A9C1+8GkJbOaeJ8v6C1GJLcMf8jx9QmmXNwJjjHVBeignOsmdQl

Vvow+esME+eX87xGjfpuHIfQ9yM3OPY3UQOLGmyijm/hVLyA50XePqwgdoPeMfQ97z+w7pqy

Vvow+esME+0KLq

wGAE0Au9IN4D2e9CFHY3kXyL5G8i/Rze3FxdDCeQXI3HwyT5AKcXfoLjk/bLyeDH4Bomt4PzD2D8

z+SMh6MRnF0l92+t+eEGVtcN4xgW+xAIPuzFFmQyPIFBmuovNF0/CnsGoAtHx5FdvReNBJam

z+SMh6MRnF0l92+t+eEGVtcN4xgW+oqk0

LAo9g3OxyDjCJVMwo98UsQLdyAozEHOUoAWYPek6FtlbLKCg2hkCKy2eyFdkRhSRfe3Fbhi5q5t+

sJxeUaUIrWiIbo6Qs4ITFbHUrLXKZFZUj6+0GmWpOlBoYHWNTCriYxryRhKv6cQqMl8qNrKg

sJxeUaUIrWiIbo6Qs4ITFbHUrLXKZFZUj6+ZO22

iilChSkqxWTrAiQzVLjaqKithmdgSoQu8tYws81e2SNWadsBprUgT+jqanIHQtq2JZZizvi7

iilChSkqxWTrAiQzVLjaqKithmdgSoQu8tYws81e2SNWadsBprUgT+vq0W

KWqUut3pfoSPtIEWU0TTp6IhcA6s0aJkukgZ5y3JSUWJy3ZEbrwi/yXVa6I0BgGrMsPOlv5LspEk

PRz0Hw63lbTGMhcGVUuW0VTxFUdLVql3JrCMJq6QJLA8Jwcw25H/G2FGfcECIRPGs+UQaViaPdF5

VqjXmdhwkXSynHYtLAUvnmyxoZpye7kQ8umtdlu8JK9WYTC4NVMasgZf4+vCmCOjWc6Q19Ao

VqjXmdhwkXSynHYtLAUvnmyxoZpye7kQ8umtdlu8JK9WYTC4NVMasgZf4+MkQJ

Psed6injzCRu2oJx18BR7XQnJH92PjRw8LvhURTE1Hvf2xdiSUZ1VveD5cN3zWTB22ob+2xp+TdY

v1oVXD/KbcjbPC9SPNli3ca5YC/r5cDzvG2AxLRt/K9MWrGstfF3lWWb1wyn2kaHUR6KijJjMmdn

RRmkoTSKHsXcppv6rxtZC4Xu2yvoH07u1TkKZW5kc3RyZWFtCmVuZG9iago2IDAgb2JqCjc2NApl

bmRvYmoKNyAwIG9iago8PC9Qcm9jU2V0IDEgMCBSCi9Gb250PDwvRjAgMyAwIFIKPj4KPj4KZW5k

b2JqCjkgMCBvYmoKPDwvTGVuZ3RoIDEwIDAgUgovRmlsdGVyL0ZsYXRlRGVjb2RlCj4+CnN0..........

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.