0

I've been using a php script to send simple emails (no attachment).
Suddenly I discovered that when making the body of the letter longer
than 5323 bytes, it appeared to be sent but was not.

I'm using direct SMTP, and no "PHPmailer" because it's just too
complicated -- and I'm not using mail() because it causes too many
rejects.

Does anyone know how to avoid this limitation -- because I know it
can be done -- the documented limit is 10MB !!!

For anyone interested, here's the code. It splits the message body
into several records, but that didn't help. I've still kept that
code though, because I don't think it's smart to send too large
records.

The script is not intended to handle appendices etc.

<?php

$long = file_get_contents ("testletter.txt");   // This is the mail body!!!
$to = "my name <my@address.com>";       // Replace these for testing
$from = "The Sender <the@sender.com>";
$subject = "MAIL TEST";

SMTPsend ($from, $to, $subject, $long);
exit();

function SMTPsend ($to, $from, $subject, $body)
{   define (LF, "\n");           // Normal linefeed
    define (CRLF, "\r\n");       // Always used by SMTP.

    if ($_SERVER["SERVER_ADDR"])
        define (BR, "<br />");       // HTML linefeed
    else
        define (BR, LF);             // Normal linefeed

//    Build header lines. Remember TWO linefeeds at the end!
    $header = <<<headend
To: $to
From: $from
Subject: $subject
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-Mailer: SMTPsend 2.0


headend;

    $smtp_host = "ssl://mail.whatever.net";   // Change these!
    $smtp_port = 465;
    $smtp_user = "username+whatever.net";
    $smtp_pass = "My SeCrEt PaSsWoRd";

// Only addresses, with brackets
    if ( ($p = strpos($to,"<") ) > 0)
        $smtp_to = substr ($to,$p);
    else
        $smtp_to = "<$to>";

    if ( ($p = strpos($from,"<") ) > 0)
        $smtp_from = substr ($from,$p);
    else
        $smtp_from = "<$from>";

//    Open socket
    $smtp_server = @fsockopen($smtp_host, $smtp_port, $errno, $errstr, 30)
    or die("Attempting connection to $smtp_host on port $smtp_port failed!"
    .BR."Connection refused on $smtp_host at port $smtp_port.");

    if(!$smtp_server)
    {    error_log ("Sending to ".$smtp_host." failed!".LF,3,ERRORLOG);
        print "Connection to $smtp_host on port $smtp_port failed!"
        .BR."[$errorno] $errorstr";
        return false;
    }

    fwrite($smtp_server,
 "EHLO".CRLF
."MAIL FROM:$smtp_from".CRLF
."RCPT TO:$smtp_to".CRLF
."DATA".CRLF);

    fwrite($smtp_server, $header);

    $max = 2048;           // Max record length
    $start = $end = 0;

    echo "Body length: ".strlen($body).BR;
    do
    {   $end = min (strlen($body),$start+$max);
        $len = $end-$start;
//      echo "Start = $start, End = $end".BR;
        fwrite($smtp_server, substr ($body,$start,$len));
        $start += $len;
    } while ($start < strlen($body));

    fwrite($smtp_server, CRLF.CRLF.".".CRLF."QUIT".CRLF);
    fclose($smtp_server);
    error_log ("Sending complete.".LF,3,ERRORLOG);
    return true;
}
?>
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ingeva
0

I have not found the error per se, but I started over and included checking
of the SMTP server responses, so I now have a working test script.

If anyone is interested (still without appendix/multipart capability) I will make it available. Just send an e-mail from here:

http://ingeva.qreply.net

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.