2010-12-02 13 views
3

SMTPメールサーバーを介してメールを送信する次のカスタムPHP関数を書きました。PHPカスタムSMTPメール関数返信エラーfputs送信バイトが失敗しましたerrno = 32壊れたパイプ

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; 

} 

機能は非常によく働いていたが、最後の日に私はnextsのPHPエラーをrecived:

お知らせ:fputs()[function.fputs]:8192バイトの送信が= 32 errnoに失敗しました。注意:fputs()[function.fputs]:49バイトの送信がerrno = 32で失敗しました/ cakeapp /内の壊れたパイプは/cakeapp/trunk/app/controllers/components/email.phpの165行目にあります。 trunk/app/controllers/components/email.php on line 169

注意:fputs()[functio n.fputs]:6バイトの送信がerrno = 32で失敗しました。/cakeapp/trunk/app/controllers/components/email.phpの182行目に壊れたパイプ

私はGoogleでいくつかの提案を探していましたが、私は、Connection Time Outsに関連する問題について話しました。

誰でもこの問題を解決する方法を提案できますか?

+0

は、このクラスを試してみてください。http://pastebin.com/e1RiPj6Pを私はすべてのプロジェクトの中にそれを使用します。 – RobertPitt

+0

私はvarible $ timeoutの値でテストしています。これを60秒から90秒に増やします。この変更が問題を解決したと思われますが、私はこの記事を閉じる前にもっと待つでしょう:) –

+0

この問題もZend_Mailコンポーネントを使用して表示され、このカスタムメール機能では一意ではありません。 –

答えて

4

私は同じ問題を抱えていて、プロトコルを 'メール'に設定して解決策を見つけました(もちろん、これは常にメールをプロトコルとして使用する必要はありません。別のプロトコルでは "smtp"私はそれを他のウェブサイトのためにコピーし貼り付けたので、うまく動作しなかったので、私はそれを「メール」に変更しなければならなかった)。私のサンプル設定は、(自分の資格情報を使用して大文字の単語を置き換える)次のようになります。

'protocol' => 'mail',    
'smtp_host' => 'mail.YOUR_WEBSITE_DOMAIN.com.au',    
'smtp_port' => 25,    
'smtp_user' => '[email protected]_WEBSITE_DOMAIN.com.au',    
'smtp_pass' => 'YOUR_PASSWORD_FOR_YOUR_EMAIL_ACCOUNT',    
'smtp_timeout' => 5,// The default value    
'mailtype' => 'html' //default: text   
-2

http://tools.ietf.org/html/rfc2045 エンコードされた行は、末尾のCRLFを数えていない、長い76文字以下でなければなりません。それでも長いコード行がコード化されたデータで見つかった場合、堅牢な実装では行をデコードし、誤ったコードをユーザーに報告する可能性があります。

同じ問題があった場合は、 'chunk_split'を試してみてください!たとえば : $メッセージ= chunk_split($メッセージ)

+0

これは問題を解決しませんでした。 – Brainfeeder

関連する問題