【问题标题】:PHP Custom SMTP Mail function Return ERROR fputs send bytes failed errno=32 Broken pipePHP 自定义 SMTP 邮件函数返回错误 fputs 发送字节失败 errno=32 Broken pipe
【发布时间】:2011-05-19 08:17:33
【问题描述】:

我编写了下一个自定义 PHP 函数来通过 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;

}

该功能运行良好,但在最后几天我收到了 nexts php 错误:

注意:fputs() [function.fputs]:第 165 行 /cakeapp/trunk/app/controllers/components/email.php 中的 errno=32 Broken pipe 发送 8192 字节失败

注意:fputs() [function.fputs]:发送 49 字节失败,errno=32 Broken pipe in /cakeapp/trunk/app/controllers/components/email.php 在第 169 行

注意:fputs() [function.fputs]: 发送 6 字节失败,errno=32 Broken pipe in /cakeapp/trunk/app/controllers/components/email.php 在第 182 行

我在 Google 中搜索了一些建议,但我发现的信息涉及与连接超时有关的问题!

任何人都可以提出解决此问题的方法吗?

【问题讨论】:

  • 试试这个类:pastebin.com/e1RiPj6P我在每个项目中都使用它。
  • 我正在使用变量 $timeout 的值进行测试,我会将其从 60 秒增加到 90 秒。看来此更改解决了问题,我将等待更多时间关闭此帖子:)
  • 这个问题在 Zend_Mail 组件中也是可见的,并不是这个自定义邮件功能所独有的。

标签: php email sockets smtp


【解决方案1】:

我遇到了同样的问题,并通过将协议设置为“邮件”找到了解决方案(当然,并非总是需要使用“邮件”作为协议,我在另一个网站上使用“smtp”作为协议,并且工作正常,但是我为其他网站复制和粘贴的相同代码不起作用,因此我不得不将其更改为“邮件”)。我的示例配置如下所示(用您自己的凭据替换大写单词):

'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        

【讨论】:

    【解决方案2】:

    https://www.rfc-editor.org/rfc/rfc2045 编码行不得超过 76 个字符,不包括尾随 CRLF。如果在传入的编码数据中发现较长的行,一个健壮的实现可能仍然会解码这些行,并可能向用户报告错误的编码。

    所以,如果您遇到同样的问题,请尝试“chunk_split”! 例如: $message = chunk_split($message)

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 2015-05-24
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多