我编写了下一个自定义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;
}
该功能运行良好,但是在最近的几天中,我收到了nexts php错误:
注意:fputs()[function.fputs]:发送8192字节失败,错误码为errno = 32,第165行的/cakeapp/trunk/app/controllers/components/email.php中的管道损坏
注意:fputs()[function.fputs]:49字节的发送失败,错误码为errno = 32,第169行的/cakeapp/trunk/app/controllers/components/email.php中的管道损坏
注意:fputs()[function.fputs]:发送6个字节失败,错误码为errno = 32,第182行的/cakeapp/trunk/app/controllers/components/email.php中的管道损坏
我在Google中搜索一些建议,但发现的信息涉及与连接超时有关的问题!
任何人都可以提出解决此问题的方法吗?
最佳答案
我遇到了同样的问题,并通过将协议(protocol)设置为“邮件”找到了解决方案(当然,并非总是需要使用“邮件”作为协议(protocol),我在另一个网站中使用“smtp”作为协议(protocol),工作正常,但是我复制并粘贴到其他网站的相同代码却无法正常工作,因此我不得不将其更改为“mail”)。我的示例配置如下所示(用您自己的凭据替换大写单词):
'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