Page 1 of 1

Mail Attachments of bigger size - Apache crashes

Posted: Wed Sep 18, 2002 6:31 am
by l331g33k
Hi,

after a lot of trying (and cursing) I was able to generate an email with attachment. The code is as follows:

Code: Select all

(...)
	/* select mode - either mail the pdf file or view it */
	switch($mode)
	{
		case "mail":		
			/* init some vars */
			$filename	= "abc_".$username.".pdf";
			$filepath 	= "../../tmp/" . $filename;
			$boundary	= $boundary = strtoupper(md5(uniqid(time())));
			
			/* init mail configuration */
			ini_set("SMTP", "smtp.abc.de");
			ini_set("smtp_port", 25);
			
			/* echo out status */
			echo "<b>Filename</b> ........ = " . $filename . "<br>";
			echo "<b>Filepath</b> ........ = " . $filepath . "<br>";
			echo "<b>Mime boundary</b> ... = " . $boundary . "<br>";
					
			/* temp solution */
			$mail_message 	 = "f***ing test mail message";
			$to		 = "abc@abc.de";
			$subject	 = "one freaking subject";
			$sender_name	 = "sender name";
			$sender_mail	 = "abc@abc.de";

			/* build mime header */	
			$mime		 = "From: "$sender_name" <$sender_mail>\r\n";
			$mime		.= "To: <$to>\r\n";
			$mime 		.= "MIME-Version: 1.0\r\n";
			$mime		.= "Content-Type: multipart/mixed; boundary=" . $boundary . "\r\n\r\n" . 
					   "Your mail system does not support MIME.\r\n";
			$mime		.= "--$boundary\r\n";
			$mime		.= "Content-Type: text/plain\r\n" . 
					   "Content-Transfer-Encoding: 8bit\r\n";
			$mime		.= $mail_message;
			$mime		.= "\r\n--$boundary\r\n" . 
					   "Content-Type: application/pdf; name="$filename"\r\n" . 
					   "Content-Transfer-Encoding: base64\r\n" . 
					   "Content-Disposition: attachment; filename="$filename"\r\n\r\n";

			/* encode file */
			$filecontent 	 = chunk_split(base64_encode(fread($fh = fopen($filepath, "rb"), filesize($filepath))));
			fclose($fh);
			
			$mime		.= $filecontent;
			$mime		.= "\r\n--$boundary--";
			
			echo "<br><b>MIME header:</b><br><font color="#777777">\n\n";
			echo $mime;
			echo "\n\n</font>";
			
    /* here it crashes in mail() */			
			if(@mail($to,$subject,"",$mime))
			{
				echo "<br><br><font color=green><b>Mail was sent successfully</b></font>";
			}
			else
			{
				echo "<br><br><font color=red><b>Unable to send eMail - mail() error</b></font>";
			}
			break;
			
		case "view":
			/* redirect user to file */
			header("Location: ./../../tmp/abc_" . $username . ".pdf");
			break;
	}
(...)
If I use a file with very small size (~1000 B) it works just fine. But if I use a bigger file (pdf) which is approximatelly 200 KB, it (Apache 1.3.26) crashes (giving a windows error with something like "could not execute read on memory 0x00ABCDEF1234"... or something like it). I am using PHP4.2.2 and Windows 2000 SP3.

Could someone please give me a hint? I guess that mail() just cannot handle a mime header of a huge size, but i hope there is a workaround. Thx. :?

Posted: Wed Sep 18, 2002 6:36 am
by jason
Is that a PHP, Apache, or Windows error? And what is the actual error? 200kb isn't that big and should be fine as an attachment.

Posted: Wed Sep 18, 2002 6:39 am
by l331g33k
Its this default-windows-error, when windows encounters an illegal memory operation (e.g. a nullpointer or something like it) it gives this dialog box saying something about "the process "read" could not be done on the memory" (i got a german system, so i don't know what exactly it says on an english platform)

EDIT: The mime header looks something like this:

_________________________________________
From: "sender name" <abc@abc.de>
To: <abc@abc.de>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=EBC8C9FDA79CC30C675739D7CBD91F67

Your mail system does not support MIME.
--EBC8C9FDA79CC30C675739D7CBD91F67
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
f***ing test mail message
--EBC8C9FDA79CC30C675739D7CBD91F67
Content-Type: application/pdf; name="abc_abc.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="abc_abc.pdf"

JVBERi0xLjMKJeTjz9IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIKL0ZpbHRlci9GbGF0ZURlY29k
ZQo+PgpzdHJlYW0KeJwDAAAAAAFlbmRzdHJlYW0KZW5kb2JqCjYgMCBvYmoKOAplbmRvYmoKOCAw
IG9iago8PC9UeXBlL1hPYmplY3QKL1N1YnR5cGUvSW1hZ2UKL1dpZHRoIDMyNwovSGVpZ2h0IDgw
Ci9CaXRzUGVyQ29tcG9uZW50IDgKL0NvbG9yU3BhY2UvRGV2aWNlUkdCCi9GaWx0ZXJbL0RDVERl
Y29kZV0KL0RlY29kZVBhcm1zWzw8Pj5dCi9MZW5ndGggOSAwIFIKPj4Kc3RyZWFtCv/Y/+AAEEpG

(.................file contents...................)

IDAwMDAwIG4gCjAwMDAwMjcwNTYgMDAwMDAgbiAKMDAwMDAyODU2OCAwMDAwMCBuIAowMDAwMDI4
NzUxIDAwMDAwIG4gCjAwMDAxNzkwNzYgMDAwMDAgbiAKMDAwMDE3OTA5OSAwMDAwMCBuIAp0cmFp
bGVyCjw8L1NpemUgMjEKL0luZm8gMyAwIFIKL1Jvb3QgMiAwIFIKL0lEWzw0MjRGMUY4NzRCQ0Iz
MkY0NjI4M0ExOEJFNTg0RjgwRT48NDI0RjFGODc0QkNCMzJGNDYyODNBMThCRTU4NEY4MEU+XQo+
PgpzdGFydHhyZWYKMTc5MjMwCiUlRU9GCg==

--EBC8C9FDA79CC30C675739D7CBD91F67--
_________________________________________