开发者

PHP Custom SMTP Mail function Return ERROR fputs send bytes failed errno=32 Broken pipe

开发者 https://www.devze.com 2023-01-28 09:36 出处:网络
I wrote the next custom PHP function to send mail trough a SMTP MAIL SERVER. function send($format = \'text\'){

I wrote the next custom PHP function to send mail trough a SMTP MAIL SERVER.

function send($format = 'text'){

    $smtpServer  = 'mail.mymailserver.com.mx';
    $port        = '25';
    $timeout     = '60';
    $username    = 'myuser';
    $password    = 'mypassword';
    $localhost   = 'www.mydomain.com.mx';
    $newLine     = "\r\n";

    $smtpConnect = fsockopen( $smtpServer, $port, $errno, $errstr, $timeout );

    fputs( $smtpConnect,'AUTH LOGIN'.$newLine );
    fputs( $smtpConnect, base64_encode( $username )  . $newLine    );
    fputs( $smtpConnect, base64_encode( $password )  . $newLine    );
    fputs( $smtpConnect, 'HELO '       . $localhost  . $newLine    );
    fputs( $smtpConnect, 'MAIL FROM: ' . $this->from . $newLine    );
    fputs( $smtpConnect, 'RCPT TO: '   . $this->to   . $newLine    );

    if( !empty( $this->cc ) ){
        fputs( $smtpConnect, 'RCPT TO: '   . $this->cc   . $newLine    );
    }

    if( !empty( $this->bcc ) ){
        fputs( $smtpConnect, 'RCPT TO: '   . $this->bcc  . $newLine    );
    }

    fputs( $smtpConnect, 'DATA'        . $newLine                  );

    fflush( $smtpConnect );

    $raw  = "";
    $raw  = @fread( $smtpConnect, 255 ) . "@";
    $raw .= @fread( $smtpConnect, 255 );

    fputs( $smtpConnect, 'To:     '  . $this->to . $newLine        );
    fputs( $smtpConnect, 'From:   <' . $this->from .'>' . $newLine );
    fputs( $smtpConnect, 'Subject:'  . $this->subject . $newLine   );

    $format = 'html';

    if( $format == 'text' ){

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

        $message  = $this->bodyText();

        fputs( $smtpConnect, $headers   . $newLine  . $newLine       );
        fputs( $smtpConnect, $message   . $newLine  . '.' . $newLine );

    }else{

        $random_hash = md5(date('r', time()));

        $headers  = "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"\r\n";
        $headers .= "--PHP-alt-" . $random_hash . "\r\n";
        $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . "\r\n";
        $headers .= "Content-Transfer-Encoding: 7bit" . "\r\n";

        $message  = $this->bodyText();

        fputs( $smtpConnect, $headers . $newLine );
        fputs( $smtpConnect, $message . $newLine );

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

        $message  = $this->bodyHtml();

        fputs( $smtpConnect, $headers  . $newLine );
        fputs( $smtpConnect, $message  . $newLine );

        $headers  = "--PHP-alt-" . $random_hash . "--\r\n";

        fputs( $smtpConnect, $headers . '.' . $newLine  );

    }

    fputs( $smtpConnect,'QUIT'      . $newLine );

    return true;

}

The function was working very well, but in the last days I recived the nexts php errors:

Notice: fputs() [function.fputs]: send of 8192 bytes failed with errno=32 Broken pipe in /cakeapp/trunk/app/controllers/components/email.php on line 165

Notice: fputs() [function.fputs]: send of 49 bytes failed with errno=32 Broken pipe in /cakeapp/trunk/app/controllers/components/email.php on line 169

Notice: fputs() [function.fputs]: send of 6 bytes failed with errno=32 Broken pipe in /cakeapp/trunk/app/controllers/components/email.php on line 182

I was searching in Google for some suggestions but the info that I found, talked about a problem related to Connection T开发者_如何转开发ime Outs !

Can Anyone suggest a way to fix this trouble ?


I had the same problem and found the solution by setting the protocol to 'mail' (of course this is not the case that always you need to use "mail" as protocol, I used "smtp" as protocol in another website, and was working fine, but the same code I copied and pasted for some other website and did not work, so I had to change it to 'mail'). My sample configuration looks like this(replace uppercase words with your own credentials):

'protocol' => 'mail',            
'smtp_host' => 'mail.YOUR_WEBSITE_DOMAIN.com.au',            
'smtp_port' => 25,            
'smtp_user' => 'YOUR_EMAIL_ACCOUNT@YOUR_WEBSITE_DOMAIN.com.au',            
'smtp_pass' => 'YOUR_PASSWORD_FOR_YOUR_EMAIL_ACCOUNT',            
'smtp_timeout' => 5,// The default value            
'mailtype' => 'html' //default: text        


https://www.rfc-editor.org/rfc/rfc2045 Encoded lines must not be longer than 76 characters, not counting the trailing CRLF. If longer lines are found in incoming, encoded data, a robust implementation might nevertheless decode the lines, and might report the erroneous encoding to the user.

So, if you had the same problem, TRY 'chunk_split'! For example: $message = chunk_split($message)

0

精彩评论

暂无评论...
验证码 换一张
取 消