草庐IT

PHP 自定义 SMTP 邮件函数返回错误 fputs 发送字节失败 errno=32 Broken pipe

coder 2024-04-16 原文

我编写了下一个自定义 PHP 函数来通过 SMTP 邮件服务器发送邮件。

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;

}

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

注意:fputs() [function.fputs]:发送 8192 字节失败,errno=32 Broken pipe in/cakeapp/trunk/app/controllers/components/email.php on line 165

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

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

我在 Google 中搜索一些建议,但我找到的信息谈到了与连接超时相关的问题!

谁能提出解决这个问题的方法?

最佳答案

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

'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        

关于PHP 自定义 SMTP 邮件函数返回错误 fputs 发送字节失败 errno=32 Broken pipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4338950/

有关PHP 自定义 SMTP 邮件函数返回错误 fputs 发送字节失败 errno=32 Broken pipe的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  10. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

随机推荐