Is there a maximum reasonable file size to attach to a MIME email with PHP?

2

I run PHP on IIS6. I have some PHP that successfully sends a 1KB image as an attachment on an email. When I try and attach a 500KB PDF however (having changed the Content-Type), it hangs and after a few minutes I get "FastCGI process exceeded configured request timeout" (Error Number 258 (0x80070102)).

Any thoughts on why it's taking so long to attach the PDF? The solution is not to increase the timeout limit, I can't have users sitting there for 3+ minutes while the file gets sent.

I've included my code below:

    $headers   = "From: ".$from."\r\n";
    $headers .= "Reply-To: ".$from."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
    $headers .="This is a multipart message in MIME format. \r\n\r\n";

    $headers .= "--".$uid."\r\n\r\n";
    $headers .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
    $headers .= $text . "\r\n\r\n";

    $headers .= "--".$uid."\r\n\r\n";
    $headers .= "Content-Type: text/html; charset-iso-8859-1\r\n";
    $headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $headers .= $html  . "\r\n\r\n";

    $headers .= "--".$uid."\r\n\r\n";
    $headers .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
    $headers .= "Content-Transfer-Encoding: base64\r\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $attachment = chunk_split(base64_encode(file_get_contents($path.$filename))); 
    $headers .= $attachment . "\r\n\r\n";

    $headers .= "--".$uid."\r\n\r\n";

    //send the email 
    $mail_sent = @mail( $to, $subject, $text, $headers );

Thanks in advance for any advice.

php
email
iis-6
mime
attachment
asked on Stack Overflow Oct 26, 2010 by Iain

1 Answer

2

Put the attachment in the message parameter of the mail() function instead of the additional headers parameter.

I ran into the same problem today and found that I couldn't submit large files as part of the headers parameter in the mail() function.

e.g.

$headers   = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
$body .="This is a multipart message in MIME format. \r\n\r\n";

$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/plain; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$body .= $text . "\r\n\r\n";

$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: text/html; charset-iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $html  . "\r\n\r\n";

$body .= "--".$uid."\r\n\r\n";
$body .= "Content-Type: image/png; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$attachment = chunk_split(base64_encode(file_get_contents($path.$filename))); 
$body .= $attachment . "\r\n\r\n";

$body .= "--".$uid."\r\n\r\n";

//send the email 
$mail_sent = @mail( $to, $subject, $body, $headers );
answered on Stack Overflow Mar 16, 2011 by Garret Flynn

User contributions licensed under CC BY-SA 3.0