【发布时间】: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 组件中也是可见的,并不是这个自定义邮件功能所独有的。